User login

Python hints

Receipts

  • # 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
    year.total_seconds()
  • # 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 script.py
    
  • # iterate with index and item
  • for index, item in enumerate(array):
    
  • # Profiling
  • sudo apt-get install python-profiler
    python -m cProfile script.py
    
  • # Empty operator
  • pass
    
  • # Python debug: attach to running process
  • wget "http://svn.python.org/projects/python/trunk/Misc/gdbinit"
    # or (a local copy on this server):
    wget "http://allexpert.no/sites/default/files/gdbinit."
    cp gdbinit ~/.gdbinit
    gdb -p PID
    pystack
    
    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 setup.py 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 "http://pypi.python.org/pypi/distribute#downloads"
    cd distribute-x.x.xx
    sudo python setup.py install
    #Get easy_install:
    curl -O "http://peak.telecommunity.com/dist/ez_setup.py"
    sudo python ez_setup.py -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>/__init__.py
    # 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: groctimespecification.py Uses other modules, like antlr3.