Read from File, or STDIN

Tags: , , , ,


I’ve written a command line utility that uses getopt for parsing arguments given on the command line. I would also like to have a filename be an optional argument, such as it is in other utilities …

Combine Pool.map with shared memory Array in Python multiprocessing

Tags: , , ,


I have a very large (read only) array of data that I want to be processed by multiple processes in parallel. I like the Pool.map function and would like to use it to calculate functions on that data …

Tags: file python

Check if object is file-like in Python

Tags: ,


File-like objects are objects in Python that behave like a real file, e.g. have a read() and a write method(), but have a different implementation from file. It is realization of the Duck Typing …

Atomic increment of a counter in django

Tags: , , ,


I’m trying to atomically increment a simple counter in Django. My code looks like this: from models import Counter from django.db import transaction @transaction.commit_on_success def …

Tags: input python

Read two variables in a single line with Python

Tags: ,


I am familiar with the input() function, to read a single variable from user input. Is there a similar easy way to read two variables? I’m looking for the equivalent of: scanf(“%d%d”, &i, &…

Tags:

How to handle exceptions in a list comprehensions?


I have some a list comprehension in Python in which each iteration can throw an exception. For instance, if I have: eggs = (1,3,0,3,2) [1/egg for egg in eggs] I’ll get a ZeroDivisionError …

Python Implementation of the Object Pool Design Pattern

Tags: , ,


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 …

match an alternative url – regular expression django urls

Tags: , , ,


I want a Django URL with just 2 alternatives /module/in/ or /module/out/ I’m using url(r’^(?Pw+[in|out])/$’, ‘by_status’, name=’module_by-status’), But it matches other patterns …

Log output of multiprocessing.Process

Tags: , , ,


Is there a way to log the stdout output from a given Process when using the multiprocessing.Process class in python?

Tags:

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 …