Articles Tagged python3.x

Python tips & tricks

Base python tips & tricks

Recently i've read the book Learning Python, 5th Edition by Mark Lutz. Here is a list of most interesting insights for me.

  • set generation:

    {x for x in [1,2]}
    set(x for x in [1,2])
    assert set(x for x in [1,2]) == {x for x in [1,2]}
  • dict generation:

    {x:x**2 for x in [1,2]}
    dict((x, x**2) for x in [1,2])
    assert {x:x**2 ...

Posted on 2015-04-30

Python logging for every day

When writing a small python program or script, it is sometimes needed to output debug message or maybe event. It is known, that python has logging module exactly for that purpose. But in my case usually such thing happens: it is lack of time and hands just writes print instead of logging, because i can't remember all those complicated logging settings. But then, if script is launched often or i must provide it ...

Posted on 2013-08-20

Python function with mutable default arguments

Python mutable default arguments

In python default function arguments are created during executing instruction def and not at each function call. If argument value is immutable object (for example string, integer, tuple) it is ok, but if value is mutable, then there can be a trap:

    def foo(l=[]):
        return l

It seems, that every call to foo() will return list ['x']. But:

    >>> foo()
    >>> foo()
    ['x', 'x']
    >>> foo()
    ['x', 'x', 'x']

So, if it is ...

Posted on 2013-03-06

Debug django project with embedded python debugger pdb

I use sublime-text as code editor. It doesn't have a debugger, so to debug django projects i often used

print var_name

and look for output in local development server console. I use this approach today also, but sometimes it is great to run code step by step to see variables at each step.

It can be done with embedded python debugger pdb:

import pdb; pdb.set_trace()

I.e. we put this line in needed ...

Posted on 2012-11-18