Autarchy of the Private Cave

Tiny bits of bioinformatics, [web-]programming etc

    • Archives

    • Recent comments

    Archive for the 'Python' Category

    Good online Python book with code examples

    1st March 2010

    Building Skills in Python: A Programmer’s Introduction to Python by Steven F. Lott (© 2002, 2005, 2007, 2008 Steven F. Lott).

    Share

    Posted in Links, Programming, Python | No Comments »

    Python: passing by value vs passing by reference

    11th February 2008

    Note: this a collection of scraps, describing how values (de)referencing works in Python, and describing when your variable is either a value or a reference. Primary source of knowledge for this post (also here).

    Python passes references-to-objects by value (like Java), and everything in Python is an object. This sounds simple, but then you will notice that some data types seem to exhibit pass-by-value characteristics, while others seem to act like pass-by-reference… what’s the deal?

    It is important to understand mutable and immutable objects. Some objects, like strings, tuples, and numbers, are immutable. Altering them inside a function/method will create a new instance and the original instance outside the function/method is not changed. Other objects, like lists and dictionaries are mutable, which means you can change the object in-place. Therefore, altering an object inside a function/method will also change the original object outside.

    Immutable variables – such as integers [strings, numerics and tuples are immutables] – are passed by value. That is, if your function accepts some integer argument, you are safe assuming that your function won’t be able to modify your integer. Mutable variables – such as dictionaries and lists – are passed by reference, and so if your function accepts mutable argument, it may modify the contents of that mutable variable outside the scope of the function.

    When doing :
    s = “Hello ”
    s += “World”
    … you are not modifying the string object bound to s, but creating a new string object and binding it to s.

    If using object’s methods within a called function, variable is considered “passed by reference” – it is modified out of the function’s scope. If using assignment on a mutable object, it is created a-new within the function, and global value isn’t modified.

    When you call a function with an arg, a “local variable” is created, which references the object passed as the argument. (well… an entry with the formal parameter name as key and a reference to the object passed in is created in the ‘local’ dict).

    So, rebinding this local symbol does not impact the binding in the caller’s namespace – because the symbol lives in another namespace.

    *But* – and if the object referenced is mutable of course – modifying the object in the function… well, just modifies the object, because it’s the *same* object that is bound to (‘referenced by’, if you prefer) both symbols (the one in the caller’s namespace and the one in the function’s namespace). So yes, the object *is* modified when the function returns.

    Share

    Posted in Links, Programming, Python | 3 Comments »

    Useful Python looping techniques

    26th September 2007

    These are all excerpts from the Python documentation.

    To synchronously and simultaneously loop over two sequences:

    1. questions = ['name', 'quest', 'favourite colour']
    2. answers = ['Lancelot', 'the holy grail', 'blue']
    3.  
    4. for q, a in zip(questions, answers):
    5.     print 'What is your %s?  It is %s.' % (q, a)

    To loop over a sequence with both key and value:
    Read the rest of this entry »

    Share

    Posted in Programming, Python | No Comments »

    How to sort Python dict (dictionary)

    26th September 2007

    Sample script (copypasted from Well House Consultants training course):
    click the PLAIN TEXT header for copy-pasteable version

    1. #!/usr/local/bin/python
    2.  
    3. author = {"php":"Rasmus Lerdorf",\
    4.     "perl":"Larry Wall",\
    5.     "tcl":"John Ousterhout",\
    6.     "awk":"Brian Kernighan",\
    7.     "java":"James Gosling",\
    8.     "parrot":"Simon Cozens",\
    9.     "python":"Guido van Rossum"}
    10.  
    11. langs = author.keys()
    12. langs.sort()
    13.  
    14. for language in langs:
    15.     print language,"is the child of",author[language]

    You can also define the Python ksort() function similar to that found in PHP:
    Read the rest of this entry »

    Share

    Posted in Programming, Python | 1 Comment »

    MySQL – Python: good MySQLdb tutorial (examples)

    7th September 2007

    Andy Dustman (used to blog at dustman.net) gave a presentation on Python and MySQL at the MySQL Users Conference 2005, Santa Clara, CA. The presentation is an excellent collection of examples for those who use the MySQLdb Python module.
    Read the rest of this entry »

    Share

    Posted in Links, Notepad, Programming, Python | No Comments »

    Date and time in Python scripts

    7th September 2007

    Here’s an excellent description of various methods to handle date and time in Python scripts.

    Share

    Posted in Links, Notepad, Programming, Python | No Comments »

    Elegantly converting Python list into another list with changed items

    30th August 2007

    I just had a small problem with my Python script:

    1. I have a list of tuples, called records_cache; each tuple looks like this:
    (note: for copy-pasting, click the PLAIN TEXT box headers)

    1. (ensembl_transcript_id, ensembl_gene_id, chrom_start, chrom_end, utr_start, utr_end, sequence, strand)

    2. I need to INSERT only some of those values into MySQL table, using the MySQLdb executemany() method. The values I need are utr_start, utr_end, ensembl_gene_id (in exactly this order).

    To do this, I need to create another list of tuples, but with shorter 3-item tuples. Let us call the new list genes_update.

    Here is the elegant, though probably not the most efficient, solution:
    Read the rest of this entry »

    Share

    Posted in Programming, Python | 1 Comment »