urlib/urlib2: don't. Use requests: http://docs.python-requests.org/en/latest/index.html

A catch-all method:

     def __getattr__(self, name, *args):
         print "CALLED! s" % (name, args)
         def method(*args):
             return ret
         return method

Finding help/doc/info about modules...

  • ipython:
    $ ipython
    In [1]: import wsgiref
    In [2]: wsgiref?
    # shows lots of help
  • python:
    $ python
    >>> help(wsgiref)

Dropping privileges:

  def drop_privileges(user=None, group=None):
    if os.getuid() != 0:
        # do nothing if the user is not root
        return False
    if isinstance(group, basestring):
        group = grp.getgrnam(group)
        if group: group = group.gr_gid
    if isinstance(user, basestring):
        user = pwd.getpwnam(user)
        if user: user = user.pw_uid

    if group is not None:
        os.setgid(group)
    else:
        raise Exception("Unknown group")
    if user is not None:
        os.setuid(user)
    else:
        raise Exception("Unknown user")
    return True

Generic main:

    def main(argv):
        ...

    if __name__ == "__main__":
        main(sys.argv[1:])

Installing external package and running tests:

    $ sudo easy_install nose
    $ nosetests

Starting the debugger at a specific place in the source: just add:

    import pdb
    [...]
    pdb.set_trace()

Starting and running the debugger:

    $ python -m pdb myscript.py <args>
    (Pdb) b myscript.py:32  # set a breakpoint at line 32
    (Pdb) c                 # continue until breakpoint
    (Pdb) s                 # step into function call (follow)
    (Pdb) n                 # next: run line and stop
    (Pdb) l                 # show surrounding source code

Cute:

    # batch_made starts as a boolean, is converted to 'yes'/'no'
    batch_mode = batch_mode and 'yes' or 'no'             

Iterate over a list:

    for l in result[:-1]:
        ...    

Rename file with os-independent paths:

    os.rename(
        os.path.join(root, 'file.conf'),
        os.path.join(root, '..', 'file.conf'),
        )

Remove dir if exists:

    try:
        shutil.rmtree(path)
    except OSError, e:
        if e.errno == errno.ENOENT:
            pass
        else:
            raise

Iterating through ls results:

    for (dirpath, dirnames, filenames) in os.walk(rootpath, onerror=_error):

Parsing arguments

    import optparser

    if __name__ == _main_:
        parser = optparse.OptionParser()
        parser.add_option("-v", "--verbose",
                          action="store_true", dest="verbose", default=False,
                          help="Show verbose output")
    (options, args) = parser.parse_args()

List a module's methods:

    import types
    dir(types)

Variations on importing modules/methods:

    import Sound.Effects.echo
    # call:
    Sound.Effects.echo.echofilter(input, output, delay=0.7, atten=4)

    from Sound.Effects import echo
    # call:
    echo.echofilter(input, output, delay=0.7, atten=4)

    from Sound.Effects.echo import echofilter
    # call:
    echofilter(input, output, delay=0.7, atten=4)

Get host ip by its name:

    #!/usr/bin/env python

    import socket
    from sys import argv

    def gethostbyname(hostname):
        try:
            return socket.gethostbyname(hostname)
        except:
            if hostname.split('.')[:2] == ['localhost','localhost']:
                return '127.0.0.1'
            raise

    if __name__ == _main_:
        print gethostbyname( argv[1] )

Typechecking complex data structures:

    def disable(self, hostnames, hostsite):
        if not isinstance(hostnames, (list, tuple)):
            raise TypeError('hostnames should be a sequence of strings')

Exception handling:

    import sys

    try:
        f = open('myfile.txt')
        s = f.readline()
        i = int(s.strip())
    except IOError as (errno, strerror):
        print "I/O error({0}): {1}".format(errno, strerror)
    except ValueError:
        print "Could not convert data to an integer."
    except:
        print "Unexpected error:", sys.exc_info()[0]
        raise


    for arg in sys.argv[1:]:
        try:
            f = open(arg, 'r')
        except IOError:
            print 'cannot open', arg
        else:
            print arg, 'has', len(f.readlines()), 'lines'
            f.close()

String operations:

    >>> a = 'blablabla s' % (1,2)
    >>> a
    'blablabla 1 blobloblo 2'

Executing a shell command:

    p = subp.Popen([ 'ssh-add', '-L' ], stdout=subp.PIPE)
    (out, _) = p.communicate()

    if not p.returncode:
        return [s.split() for s in out.splitlines()]

Select from postgres database:

    import psycopg2

    class DatabaseConfig:

        def __init__(self):
            conn = psycopg2.connect("dbname=blabla user=foo password=secret")
            self.cursor = conn.cursor()

        def list_binaries(self):
            self.cursor.execute("SELECT name FROM binarytypes")
            while (1):
                row = self.cursor.fetchone ()
                if row == None:
                    break
                print "got  row

Reading files:

    file = open("sample.txt")

    for line in file:
        pass # do something


    # or
    file = open("sample.txt")

    while 1:
        lines = file.readlines(100000)
        if not lines:
            break
        for line in lines:
            pass # do something