Archive for the ‘Python’ Category

RPy – simple and efficient access to R from Python

July 28, 2009

RPy is an interface that allows you to call R functions and handle R objects in Python.

R language

Using mutable objects as default parameter in Python

July 22, 2009

We just came across a weird Python behaviour (of course only weird if you don’t know why). If you use mutable objects such as lists as the default parameter in a function declaration, you might end up with some unintented behaviour. Everytime the function modifies the object, the default value is in effect modified as well (e.g. appending to the list). This is also explained in the Python documentation:

Default parameter values are evaluated when the function definition is executed. This means that the expression is evaluated once, when the function is defined, and that that same “pre-computed” value is used for each call. This is especially important to understand when a default parameter is a mutable object, such as a list or a dictionary: if the function modifies the object (e.g. by appending an item to a list), the default value is in effect modified. This is generally not what was intended. A way around this is to use None as the default, and explicitly test for it in the body of the function, …

Find the whole thing here.

How do I read a huge file line by line in Python?

February 4, 2009

This is taken from here and was written by rupe.

In Python, the most common way to read lines from a file is to do the following:

for line in open('myfile','r').readlines():
do_something(line)

When this is done, however, the readlines() function loads the entire file into memory as it runs. A better approach for large files is to use the fileinput module, as follows:

import fileinput
for line in fileinput.input(['myfile']):
do_something(line)

the fileinput.input() call reads lines sequentially, but doesn’t keep them in memory after they’ve been read.

Statistics with Python

April 11, 2008

Python for scientific use. Part I: Data Visualization

Python for scientific use, Part II: Data analysis

Do you want to put a black/white/both frame around your picture?

April 7, 2008

I was tired of looking at complicated (ehm, not free) applications to do this, so I wrote my own 🙂

Enjoy


#!/usr/bin/env python

from PIL import Image
import sys

if len(sys.argv)!=3 and len(sys.argv)!=4:
    print "USAGE: <command> <filename> <black border percentage> <white border percentage>"
    sys.exit(0)
    
filename=sys.argv[1]
percBlack=float(sys.argv[2])

if len(sys.argv)==3:
    percWhite=0
else:
    percWhite=float(sys.argv[3])
    

im = Image.open(filename)
blackWidth=int(im.size[0]*percBlack)
whiteWidth=int(im.size[0]*percWhite)
white = Image.new('RGB',(im.size[0]+whiteWidth,im.size[1]+whiteWidth),'white')
black = Image.new('RGB',(white.size[0]+blackWidth,white.size[1]+blackWidth),'black')
white.paste(im,(int(whiteWidth/2),int(whiteWidth/2)))
black.paste(white,(int(blackWidth/2),int(blackWidth/2)))
black.save('framed-'+filename)

A simple non-recursive directory walker in Python

January 19, 2008

This is taken from here:

A simple directory walker, without the burden of creating a callback for os.path.walk().

This is also usefull for incremental directory walking (where you want to scan remaining directories in subsequent calls).

import os, os.path

startDir = "/"

directories = [startDir]
while len(directories)>0:
    directory = directories.pop()
    for name in os.listdir(directory):
        fullpath = os.path.join(directory,name)
        if os.path.isfile(fullpath):
            print fullpath                # That's a file. Do something with it.
        elif os.path.isdir(fullpath):
            directories.append(fullpath)  # It's a directory, store it.

Can I restart a thread in Python?

December 20, 2007

Have you ever got something like: 

File “threading.py”, line 410, in start

    assert not self.__started, “thread already started”

AssertionError: thread already started
?
 
It means that Python is complaining because you are trying to call the start() method on a thread more than once. This is not possible, so each time you want a thread object to start, you have to recreate the object. 
 
From the Python documentation:
start()
Start the thread’s activity.
This must be called at most once per thread object. It arranges for the object’s run() method to be invoked in a separate thread of control. 

python: copy by reference, copy by value, shit it depends on what you copy!

December 19, 2007
class MyClass:                                                                  
    def __init__(self):                                                         
        self.__data = []                                                        
        self.doSomething()                                                      
    def doSomething(self):                                                      
        #[self.__buckets.append([1,2,i] ) for i in range (0,4)]                 
        [self.__data.append(i ) for i in range (0,4)]                           
    def getData(self):                                                          
        return self.__data                                                      
    def modBucket(self):                                                        
        h = self.__data[2]                                                      
        h = 5                                                                   
    def modBucket2(self):                                                       
        h = self.__data                                                         
        h[2] = 5                                                                

c = MyClass()                                                                   
print "init:", c.getData()                                                      
c.modBucket()                                                                   
print "mod1:", c.getData()                                                      
c.modBucket2()                                                                  
print "mod2:", c.getData()                                                      
print "-------------------------"                                                                               

The output of running this code is:

init: [0, 1, 2, 3]
mod1: [0, 1, 2, 3]
mod2: [0, 1, 5, 3]
------------------------ 

Notice the problem, if you ask for the whole object, you get if by reference, if you ask for a part of it, you get a copy!!!!
Haven’t yet found the logic for this, any directions welcome.

Check if an entry is a file or directory in Python

December 3, 2007

To check whether an entry is a file or a directory in python, use the os.path.isdir() method. To read some file attributes, use the os.stat() method. The following code will read all entries in the current directory and then print out whether the entry is a file or a directory together with the time the file/dir was created.


import os, time
dirList = os.listdir("./")
for d in dirList:
    if os.path.isdir(d) == True:
        stat = os.stat(d)
        created = os.stat(d).st_mtime
        asciiTime = time.asctime( time.gmtime( created ) )
        print d, "is a dir  (created", asciiTime, ")"
    else:
        stat = os.stat(d)
        created = os.stat(d).st_mtime
        asciiTime = time.asctime( time.gmtime( created ) )
        print d, "is a file (created", asciiTime, ")"

Threading in Python

November 28, 2007

Just a simple example on Python threading:

 

#!/usr/bin/env python

import time
from threading import Thread

class MyThread(Thread):
    def __init__(self):
        Thread.__init__(self)

    def run(self):
        while True:
            print 'hello Thready World!!!'
            time.sleep(2)

t = MyThread()

t.start()