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

What Happens When Starting Redis

Redis is a great example of high-performance server. When I worked as a backend developer, my mentor recommended that I should read the code of Redis. And so I did, it’s rewarding. As the time goes by, the ideas behind Redis are still cutting-edge, so I think it’s time to retrospect the implementation of Redis. This post is a good beginning. It’s my habit to start from the entry when meeting a big project. In Redis, server.c is responsible for bootstrap, that’s what I pay close attention to in this post. ...

Oct 22, 2018 · 4 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

A Few Things That I Know About Spark

This post concludes a global view of Spark using a mind map, in which I try to enumerate what I know about it. A graph for the relationship of terminologies like Job, Stage is illustrated firstly so that you would be confused. ...

Sep 3, 2018 · 1 min · xgugeng

Handling Missing Keys in Python

In line with fail-fast philosophy, dict access with d[k] raises an error if k is not an existing key. A common solution is d.get(k, default) which return a default rather handling KeyError if k is not present. This post introduces three alternatives - setdefault, defaultdict and __mising for this situation. ...

Aug 27, 2018 · 2 min · xgugeng

Consensus in Distributed System

The distributed consensus problem deals with reaching an agreement on a single data value among a group of process connected by an unreliable network. The processes must put forth their candidate values, communicate with one another, and agree on a single consensus value in presence of failures of some processes. Examples of applications of consensus include: Leader election / Mutable Exclusion Commit or Abort in distributed transactions Reaching agreement about which process has failed Clock phase synchronization Load balancing A visual diagram of this problem unfolds in the following. Each process pk with an input value uk runs a program to exchange its value. Finally the output of all non-faulty processes become identical. It is permissive that one or more processes may fail at any time, but the output v must be equal to the value of at least one process. ...

Aug 19, 2018 · 11 min · xgugeng

Unpacking in Python

Python provides many friendly advanced API for programmer, one of which is unpacking. By unpacking, it means getting all the elements from a container. A simplest case in the below demonstrates assigns the two items in tuple to variables. >>> a, b = (1,2) >>> a 1 >>> b 2 Besides tuple, every iterative object can support unpacking, such as list, dictionary, set, string, generator etc. Let’s traverse them one by one. ...

Aug 14, 2018 · 2 min · xgugeng