Wednesday, May 20, 2015

Nina Zakharenko - Technical Debt - The code monster in everyone's closet - PyCon 2015

Technical debt is the code monster hiding in everyone's closet. If you ignore it, it will terrorize you at night. To banish it and re-gain your productivity, you'll need to face it head on.

Alex Gaynor - Techniques for Debugging Hard Problems - PyCon 2015

Sometimes your programs have bugs. Often they're shallow things, simple AttributeErrors or TypeErrors. Sometimes they're large, complex, and nearly impossible to debug. This talk explores techniques for figuring these out.

Tuesday, May 19, 2015

Distributed Systems 101 - PyCon 2015

A very brief introduction to the theory and practice of distributed systems.

Thursday, March 12, 2015

Console editor with Notepad behavior

When I started using Linux (it was Ubuntu in 2007), I wanted a console text editor which would support keyboard shortcuts used in Windows Notepad:

- Ctrl + Left/Right -- go to the beginning/end of the previous/next word
- Ctrl + Home/End -- go to the beginning/end of the file
- Shift + Left/Right -- select previous/next character
- Control + Shift + Left/Right -- select previous/next word
- Control + Shift + Home/End -- select line to the beginning/end
- Control + C/V/X/Z -- copy/paste/cut selected text, undo last operation

Nano, which is present in Ubuntu out of the box, is quite simple, but doesn't support almost all of the shorcuts.

Vim -- I didn't like it immediately -- I even didn't know to close it.

Emacs -- I could not exit it either, but I liked it more.

In the end I stopped looking and used what I have -- Vim/Emacs/Nano.

But today I found out that Emacs starting with version 22.1.1 has Cua mode.
The command M-x cua-mode sets up key bindings that are compatible with the Common User Access (CUA) system used in many other applications.
Add this to `~/.emacs`:

(cua-mode t)
(setq cua-auto-tabify-rectangles nil) ;; Don't tabify after rectangle commands
(transient-mark-mode 1) ;; No region when it is not highlighted
(setq cua-keep-region-after-copy t) ;; Standard Windows behaviour

and it works!

Be aware that Emacs has its own clipboard, and Ctrl+V will not paste system clipboard contents.

Sunday, March 8, 2015

Shell script argument with a default value

So, I made I shell script which makes some backup copies. I want it to accept one argument which is the directory where to store the backups. And if it was not provided, it should have some default value.

This works:
if [ -z "$1" ]; then
echo "${BACKUP_DIR}"

But it's too verbose. Of course you could write it in one line, but here is a nicer version:
[ -z "$1" ] && BACKUP_DIR="/home/ubuntu/backups" || BACKUP_DIR="$1"

But the variable name is repeated twice. We can do better:

BACKUP_DIR=$([ -z "$1" ] && echo "/home/ubuntu/backups" || echo "$1")

But I think this is the clear winner:


If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.

Friday, January 23, 2015

URLs vs URIs

Whenever I needed to work with a URL in my code I wasn't sure how to name the variable: `url` or `uri`? What term to use in documentation: URL or URI?

Some projects use term `URI` ( `Connection URI` in Flask,  `redirect_uri` in Facebook API). Other projects use term `URL` (like SqlAlchemy or `redis.from_url`).

I've been thinking about this occasionally in the last several years. Today, after reading again the Wikipedia article about URI I reached a conlusion.

A URN is a URI that identifies a resource by name in a particular namespace. A URN can be used to talk about a resource without implying its location or how to access it.
The International Standard Book Number (ISBN) system for uniquely identifying books provides a typical example of the use of URNs. ISBN 0-486-27557-4 cites unambiguously a specific edition of Shakespeare's play Romeo and Juliet. The URN for that edition would be urn:isbn:0-486-27557-4. To gain access to this object and read the book, its location is needed, for which a URL would have to be specified.

A URL is a URI that, in addition to identifying a web resource, specifies the means of acting upon or obtaining the representation, specifying both its primary access mechanism and network location. For example, the URL refers to a resource identified as /wiki/Main_Page whose representation, in the form of HTML and related code, is obtainable via HyperText Transfer Protocol (http) from a network host whose domain name is

In other words: the URN defines an item's identity, while the URL provides a method for finding it.

Or in my words, URN is like an identifier of a resource, and URL is identifier of one of its locations (but not resources global identity).

So, every URL is a URI, but not every URI is a URL.

So, if we talk about a database whose location we want to put into settings, we should use term URL, because it's a location (`postgresql://scott:tiger@localhost/mydatabase`)! Using an URN (something like `urn:database:6e8bc430-9c3a-11d9-9669-0800200c9a66`) would not work in these cases. So if we can't use URNs, we should not use term URI which includes URN.

Thursday, January 1, 2015

Websockets chat in Python

I've made a simple Websockets server in Python using Flask + gevent-websocket.

The idea was taken from

It uses Redis Pub/Sub subsystem to ensure that all connected clients receive the messages, even if there are several Gunicorn workers.

See the code here:

Feel free to comment and improve.