August 25, 2014

Knight's tour in 8086 assembly (MASM)

Almost 20 years ago I wrote an assembly code to solve Knight’s tour problem as my C solution was too slow. The code was lost on a backup disk and I’ve accidentally found it over the weekend. If you have MASM you can make a binary and test it.

August 10, 2014

GoPacker

A year ago I was working on a server in Go that I wanted to be easy to deploy and move around. Go is great for providing you with one statically linked binary, but I also had few files that needed to be bundled with the code (CSS, JavaScript, images).

At that time I couldn't find a solution that would fit my needs so I wrote one: GoPacker. Hope you find this script useful.

March 7, 2013

Configure git to use a different tool for diffing

I don't like default git diff output: I prefer vimdiff or tkdiff. Here is a recipe that explains how to set up external diff.
Your basic ~/.gitconfig should have [diff] section as in:
[user]
        name = <your name>
        email = <your@email>
[color]
        status = auto
        diff = auto
        branch = auto
        interactive = auto
[core]
        editor = vim
        pager = less -FRSX
[diff]
        external = /home/<username>/bin/gittkdiff.sh
[merge]
        tool = vimdiff
Create external gittkdiff.sh and make it executable (chmod +x):
#!/bin/sh

/usr/bin/tkdiff "$2" "$5" | cat
For vimdiff use:
#!/bin/sh

/usr/bin/vimdiff "$2" "$5" | cat



[Update - 08/10/2014]

Newer git can do this for you:
git config --global diff.tool tkdiff
git config --global merge.tool tkdiff
git config --global --add difftool.prompt false

March 4, 2013

Replacing Python's positional arguments with keyword arguments

I was renaming a positional function argument named id (which was masking Python's built in function id()), but that broke the rest of the third party code. I've just discovered a very nasty language feature.

A function in Python that takes only positional arguments:

def foo(x, y):
    print x ** y

should be called as:

foo(23)

But when calling a function it is possible to use keyword arguments in place of the given positional arguments:

foo(2, y=3)
foo(x=2, y=3)

Whoever wrote foo() counts it will always be called with positional arguments, but there is no way to prevent someone to use keyword calling style (foo(2, y=3)). If foo() arguments are renamed, e.g. to make it more readable:

def foo(base, exponent):
    print base ** exponent

the code using it will stop working:

>>> foo(x=2, y=3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: foo() got an unexpected keyword argument 'x'

Positional function arguments should be and stay local, but that is not the case in Python. One could blame the programmer, but language should be able to prevent this problem. Never use keyword argument for a function that doesn't explicitly define one. This is a very nasty way to introduce bugs in otherwise very clean and safe code.

It goes the other way around: a function with keyword arguments can be called as if it was defined with positional arguments:

def hi(name="Nobody"):
    print "Hi {}!".format(name)

Usage:

>>> hi(name="Aleksa")
Hi Aleksa!
>>> hi("Aleksa")
Hi Aleksa!

I'd prefer to see an error here, but it "does the right thing" in a Perl like fashion. Keyword to positional argument replacement is going to make your code less readable, but will not break it as when replacing positional for named arguments.

Python 3 changed things a bit, but didn't fix the problem. Having * between positional and keyword arguments ensures that keyword arguments are properly used:

def foo(x, *, y=None):
    print("X: {}".format(x), end="\n")
    print("Y: {}".format(y), end="\n")
>>> foo(1, 2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: foo() takes 1 positional argument but 2 were given
>>> foo(1, y=2)
X: 1
Y: 2

However, positional arguments can still be replaced with keyword arguments:

>>> foo(x=1, y=2)
X: 1
Y: 2

Every function argument in Python is a hybrid global variable!

March 2, 2013

Python friendly .vimrc

syn on
let python_highlight_builtins=1
au FileType python setlocal expandtab shiftwidth=4 softtabstop=4 colorcolumn=80 ai nu nowrap cul

let python_highlight_builtins=1 enables coloring of reserved and built in functions in Python. It is going to make you aware when a variable name overrides a built in function (e.g. id or type). For details on other specific parameters see Python's wiki page on vim and "Secrets of tabs in vim."

To enable folding add:

set foldmethod=indent

and you can open a fold with zo, close with zc, close all in the document with zM or open with zR.

To emulate PyCharm's variables highlighting under the cursor try this command:

:autocmd CursorMoved * exe printf('match Search /\V\<%s\>/'escape(expand('<cword>')'/\'))

March 1, 2013

Python resources

Useful material for mastering and using Python in the industry.

Books

Web Material

Development Tools

Videos

Useful Libraries

  • python-gflags — Google command line flag is intended to be used in situations where a project wants to mimic the command-line flag handling of a C++ app that uses google-gflags, or for a Python app that, via swig or some other means, is linked with a C++ app that uses google-gflags.

  • gevent for the Working Python Developer — The structure of this tutorial assumes an intermediate level knowledge of Python but not much else. No knowledge of concurrency is expected. The goal is to give you the tools you need to get going with gevent, help you tame your existing concurrency problems and start writing asynchronous applications today.

  • pymox — A mock object framework for Python. Mox is based on EasyMock, a Java mock object framework.

  • Django — A high-level Python Web framework that encourages rapid development and clean, pragmatic design.

  • matplotlib — A 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms.

  • pydot — Allows to easily create both directed and non directed graphs from Python. You'd need to install GraphViz as well.

  • Gnuplot.py — A Python package that interfaces to gnuplot, the popular open-source plotting program. It allows you to use gnuplot from within Python to plot arrays of data from memory, data files, or mathematical functions.

  • SWIG — A software development tool that connects programs written in C and C++ with a variety of high-level programming languages. SWIG is used with different types of target languages including common scripting languages such as Perl, PHP, Python, Tcl and Ruby. Check out this tutorial that shows you how to interface with Python.

  • SciPy — An open-source software for mathematics, science, and engineering. The SciPy library depends on NumPy, which provides convenient and fast N-dimensional array manipulation.

Related Resources

  • HTTP status codes cheat sheet — You will need this for web programming.

  • Graphical vi-vim Cheat Sheet and Tutorial — I found vim to be good for editing Python, mainly because it can do copy-paste with proper indent adjustment (pasting with ]p instead of p).

  • git — Distributed version control system designed to handle everything from small to very large projects with speed and efficiency.