python

Python binary search-like function to find first number in sorted list greater than a specific value

I'm trying to write a function in Python that finds the first number in a sorted list greater than a specific value that I pass in as an argument. I've found examples online that use simple list comprehensions to achieve this, but for my purposes I need to be performing this operation frequently and on large lists, so a search that runs...

Adding an array in numpy at a specified location

Is there a fast way in numpy to add array A to array B at a specified location? For instance, if B = [ [0, 1, 2], [2, 3, 4], [5, 6, 7] ] and A = [ [2, 2], [2, 2] ] and I want to add A to B starting from point (0, 0) to get C = [ [2, 3, 2], [4, 5, 4], [5, 6, 7], ] Of course I can do that via ext...

Where can I find a full reference of wxpython?

Sorry the question may sound stupid, but I do need one. Right now I'm just adding a wx.TextCtrl in my GUI program, and I want to know what styles can I add (such as style=wx.TE_MULTILINE|wx.TE_PROCESS_ENTER), so I googled and end up reading this page: http://www.wxpython.org/docs/api/wx.TextCtrl-class.html. It must be the official one, b...

Bug import main with arguments in Python.

Hello, In a script I'm trying to import the main module from another script with an argument. I get the error message "NameError: global name 'model' is not defined". If someone sees the mistake, I'd be grateful ! My code : script1 #!/usr/bin/python import sys import getopt import pdb import shelve class Car: """ class repre...

Printing objects and unicode, what's under the hood ? What are the good guidelines?

Hi, I'm struggling with print and unicode conversion. Here is some code executed in the 2.5 windows interpreter. >>> import sys >>> print sys.stdout.encoding cp850 >>> print u"é" é >>> print u"é".encode("cp850") é >>> print u"é".encode("utf8") ├® >>> print u"é".__repr__() u'\xe9' >>> class A(): ... def __unicode__(self): ... r...

Dealing with simultaneous button presses and changing shift states

I am currently working on a (Python2.5) application which handles input from a game controller. We've designated a button as a shift button to change the mapping (inputtype,value->function) of the other buttons on the fly. The mapping also depends on the mode our application is running in. We are running into lots of hairy edge cases (e....

Reading a Turtle/N3 RDF File with Python

I'm trying to encode some botanical data in Turtle format, and read this data from Python using RDFLib. However, I'm having trouble, and I'm not sure if it's because my Turtle is malformed or I'm misusing RDFLib. My test data is: @PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#&gt; . @PREFIX rdfs: <http://www.w3.org/2000/01/rd...

Is this a known DES cipher? What DES cipher is it? DES-CTR?

import Crypto.Cipher.DES import struct def rol32(x, y): ret = ((x<<y)&0xFFFFFFFF)|((x>>(32-y))&0xFFFFFFFF) #print 'rol32', hex(x), hex(y), hex(ret) return ret def sub32(x, y): ret = (x & 0xFFFFFFFF) - (y & 0xFFFFFFFF) if ret < 0: ret += 0x100000000 #print 'sub32', hex(x), hex(y), hex(ret) return ret def mul32...

Pre-configured Python web framework with Authentication, Profiles, etc

I want port some my Python scripts into web apps so that others can use it and I'll use some sort of web framework. I've been playing around with Django lately but it doesn't have the basic user registration, email verification stuff built in and one would probably end up using django-registration. Almost all web applications require you...

Buildout + Nose failing with passed options options

After running a buildout operation on my project, I can run nose with the following command: # ./bin/nosetests ---------------------------------------------------------------------- Ran 0 tests in 0.310s However, when I try to pass options (such as -w for the base directory, I get the following: # ./bin/nosetests -vv --detailed-erro...

add Python path to PATH system variable automatically under Windows

I'm creating one-click python installer (integrated with my application). Is there any way to force Python MSI installer to add python's path to SYSTEM PATH variable? I'm using MSI installer because it is very easy to specify (using command line) how it should interact with the user. ...

How to measure Python import latencies

My django application takes forever to load so I'd like to find a way to measure the import latencies so I can find the offending module and make it load lazily. Is there an obvious way to do this? I was considering tweaking the import statement itself to produce the latencies but I'm not sure exactly how to do that. I guess that it wou...

How to apply __str__ function when printing a list of objects in python

Well this interactive python console snippet will tell everything: >>> class Test: ... def __str__(self): ... return 'asd' ... >>> t = Test() >>> print t asd >>> l = [Test(), Test(), Test()] >>> print l [__main__.Test instance at 0x00CBC1E8, __main__.Test instance at 0x00CBC260, __main__.Test instance at 0x00CBC238] ...

Pass each element of a list to a function that takes multiple arguments in Python?

For example, if I have a=[['a','b','c'],[1,2,3],['d','e','f'],[4,5,6]] How can I get each element of a to be an argument of say, zip without having to type zip(a[0],a[1],a[2],a[3])? ...

regex to eliminate field in bibtex file

I am trying to slim down the bib text files I get from my reference manager because it leaves extra fields that end up getting mangled when I put it into LaTeX. A characteristic entry that I want to clean up is: @Article{Kholmurodov:2001p113, author = {K Kholmurodov and I Puzynin and W Smith and K Yasuoka and T Ebisuzaki}, journal = {...

How do I pickle an object?

Here is the code I have: import pickle alist = ['here', 'there'] c = open('config.pck', 'w') pickle.dump(alist, c) and this is the error I receive: Traceback (most recent call last): File "C:\pickle.py", line 1, in ? import pickle File "C:\pickle.py", line 6, in ? pickle.dump(alist, c) AttributeError: 'module' object has no at...

python: how many similar words in string?

i have ugly strings that looks like this: string1 = 'Fantini, Rauch, C.Straus, Priuli, Bertali: 'Festival Mass at the Imperial Court of Vienna, 1648' (Yorkshire Bach Choir & Baroque Soloists + Baroque Brass of London/Seymour)' string2 = 'Vinci, Leonardo {c.1690-1730}: Arias from Semiramide Riconosciuta, Didone Abbandonata, La Cad...

URL is appended to WSGI script's path, why?

I have a development server set up running Apache 2.2 with mod_wsgi. I have a test project and a webapp in development setup, and they half work. When I attempt to access something other than the project's landing page, Apache appends the rest of the URL onto the path of the WSGI script and won't load the page. In httpd.conf: WSGIScrip...

If you import yourself in Python, why don't you get an infinite loop?

This question is a response to the following SO post: http://stackoverflow.com/questions/3558718/how-do-i-pickle-an-object/3558783#3558783 In that thread, the OP accidentally imports his own module at the top of the same module. Why doesn't this cause an infinite loop? ...

Django ForeignKey which does not require referential integrity?

I'd like to set up a ForeignKey field in a django model which points to another table some of the time. But I want it to be okay to insert an id into this field which refers to an entry in the other table which might not be there. So if the row exists in the other table, I'd like to get all the benefits of the ForeignKey relationship. ...