User login

Python hints


  • # Check the type of an object
  • isinstance(variable, basestring) # string object
    isinstance(variable, (list, tuple)) # any of the list types
    isinstance(variable, dict) # dictionary
    print str(type(variable))
  • # Convert timestamp to date:
  • date= datetime.datetime.fromtimestamp(timestamp)
  • # Time span (data difference):
  • from dateutil import relativedelta
    ddiff= relativedelta.relativedelta (date2, date1)
    print "%d years, %d months, %d days, %d hours, %d minutes and %d seconds" % \
              (rd.years, rd.months, rd.days, rd.hours, rd.minutes, rd.seconds)
    year = datetime.timedelta(days=365)
    another_year = datetime.timedelta(weeks=40, days=84, \
                             hours=23, minutes=50, seconds=600)  # adds up to 365 days
  • # Sort an array of dictionaries based on a given key
  • some_list= sorted(some_list, key=lambda k: k['<some key>'])
    some_list= sorted(list_to_be_sorted, key=operator.itemgetter('<some key>'))
  • # Check python grammar (aka: pre-compile)
  • pyflakes
  • # iterate with index and item
  • for index, item in enumerate(array):
  • # Profiling
  • sudo apt-get install python-profiler
    python -m cProfile
  • # Empty operator
  • pass
  • # Python debug: attach to running process
  • wget ""
    # or (a local copy on this server):
    wget ""
    cp gdbinit ~/.gdbinit
    gdb -p PID
    If "pystack" does not work, recompile python with (see also Python from source):
    make "CFLAGS=-g -fno-inline -fno-strict-aliasing"
  • # Python from source
  • sudo apt-get remove python # if python is already installed as dpkg
    sudo apt-get install build-essential libncursesw5-dev \
       libreadline5-dev libssl-dev libgdbm-dev libbz2-dev libc6-dev \
       libsqlite3-dev tk-dev zlib1g-dev
    Get python source from here If on Ubuntu 11.XX, correct by adding '/usr/lib/x86_64-linux-gnu' (or '/usr/lib/i386-linux-gnu' for 32bit) to:
            lib_dirs = self.compiler.library_dirs + [
                      '/lib64', '/usr/lib64',
                      '/lib', '/usr/lib',
    # so that it becomes like:
            lib_dirs = self.compiler.library_dirs + [
                      '/lib64', '/usr/lib64',
                      '/lib', '/usr/lib', '/usr/lib/x86_64-linux-gnu',
    # Then:
    	./configure --prefix=/usr/
    	make "CFLAGS=-g -fno-inline -fno-strict-aliasing"; sudo make install
    Add packages:
    wget ""
    cd distribute-x.x.xx
    sudo python install
    #Get easy_install:
    curl -O ""
    sudo python -U setuptools
    #Then, install packages:
    sudo easy_install -U 
  • # Parse json in command line:
  • curl -X GET "..." | python -mjson.tool
  • # Fix import problem ("cannot import name <name>"):
  • sudo vi /usr/local/lib/python2.7/dist-packages/<package>.egg/<package>/
    # Add <name> to appropriate import statement, for example:
    from <package>.<class> import Database, Document, Server, <name>
  • # Python library path:
  • PYTHONPATH=<path> python <script>.py <options>
  • # Merge dictionaries:
  • z = dict(x.items() + y.items())
    # y overrides same fields of x
  • # Pretty-print json from python:
  • json.dumps(object, indent=4)

    Useful tools:

  • pyflakes - python grammar checker. For example unused imports...
  • pylint - python code static analyzer
  • Cron scheduler in Google Apps - uses a human readable format, called Groc schedule. A schedule looks like: '1st,2nd monday 9:00', or 'every 5 minutes from 10:00 to 14:00'. See the following module: Uses other modules, like antlr3.