python

key=operator.attrgetter sort order?

in my django view, if i import operator, and use the following code: multitags = sorted(multitags, key=operator.attrgetter('date_added')) is there an easy way to reverse the order – such that i get the dates in descending order (today at top; last week underneath)? ...

How to produce the i-th combination/permutation without iterating

Given any iterable, for example: "ABCDEF" Treating it almost like a numeral system as such: A B C D E F AA AB AC AD AE AF BA BB BC .... FF AAA AAB .... How would I go about finding the ith member in this list? Efficiently, not by counting up through all of them. I want to find the billionth (for example) member in this list. I'm tr...

Django: reverse function fails with an exception

I'm following the Django tutorial and got stuck with an error at part 4 of the tutorial. I got to the part where I'm writing the vote view, which uses reverse to redirect to another view. For some reason, reverse fails with the following exception: import() argument 1 must be string, not instancemethod Currently my project's urls.p...

Creating a decorator in a class with access to the (current) class itself

Currently, I'm doing it in this fashion: class Spam(object): decorated = None @classmethod def decorate(cls, funct): if cls.decorated is None: cls.decorated = [] cls.decorated.append(funct) return funct class Eggs(Spam): pass @Eggs.decorate def foo(): print "spam and eggs" ...

virtualenv with all Python libraries

I need to get Python code, which relies on Python 2.6, running on a machine with only Python 2.3 (I have no root access). This is a typical scenario for virtualenv. The only problem is that I cannot convince it to copy all libraries to the new environment as well. virtualenv --no-site-packages my_py26 does not do what I need. The lib...

Get POST data from a complex Django form?

I have a Django form that uses a different number of fields based on the year/month. So I create the fields in the form like this: for entry in entry_list: self.fields[entry] = forms.DecimalField([stuffhere]) but now I don't know how to get the submitted data from the form. Normally I would do something like: form.cleaned_data["...

Encryption with Python

I'm making an encryption function in Python and I want to encrypt a random number using a public key. I wish to know that if I use Crypto package (Crypto.publicKey.pubkey) than how can I use the method like... def encrypt(self,plaintext,k) Here the k is itself a random number, is this mean the key. Can somebody help me with somewhat ...

Jython or JRuby?

It's a high level conceptual question. I have two separate code bases that serve the same purpose, one built in Python and the other in Ruby. I need to develop something that will run on JVM. So I have two choices: convert the Python code to Jython or convert the Ruby to JRuby. Since I don't know any of them, I was wondering if anyone ca...

which exception catches xxxx error in python

Thanks! the question is solved in all the answers given. Original question follows: given a traceback error log, i don't always know how to catch a particular exception. my question is in general, how do i determine which "except" clause to write in order to handle a certain exception. example 1: File "c:\programs\python\lib\httpl...

How to create arrayType for WSDL in Python (using suds)?

Environment: Python v2.6.2 suds v0.3.7 The WSDL (server) I work with, have the following schema sub-sections (I tried to write it clearly using plain text) - [ sub-section #1 ] searchRequest: (searchRequest){ userIdentification = (userIdentification){ username = "" password = "" } itineraryArr = (i...

Following a javascript postback using COM + IE automation to save text file

Hi, I want to automate the archiving of the data on this page http://energywatch.natgrid.co.uk/EDP-PublicUI/Public/InstantaneousFlowsIntoNTS.aspx, and upload into a database. I have been using python and win32com (behind a corporate proxy, so no direct net access, hence I am using IE to do so) on other pages to do this. My question is...

How to call Apple Authorization API from PyQt4

I am currently working on a PyQt4 app which requires administrator privileges to access some files. I would like it to follow Apple guidelines and open the Authenticate Dialog to prompt the user to input their password in order to perform administrator tasks. I have already looked into MacPython Authorization package, but it seems a bit...

most efficient data structure for a read-only list of strings (about 100,000) with fast prefix search

I'm writing an application that needs to read a list of strings from a file, save them in a data structure, and then look up those strings by prefixes. The list of strings is simply a list of words in a given language. For example, if the search function gets "stup" as a parameter, it should return ["stupid", "stupidity", "stupor"...]. I...

Get MD5 hash of a files without open it in Python

I have used hashlib (which replaces md5 in Python 2.6/3.0) and it worked fine if I opened a file and put its content in hashlib.md5 function. The problem is with very big files that their sizes could exceed RAM size. How to get a MD5 hash of a file without open it? ...

Form validation in django

I just started to use django. I came across forms and I need to know which one is the better way to validate a forms. Would it be using django forms or should we use javascript or some client side scripting language to do it? ...

server side marker clustering in django

hi i am creating a mashup in django and google maps and i am wondering if there is a way of clustering markers on the server side using django/python. ...

Are Generators Threadsafe?

I have a multithreaded program where I create a generator function and then pass it to new threads. I want it to be shared/global in nature so each thread can get the next value from the generator. Is it safe to use a generator like this, or will I run into problems/conditions accessing the shared generator from multiple threads? If...

Eliminating multiple inheritance

I have the following problem and I'm wondering if there's a nice way to model these objects without using multiple inheritance. If it makes any difference, I am using Python. Students need contact information plus student information. Adults need contact information plus billing information. Students can be adult students, in which cas...

Chain FormEncode Validators

Problem: I have a form in TurboGears 2 that has a text field for a list of e-mails. Is there a simple way using ToscaWidgets or FormEncode to chain form validators for Set and Email or will I have to write my own validator for this? ...

Replacing Functionality of PIL (ImageDraw) in Google App Engine (GAE)

So, Google App Engine doesn't look like it's going to include the Python Imaging Library anytime soon. There is an images api, but it's paltry, and inadequate for what I need. I'm wondering what Python only (no C-extensions) there are that can replace the Image.paste and the ImageDraw modules. I don't want to write them myself, but ...