python

isDigit() for rational numbers?

I am trying to evaluate if the string in one of the textbox of my interface is a number (i.e. not text or anything else). In Python, there is a method called isdigit() that will return True if the string only contains digits (no negative signs or decimal points). Is there another way I could evaluate if my string is a rational number (ex...

Substituting a regex only when it doesn't match another regex (Python)

Long story short, I have two regex patterns. One pattern matches things that I want to replace, and the other pattern matches a special case of those patterns that should not be replace. For a simple example, imagine that the first one is "\{.*\}" and the second one is "\{\{.*\}\}". Then "{this}" should be replaced, but "{{this}}" should...

Function Decorators

I like being able to measure performance of the python functions I code, so very often I do something similar to this... import time def some_function(arg1, arg2, ..., argN, verbose = True) : t = time.clock() # works best in Windows # t = time.time() # apparently works better in Linux # Function code goes here t = tim...

Python regular expression

I would like to intercept string starting with *#* followed by a number between 0 and 7 and ending with: ## so something like *#*0## but I could not find a regex for this ...

How to check if a file is a valid image file?

I am currently using PIL. import Image try: im=Image.open(filename) # do stuff except IOError: # filename not an image file However, while this sufficiently covers most cases, some image files like, xcf, svg and psd are not being detected. Psd files throws an OverflowError exception. Is there someway I could include them ...

Dividing in an if statement

In Python, if I had a range, and I wanted to iterate over it and divide each number by another number, could I do that in a if statement. a = range(20) for i in a: if i / 3 == True: print i ...

How can I read()/write() against a python HTTPConnection?

I've got python code of the form: (o,i) = os.popen2 ("/usr/bin/ssh host executable") ios = IOSource(i,o) Library code then uses this IOSource, doing writes() and read()s against inputstream i and outputstream o. Yes, there is IPC going on here.. Think RPC. I want to do this, but in an HTTP fashion rather than spawning an ssh. ...

Accurate timing of functions in python

I'm programming in python on windows and would like to accurately measure the time it takes for a function to run. I have written a function "time_it" that takes another function, runs it, and returns the time it took to run. def time_it(f, *args): start = time.clock() f(*args) return (time.clock() - start)*1000...

Is it possible to format a date with sqlite3 ?

At start you have a string 'DDMMYYYY HHMMSS' and I want at the end to insert the string in a date field in sqlite3 database. The program is made in python. How can I do that ? ...

How do I generate circuar thumbnails with PIL?

How do I generate circular image thumbnails using PIL? The space outside the circle should be transparent. Snippets would be highly appreciated, thank you in advance. ...

Python Lambda - why?

I'm just beginning Python and ran head first into Lambda- which took me a while to figure out. Is lambda one of those 'interesting' language items that in real life should be forgotten? I'm sure there are some edge cases where it might be needed, but given the obscurity of it, the potential of it being redefined in future releases (my a...

How do I detect when my window is minimized with wxPython?

I am writing a small wxPython utility. I would like to use some event to detect when a user minimizes the application/window. I have looked around but did not find an event like wx.EVT_MINIMIZE that I could bind to. Anyone know of a way that can be used to detect this? ...

Algorithm to Divide a list of numbers into 2 equal sum lists

There is a list of numbers. The list is to be divided into 2 equal sized lists, with a minimal difference in sum. The sums have to be printed. #Example: >>>que = [2,3,10,5,8,9,7,3,5,2] >>>make_teams(que) 27 27 Is there an error in the following code algorithm for some case? How do I optimize and/or pythonize this? def make_teams(q...

Why would one choose Iron Python instead of Boo?

Possible Duplicates: BOO Vs IronPython Boo vs. IronPython Say you want to embed a scripting language into a .NET application. Boo is modelled on Python syntax, but also includes type inference, and just in general seems to be a better, more modern language to embed as a scripting language. Why, then, is there so much fuss ab...

Explain to me what the big deal with tail call optimization is and why Python needs it

So apparently, there's been a big brouhaha over whether or not Python needs tail call optimization. This came to a head when someone shipped Guido a copy of SICP because he didn't "get it." I'm in the same boat as Guido. I understand the concept of tail call optimization. I just can't think of any reason why Python really needs it. ...

Python: How do I write a list to file and then pull it back into memory (dict represented as a string convert to dict) later ?

More specific dupe of 875228—Simple data storing in Python. I have a rather large dict (6 GB) and I need to do some processing on it. I'm trying out several document clustering methods, so I need to have the whole thing in memory at once. I have other functions to run on this data, but the contents will not change. Currently, every t...

Looking for the "Hello World" of ctypes unicode processing (including both Python and C code)

Can someone show me a really simple Python ctypes example involving Unicode strings including the C code? Say, a way to take a Python Unicode string and pass it to a C function which catenates it with itself and returns that to Python, which prints it. ...

Install older versions of Python for testing on Mac OS X

I have Mac OS X 10.5.7 with Python 2.5. I need to test a package I am working on with Python 2.3 for compatibility. I don't want to downgrade my whole system so is there a way to do an install of Python 2.3 that does not change the system python? ...

Google App Engine Python Code: User Service

What does this example program from the Google App Engine documentation mean when it references self? Where can i look up what methods (such as self.response...)? from google.appengine.api import users from google.appengine.ext import webapp from google.appengine.ext.webapp.util import run_wsgi_app class MainPage(webapp.RequestHandler)...

How do you reload a Django model module using the interactive interpreter via "manage.py shell"?

I know how to reload a regular Python module within a regular Python interpreter session. This question documents how to do that pretty well: How do I unload (reload) a Python module? For some reason, I am having trouble doing that within Django's "manage.py shell" interpreter session. To recreate my issue, start the basic Django tutor...