Connection Pool in redis-py

A typical example of using Collection Pools in redis-py is: pool = redis.ConnectionPool(host='localhost', port=6379, db=0) r = redis.Redis(connection_pool=pool) ...

Jan 18, 2019 · 3 min · xgugeng

Weak References

In Python, objects are never explicitly destroyed. However, they may be garbage-collected when they become unreachable. The del statement deletes names, not objects. An object may be garbage collected as result of a del command, but only if the variable deleted holds the last reference to the object, or if the object becomes unreachable. In CPython, the primary algorithm for garbage collection is reference counting. Essentially each object keeps count fo how many references point to it. As soon as that refcount reaches 0, the object is immediately destroyed: CPython calls the __del__ method on the object (if defined) and the frees the memory. But sometimes, it is useful to have a reference to an object that does not keep it around longer than necessary. Week references to an object do not increase its reference count. A weak reference does not prevent the referent from being garbage collected. ...

Nov 5, 2018 · 2 min · xgugeng

Variables Are Labels, Not Boxes

Variables are labels, not boxes. Take the following code for example: a = [1, 2, 3] b = a a.append(4) print(b) If you imagine that variables are like boxes, you cannot make sense of assignment in Python. For an assignment, you must always read the right-hand side first: that’s where the object is created or retrieved. After that, the variable on the left is bound to the object, like a label stuck to it. Just forget about the boxes. ...

Nov 1, 2018 · 3 min · xgugeng

When Python Executes Decorators

A key feature of decorators is that they run right after the decorated function is defined. This is usually at import time. ...

Oct 29, 2018 · 2 min · xgugeng

Design Patterns with First-class Functions

Design patterns are language-independent, but in the context of languages with first-class functions, it’s beneficial to rethink some patterns. The general idea is: you can replace instances of some participant class with simple functions, reducing a lot of boilerplate code. In this post, I will refactor Strategy using functions objects. ...

Oct 27, 2018 · 4 min · xgugeng

A List Comprehension Puzzler

Sometimes, we need to initialize a list with a certain number of nested lists, for example, a list with 3 lists of length 3 that can represent a Tic-tac-toe board. >>> board = [[''] * 3] * 3 >> board [['', '', ''], ['', '', ''], ['', '', '']] >> board[1][2] = 'X' >> board [['', '', 'X'], ['', '', 'X'], ['', '', 'X']] The output is not expected, why? ...

Oct 24, 2018 · 2 min · xgugeng

Using Mixins with Python

Python supports multiple inheritance, while it doesn’t provide the syntax for interface. See PEP-0245. Of course, you can use the abc module for abstract base classes, which seems tricky. Python introduces a concept called mixin that encapsulates behavior that can be reused in other classes. It’s supported via multiple inheritance, the difference of inheritance and mixin is, inheritance means “is-a” and mixin means “has-a”. This post will illustrate how mixin works with a simple example. ...

Oct 18, 2018 · 2 min · xgugeng

Special Methods in Python Data Model

You can think of the Data Model as a description of Python as a framework. It formalizes the interfaces of the building blocks of the languages itself, such as sequences, iterators, functions, classes, context managers and so on. Python allows operator overloading, which let us to define the custom behavior for a class. For instance, if a class defines a __str__(), then it can return a readable string representation of an object. In this post, I will enumerate some special methods that can change the behavior of an object and may putted into practice in daily work. ...

Oct 16, 2018 · 4 min · xgugeng

Managing Ordered Sequences With bisect

The bisect module offers two main functions - bisect and insort - that use the binary search algorithm to quickly find an d insert items in any sorted sequence. ...

Sep 24, 2018 · 2 min · xgugeng

Python Closure

A closure is a function with an extended scope that encompasses non-global variables but not defined there. It does not matter whether the function is anonymous of not, what matters is that it can access non-global variables that are defined outside of its body. ...

Sep 19, 2018 · 3 min · xgugeng