RPy is an interface that allows you to call R functions and handle R objects in Python.
Archive for the ‘Python’ Category
RPy – simple and efficient access to R from Python
July 28, 2009How do I read a huge file line by line in Python?
February 4, 2009This 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, 2008Do you want to put a black/white/both frame around your picture?
April 7, 2008I 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, 2008This 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, 2007Have you ever got something like:
File “threading.py”, line 410, in start
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, 2007To 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, 2007Just 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()