Wednesday, September 21, 2016

Python dictionary: Past, Present, Future

I've made a presentation about Python dictionary on SPb Python meetup yesterday.
Thanks to all who came to the meetup :)

In my talk I've made a review of how the dictionary in CPython 2.x works. Also I've discussed dictionary in CPython 3.x, and reviewed the changes in CPython 3.6.
In addition to CPython I've superficially reviewed the internal work of dictionary in alternative Python implementation such as PyPy, IronPython and Jython.

Friday, August 26, 2016


Any sufficiently complicated C or Fortran program contains an ad hoc, informally-specified, bug-ridden, slow implementation of half of Common Lisp — Philip Greenspun 
Organizations which design systems ... are constrained to produce designs which are copies of the communication structures of these organizations — Melvin Conway

Sunday, August 14, 2016

Aztec Code generator in Python

When I was in Europe I've noticed unusual 2D barcodes printed on train tickets. These codes are called Aztec Code. I've already seen this code when played the CTF games.
I've decided to understand the principle of encoding and create Aztec code generator in Python. It shouldn't be so hard, because I've already understood the principles of QR Code encoding recently.
I've found a specification (surprisingly in Russian) describing the Aztec Code and created github repository. 
The most difficult part was to implement the algorithm of searching the optimal encoding sequence from the specification.
Source code of my version of Aztec code generator in Python on github:
Here are some resulting Aztec codes:
This is 71x71 Aztec code with 394 '\x00' bytes :

And this one is with 394 '\xff' bytes:

And here is 53x53 Aztec code with 433 "3" digits:

Tuesday, June 14, 2016

Amusing QR codes

This weekend I've implemented own QR Code generator in pure Python. The link to github repository:
I want to share amusing QR codes I've found during testing of my QR code generator.
A mode is "numeric", error correction level is "Q", here version is 20 (97 x 97), masks 0 to 7.
module size is 2 pixels.
The encoded data contain only zeros, so the error correction codes also contain zeros. That's why the patterns are so uniform.

Thursday, April 28, 2016

Maximum lines of code for Python script

I've created a script with 10 million non-empty and non-comment lines. Python process has consumed over 10 Gigs of RAM, but successfully executed :)

So what is the maximum lines of code permitted in Python?
No limitation.

Sunday, April 24, 2016

Python frozen modules __hello__ and __phello__

Under Python 2.7:
>>> import __hello__
Hello world...
>>> import __phello__
Hello world...
>>> import __phello__.spam
Hello world...

Under Python 3.x:
>>> import __hello__
Hello World!

If check a file of this modules the next result will be returned:
>>> __hello__.__file__

The byte-code of these modules (see Python' source file ./Python/frozen.c) is compiled into Python lib (python27.dll on Windows and on Linux).

To check whether the module is frozen it's possible to use imp.is_frozen:
>>> import imp
>>> imp.is_frozen('__hello__')
>>> imp.is_frozen('__phello__')
>>> imp.is_frozen('__phello__.spam')

It's also possible to get the code object of these modules and for example get the bytecode:
>>> imp.get_frozen_object('__phello__.spam').co_code

Or get the code object filename:
>>> imp.get_frozen_object('__hello__').co_filename
>>> imp.get_frozen_object('__phello__').co_filename
>>> imp.get_frozen_object('__phello__.spam').co_filename

To load a frozen module Python C API function PyImport_ImportFrozenModule is used.

Thursday, April 14, 2016

Python anonymous class name with anonymous class variable name

In Python it is possible to create new type object without any name.
Moreover, you can create an anonymous class variable.
For example to create Phantom class with anonymous class variable:
>>> Phantom = type('', (object,), {'': 'surprise'})
>>> p = Phantom()
>>> Phantom.__name__
>>> p.__class__
<class '__main__.'>
>>> getattr(p, '')