How to Handle a Deque in Python

Deques are a great way to handle memory efficient appends to a list-like object, it is a special module that allows you to handle list items in a more appropriate way. Create a deque To create a list simply import the deque module from collections library and call deque(_items_) on a variable. >>> from collections import deque >>> dq = deque('123') >>> dq deque(['1', '2', '3']) >>> type(dq) <class 'collections.deque'> Or if you wish to create an empty deque....

April 14, 2018 · 3 min · Franccesco Orozco

Create and Object With Namedtuple

Namedtuples are a subclass of typename which allow us to create objects (or classes) with tuple-like attributes, it’s easy to create a class with immutable attributes with this library. Creating an object To create a namedtuple we only need the name of the class first and then the attribute list. >>> from collections import namedtuple >>> Person = namedtuple('Person', 'name job_position sex married') >>> john = Person('john', 'developer', 'male', False) >>> john Person(name='john', job_position='developer', sex='male', married=False) Be aware that you can pass an argument list as a string ('att1 att2'), as an actual list (['att1', 'att2']) or a string with comma separated values ('att1, att2')....

April 7, 2018 · 2 min · Franccesco Orozco

Classes and Special Methods in Python

These are my study notes on Classes and special methods. As always, if something is wrong then you can always correct me, it would help me and everybody else. Class ‘Employee’ example This is the example class that we’re going to use, a class Employee which we will inherit from to create more classes like Manager and Supervisor: class Employee(object): """Class simulating an Employee with basic attributes.""" total_employees = 0 def __init__(self, name, rate, position): self....

March 18, 2018 · 6 min · Franccesco Orozco

Understanding Accessors in Ruby

One thing that puzzled me as a newbie (disclaimer: I still am) are accessors in Ruby, more commonly known as setters and getters or explicitly described as attr_reader, attr_writer and attr_accessor. Now let’s dive into the code first and describe the concepts of accessors after we’re done with coding. Initializing a Class Let’s say we want to create a class to resemble a Person with a name, and finally let’s try to access that name outside the class:...

February 25, 2018 · 5 min · Franccesco Orozco