I have some a list comprehension in Python in which each iteration can throw an exception. For instance, if I have: I’ll get a ZeroDivisionError exception in the 3rd element. How can I handle this exception and continue execution of the list comprehension? The only way I can think of is to use a helper function: But this looks a
Python Implementation of the Object Pool Design Pattern
I need an Object Pool, and rather than implement it myself, I thought I would look around for a ready-made and tested Python library. What I found was plenty of other people looking, but not getting many straight answers, so I have brought it over here to Stack Overflow. In my case, I have a large number of threads (using
How to check dimensions of all images in a directory using python?
I need to check the dimensions of images in a directory. Currently it has ~700 images. I just need to check the sizes, and if the size does not match a given dimension, it will be moved to a different folder. How do I get started? Answer You can use the Python Imaging Library (aka PIL) to read the image
match an alternative url – regular expression django urls
I want a Django URL with just 2 alternatives /module/in/ or /module/out/ I’m using But it matches other patterns like /module/i/, /module/n/ and /module/ou/. Any hint is appreciated :) Answer Try r’^(?P<status>in|out)/$’ You need to remove w+, which matches one or more alphanumeric characters or underscores. The regular expression suggested in bstpierre’s answer, ‘^(?P<status>w+(in|out))/$’ will match helloin, good_byeout and so
Log output of multiprocessing.Process
Is there a way to log the stdout output from a given Process when using the multiprocessing.Process class in python? Answer The easiest way might be to just override sys.stdout. Slightly modifying an example from the multiprocessing manual: And running it: $ ls m.py $ python m.py $ ls 27493.out 27494.out m.py $ cat 27493.out function f module name: __main__
How can I implement decrease-key functionality in Python’s heapq?
I know it is possible to realize decrease-key functionality in O(log n) but I don’t know how? Answer To implement “decrease-key” effectively, you’d need to access the functionality “decrement this element AND swap this element with a child until heap condition is restore”. In heapq.py, that’s called _siftdown (and similarly _siftup for INcrementing). So the good news is that the
Format floats with standard json module
I am using the standard json module in python 2.6 to serialize a list of floats. However, I’m getting results like this: I want the floats to be formated with only two decimal digits. The output should look like this: I have tried defining my own JSON Encoder class: This works for a sole float object: But fails for nested
fcntl substitute on Windows
I received a Python project (which happens to be a Django project, if that matters,) that uses the fcntl module from the standard library, which seems to be available only on Linux. When I try to run it on my Windows machine, it stops with an ImportError, because this module does not exist here. Is there any way for me
Is there a function to determine which quarter of the year a date is in?
Sure I could write this myself, but before I go reinventing the wheel is there a function that already does this? Answer Given an instance x of datetime.date, (x.month-1)//3 will give you the quarter (0 for first quarter, 1 for second quarter, etc — add 1 if you need to count from 1 instead;-). Originally two answers, multiply upvoted and
How do I determine if my python shell is executing in 32bit or 64bit?
How can I tell what mode the shell is in, from within the shell? I’ve tried looking at the platform module, but it seems only to tell you about “the bit architecture and the linkage format used for the executable”. My binary is compiled as 64bit (I’m running on OS X 10.6), so it seems to always report 64bit even