python

Using a variable outside of function in Python

A really simple question, and I'm sure I knew it but must have forgotten When running this code: x = 0 def run_5(): print "5 minutes later" x += 5 print x, "minutes since start" run_5() print x I get x isn't defined. How can I have x used in the function and effected outside of it? ...

Python "strange" output

class Foo(object): def __init__(self,x): self.x = x self.is_bar = False def __repr__(self): return str(self.x) class Bar(object): def __init__(self,l = []): self.l = l def add(self,o): self.l += [o] def __repr__(self): return str(self.l) def foo_plus_foo(f1,f2): t = Bar() if n...

How to remove repeating non-adjacent string

Given lines that look like the following: Blah \cite[9.1173]{Joyce:1986aa}\autocite[42]{Kenner:1970ab}\autocite[108]{Hall:1960aa} bbb.\n I’d like to remove the second (and any subsequent) occurrence of \autocite, resulting in the following: Blah \autocite[9.1173]{Joyce:1986aa}[42]{Kenner:1970ab}[108]{Hall:1960aa} bbb.\n I’m struggling...

Adding database module

I am new to django I would like to start a project but when i run it i get this error Error loading MySQLdb module How do i add the MYSQL module or any other module for that matter ...

zlib module missing

I have compiled and installed python 2.7 on my ubuntu lucid. But I am unable to install setuptools for python 2.7 because the data decompression module zlib is not present. This is the exact error: Traceback (most recent call last): File "setup.py", line 94, in <module> scripts = scripts, File "/usr/local/lib/python2.7/distut...

Twisted factory protocol instance based callback

Hey, I got a ReconnectingClientFactory and I wonder if I can somehow define protocol-instance-based connectionMade/connectionLost callbacks so that i can use the factory to connect to different hosts ans distinguish between each connection. Thanks in advance. ...

web access by python

hello everyone i'am looking up for way to enter web site "Login" i tried this login_form_seq = [ ('user', 'Lick'), ('pass', 'Shot'), ('submit', 'login')] A=urllib.urlencode(login_form_seq) opener = urllib2.build_opener() try: site = opener.open('http://www.SMS-Example.com/user.php', A).read() site2 = urllib.urlope...

Python : Closing a socket already opened by a precedent python program or dirty trick to close a socket.

here is my dirty little web server : class Serverhttp: def __init__(self): self.GET = re.compile("GET.*?HTTP") self.POST = re.compile("POST.*?HTTP") try : sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_address = ('localhost', 36000) print >>sys.stderr, 'starting up on %s port %s' % se...

Python hangs in futex calls

I have a Python daemon running in production. It employs between 7 and 120 threads. Recently the smallest instance (7 threads) started to show hangs while all other instances never showed this kind of problem. Attaching strace to the python process shows that all threads are calling futex FUTEX_WAIT_PRIVATE, so they are probably trying t...

auto_ptr with swig

Hi, I'm trying to wrap a C++ library which uses auto_ptr. I'm using swig and want to generate python bindings. I'v seen the section of the swig docu on how to use swig with smart pointers here[0]. But I can't get it to work. swig generates code that wants to initialize the auto_ptr using a const reference, but auto_ptr defines the cop...

Why can't I call read() twice on an open file?

For an exercise I'm doing, I'm trying to read the contents of a given file twice using the read() method. Strangely, when I call it the second time, it doesn't seem to return the file content as a string?? Here's the code f = f.open() # get the year match = re.search(r'Popularity in (\d+)', f.read()) if match: print match.group(1) ...

python : get the print output in an exec statement

i've got a little problem here is my code : code = """ i = [0,1,2] for j in i : print j """ result = exec(code) how could i get the things that print outputed ? bref here how can i get in something : 0 1 2 regards and thanks Bussiere ...

sqlalchemy query refresh issue after use mysql load file

i use a sqlalchemy as my orm,i do a mysqlimport cmd through subprocess,and before & after the execution i query the db records,which i use statistics_db method, but the records count results after import from csv didn't increase,i think this is a sqlalchemy problem,how to slove this?,thanks def statistics_db(f): @wraps(f) def wr...

Combination of list in Python

Possible Duplicate: Finding combination in Python without importing itertools Hi everyone, I want following task to be done in Python without importing any modules. My Code consists Two List --------- list1=['aun','2ab','acd','3aa'] list2=['ca3','ba2','dca','aa3'] Function --------- Where it will *Generates 2 items ...

Image classification in python

I'm looking for a method of classifying scanned pages that consist largely of text. Here are the particulars of my problem. I have a large collection of scanned documents and need to detect the presence of certain kinds of pages within these documents. I plan to "burst" the documents into their component pages (each of which is an ind...

Support for SMIL animation in libwebkt on Ubuntu 10.04

I'm just starting to explore the use of libwebkit in GTK+ applications on Ubuntu 10.04. I was curious about whether SVG with SMIL animation would be supported, so I created a small test using python-webkit: import gtk import webkit view = webkit.WebView() sw = gtk.ScrolledWindow() sw.add(view) win = gtk.Window(gtk.WINDOW_TOPLEVE...

Non-binary(hex) characters in string received over TCP with Python

Hi, maybe this is a noob question, but I'm receiving some data over TCP and when I look at the string I get the following: \x00\r\xeb\x00\x00\x00\x00\x01t\x00 What is that \r character, and what does the t in \x01t mean? I've tried Googling, but I'm not sure what to Google for... thanks. ...

How can I implement a color table with VTK / TK in python

hi, I'm developing an application in VTK / TK and I was wondering what's the best way to provide the user with a table which lists items and allow the user to pick the color for each item: item1 | color item2 | color item3 | color thanks ...

Django Monthly/quartarly grouping of DateField() data

I've got a django model which contains, among other things, a DateField() attribute: class Table(): date = models.DateField() value = models.FloatField() I'm writing a view that groups this data by week, month Quarter and year. I've hardcoded a calculation that gets my monthly value simply enough - by adding up all the values ...

Simplest way to calculate the width and height of a jpeg image in Python

How can I calculate the width and height of an image in Python? I'm using the "Image" library already if that makes it easier. Thanks. ...