python

How to read formatted input in python?

I want to read from stdin five numbers entered as follows: 3, 4, 5, 1, 8 into seperate variables a,b,c,d & e. How do I do this in python? I tried this: import string a=input() b=a.split(', ') for two integers, but it does not work. I get: Traceback (most recent call last): File "C:\Users\Desktop\comb.py", line 3, in <module> ...

What is the simplest way to implement a remote FIFO queue as a Python GAE application?

What is the simplest way to implement a remote FIFO queue as a Python GAE application and then push/pull name-value pair dictionaries to and from it? For example, when an http get is made to the GAE application, the GAE app would return the oldest collection of name-value pairs that were posted to the app which have not been previously...

Are Python extensions produced by Cython/Pyrex threadsafe?

If not, is there a way I can guarantee thread safety by programming a certain way? To clarify, when talking about "threadsafe,' I mean Python threads, not OS-level threads. ...

looping over all member variables of a class in python

How do you get a list of all variables in a class thats iteratable? Kind of like locals(), but for a class class Example(object): bool143 = True bool2 = True blah = False foo = True foobar2000 = False def as_list(self) ret = [] for field in XXX: if getattr(self, field): re...

Initialize a string variable in Python: "" or None?

Suppose I have a class with a string instance attribute. Should I initialize this attribute with "" value, or None is OK either? def __init__(self, mystr = "") self.mystr = mystr or def __init__(self, mystr = None) self.mystr = mystr Thanks Edit: What I thought is that if I use "" as an initial value, I "declare" a variable ...

Django: show list of many to many items in the admin interface

Hi, This might be a simple question, but i can't seem to grasp it. I have two simple models in models.py: Service and Host. Host.services has a m2m relationship with Service. In other words, a host has several services and one service can reside on multiple hosts; a basic m2m. models.py class Service(models.Model): servicena...

Python: display the time in a different time zone

Hi, Is there an elegant way to display the current time in another time zone? I would like to have something with the general spirit of: cur=<Get the current time, perhaps datetime.datetime.now()> print "Local time ", cur print "Pacific time ", <something like cur.tz('PST')> print "Israeli time ", <something like cur.tz('IST')> An...

Problems with Snow Leopard, Django & PIL

Hi I am having some trouble getting Django & PIL work properly since upgrading to Snow Leopard. I have installed freetype, libjpeg and then PIL, which tells me: --- TKINTER support ok --- JPEG support ok --- ZLIB (PNG/ZIP) support ok --- FREETYPE2 support ok but when I try to upload a jpeg through the django admin interface I get: ...

using cookies with twisted.web.client

I'm trying to make a web client application using twisted but having some trouble with cookies. Does anyone have an example I can look at? ...

Downloading file using IE from python.

I'm trying to download file with Python using IE: from win32com.client import DispatchWithEvents class EventHandler(object): def OnDownloadBegin(self): pass ie = DispatchWithEvents("InternetExplorer.Application", EventHandler) ie.Visible = 0 ie.Navigate('http://website/file.xml') After this, I'm getting a window asking...

The Assignment Problem, a numpy function?

Since an assignment problem can be posed in the form of a single matrix, I am wandering if numpy has a function to solve such a matrix. So far I have found none. Maybe one of you guys know if numpy/scipy has an assignment-problem-solve function? Edit: In the meanwhile I have found a python (not numpy/scipy) implementation at http://www....

Is it possible to intercept attribute getting/setting in ActionScript 3?

When developing in ActionScript 3, I often find myself looking for a way to achieve something similar to what is offered by python's __getattr__ / __setattr__ magic methods i.e. to be able to intercept attribute lookup on an instance, and do something custom. Is there some acceptable way to achieve this in ActionScript 3? In AS3 attrib...

Django : import problem with python-twitter module

Hi, When I try to import python-twitter module in my app, django tries to import django.templatetags.twitter instead of python-twitter module (in /usr/lib/python2.5/site-packages/twitter.py), but I don't know why. :s For example: myproject/ myapp/ templatetags/ file.py In file.py: import twitter # this impor...

Store django forms.MultipleChoiceField in Models directly

Say I have choices defined as follows: choices = (('1','a'), ('2','b'), ('3','c')) And a form that renders and inputs these values in a MultipleChoiceField, class Form1(forms.Form): field = forms.MultipleChoiceField(choices=choices) What is the right way to store field in a model. I can of course loop thr...

Python datatype suitable for my cache

I'm searching for the a datatype for a cache, basically I need the functionality of a dict, i.e. random access based on a key, which has a limited number of entries so that when the limit is reached the oldest item gets automatically removed. Furthermore I need to be able to store it via shelve or pickle and rely on Python 2.4. Should I ...

Python: convert free text to date

Hi, Assuming the text is typed at the same time in the same (Israeli) timezone, The following free text lines are equivalent: Wed Sep 9 16:26:57 IDT 2009 2009-09-09 16:26:57 16:26:57 September 9th, 16:26:57 Is there a python module that would convert all these text-dates to an (identical) datetime.datetime instance? I would like to...

Django and monkey patching issue

I have recently started experimenting with Django for some web applications in my spare time. While designing the data model for one, I came across the dilemma of using inheritance to define a user of the website or using a technique known as monkey patching with the User class already supplied by the framework. I tried to add a field b...

Import failed when the module is already in the sys.path

Hi all: It's weird to me that the import fails even when it's in the sys.path. today, I set up a google app engine django environment on ubuntu in my lab's pc. And it works fine when I checked out the code and ran it in windows(same pc in the lab). But when I went to the dorm, and checked out the code and start to run, it failed weird...

Endianness of integers in Python

I'm working on a program where I store some data in an integer and process it bitwise. For example, I might receive the number 48, which I will process bit-by-bit. In general the endianness of integers depends on the machine representation of integers, but does Python do anything to guarantee that the ints will always be little-endian?...

Multi-line Pattern and tag search

Hello I'm trying to make a pattern for tags, but the sub method just replace the first char and 3 at the end of the line, im trying to replace all tags on the line and with multiline p=re.compile('<img=([^}]*)>([^}]*)</img>', re.S) p.sub(r'[img=\1]\2[/img]','<img="test">dsad</img> <img="test2">dsad2</img>') output: '**[**img="test">dsa...