python

Fastest way to make a dict a single comma-separated string

I have a list of uids: ['1234','4321','1111'] and I would like to turn this into a single string of: "uid = '1234' OR uid = '4321' OR uid = '1111'" What's the most efficient way to do this? Thanks! ...

Using M2Crypto to save and load X509 certs in pem files

I would expect that if I have a X509 cert as an object in memory, saved it as a pem file, then loaded it back in, I would end up with the same cert I started with. This seems not to be the case however. Let's call the original cert A, and the cert loaded from the pem file B. A.as_text() is identical to B.as_text(), but A.as_pem() differs...

Parse a CSV file using python (to make a decision tree later)

First off, full disclosure: This is going towards a uni assignment, so I don't want to receive code. :). I'm more looking for approaches; I'm very new to python, having read a book but not yet written any code. The entire task is to import the contents of a CSV file, create a decision tree from the contents of the CSV file (using the ...

How to change font size using the Python ImageDraw Library

I am trying to change the font size using python's ImageDraw library. You can do something like this: fontPath = "/usr/share/fonts/dejavu-lgc/DejaVuLGCSansCondensed-Bold.ttf" sans16 = ImageFont.truetype ( fontPath, 16 ) im = Image.new ( "RGB", (200,50), "#ddd" ) draw = ImageDraw.Draw ( im ) draw.text ( (10,10), "Run awayyyy!", ...

Can I get the amount of time for which a key is pressed on a keyboard

Dear all, I am working on a project in which I have to develop bio-passwords based on user's keystroke style. Suppose a user types a password for 20 times, his keystrokes are recorded, like holdtime : time for which a particular key is pressed. digraph time : time it takes to press a different key. suppose a user types a password ...

how to use @ in python.. and the @property and the @classmethod

this is my code: def a(): print 'sss' @a() def b(): print 'aaa' b() and the Traceback is: sss Traceback (most recent call last): File "D:\zjm_code\a.py", line 8, in <module> @a() TypeError: 'NoneType' object is not callable so how to use the '@' thanks updated class a: @property def b(x): print '...

Django ForeignModels

How do I get/set foreign key fields on a model object without touching the database and loading the related object? ...

Add a value to an element in a list of sets

Hello. I'm using python, and I have a list of sets, constructed like this: list = [set([])]*n ...where n is the number of sets I want in the list. I want to add a value to a specific set in the list. Say, the second set. I tried list[1].add(value) But this instead adds the value to each set in the list. This behaviour is pretty non...

Django Multiple Choice Field / Checkbox Select Multiple

I have a Django application and want to display multiple choice checkboxes in a user's profile. They will then be able to select multiple items. This is a simplified version of my models.py: from profiles.choices import SAMPLE_CHOICES class Profile(models.Model): user = models.ForeignKey(User, unique=True, verbose_name_('user')) ...

Call Python From PHP And Get Return Code

Hello everyone, I am calling a python script from PHP. The python program has to return some value according to the arguments passed to it. Here is a sample python program, which will give you a basic idea of what i am doing currently: #!/usr/bin/python import sys #get the arguments passed argList = sys.argv #Not enough arguments. ...

How to make lists automatically instantiate on use in Python as they do in Perl?

In Perl, I can do this: push(@{$h->[x]}, y); Can I simplify the following python codes according to above Perl example? if x not in h: h[x] = [] h[x].append(y) I want to simplify this, because it goes many places in my code, (and I cannot initialize all possible x with []). I do not want to make it a function, because there is no...

Replace each char in a multi-line string except space and \r \n, how?

A multi-line string, e.g. abc 123 456 def wanted result (ordinal + 2): cde 345 678 fgh if I use: text = "abc 123\n456 def" add2=''.join(chr(ord(c)+2) for c in text) print text print add2 the space and \r \n will also be replaced, how can I add the exception of not including space, \r or \n in the 2nd line of code. p.s. it's fol...

Help with filetype association!

I have the actual association part down, but when I open the file that is associated with my Python program, how do I get the filepath of the file opened? I think it is something like sys.argv? But that just returns the path to the python program, not the associated file. ...

Creating a pygtk text field that only accepts number

Hi, Does anybody know how to create a text field using PyGTK that only accepts number. I am using Glade to build my UI. Cheers, ...

Getting facebook OAuth access_token through Python SDK does not seem to be working, any ideas?

According to http://github.com/facebook/python-sdk/blob/master/src/facebook.py , In my canvas application, i can do the following call to get my access_token, which will work because my user has used facebook to login: import facebook myDict = facebook.get_user_from_cookie(cookies, app_id, app_secret) # my access_token is myDict["acce...

SQL-wrappers (activerecord) to recommened for python?

is there an activerecord (any similar SQL-wrapper) for python? which is good for: used in a server-side python script light-weight supports MySQL what I need to do: insert (filename, file size, file md5, the file itself) into (string, int, string, BLOB) columns if the same file (checksum + filename) does not exist in db thx ...

writing an api for python that can be installed using setup.py method

I am new at writing APIs in python, in any language for that matter. I was hoping to get pointers on how i can create an API that can be installed using setup.py method and used in other python projects. Something similar to the twitterapi. I have already created and coded all the methods i want to include in the API. I just need to kno...

How to share memory buffer across sessions in Django?

I want to have one party (or more) sends a stream of data via HTTP request(s). Other parties will be able to receive the same stream of data in almost real-time. The data stream should be accessible across sessions (according to access control list). How can I do this in Django? If possible I would like to avoid database access and use...

How do I stop Python install on Mac OS X from putting things in my home directory?

Hi, I'm trying to install Python from source on my Mac. (OS X 10.6.2, Python-2.6.5.tar.bz2) I've done this before and it was easy, but for some reason, this time after ./configure, and make, the sudo make install puts things some things in my home directory instead of in /usr/local/... where I expect. The .py files are okay, but not ...

what is this code mean in python,

def a(): print 'sss' print getattr(a, "_decorated_function", a).__name__ it print : a thanks updated my code: def a(): w='www' print getattr(a,'w') but it print : Traceback (most recent call last): File "D:\zjm_code\a.py", line 8, in <module> print getattr(a,'w') AttributeError: 'function' object has no attribu...