python

Fast way to get N Min or Max elements from a list in Python

I currently have a long list which is being sorted using a lambda function f. I then choose a random element from the first five elements. Something like: f = lambda x: some_function_of(x, local_variable) my_list.sort(key=f) foo = choice(my_list[:4]) This is a bottleneck in my program, according to the profiler. How can I speed things...

Complete django DB reset

How do I completely reset my Django (1.2 alpha) DB (dropping all tables, rather than just clearing them)? manage.py flush does too little (won't work if there are schema changes) and manage.py reset requires me to specify all apps (and appears to take a format that is different from just " ".join(INSTALLED_APPS)). I can obviously achiev...

SQLAlchemy.declarative and deferred column loading

Hi everybody, is it possible to specify some columns in the SQLAlchemy to be deferred-loading? I am using the sqlalchemy.ext.declarative module to define my mapping, example: from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() class SomeClass(Base): __tablename__ = 'some_table' id = Column(Integer...

Automate conversion of Sybase .ADT files to SQL.

I am working with some data I obtained that is read with a program using an embedded Advantage Database Server. The program was not written by me and does not have all of the functionality that I need. I would like to convert this data to a different format so that I can work with it more freely, such as MySQL. I know that Sybase pr...

Python and urllib

I'm trying to download a zip file ("tl_2008_01001_edges.zip") from an ftp census site using urllib. What form is the zip file in when I get it and how do I save it? I'm fairly new to Python and don't understand how urllib works. This is my attempt: import urllib, sys zip_file = urllib.urlretrieve("ftp://ftp2.census.gov/geo/tiger/TIGE...

Disable class instance methods

How can I quickly disable all methods in a class instance based on a condition? My naive solution is to override using the __getattr__ but this is not called when the function name exists already. class my(): def method1(self): print 'method1' def method2(self): print 'method2' def __getattr__(self, name): ...

sort lines in text file, but only use the first N characters

I have a text file with lines like this: 2010-02-18 11:46:46.1287 bla 2010-02-18 11:46:46.1333 foo 2010-02-18 11:46:46.1333 bar 2010-02-18 11:46:46.1467 bla A simple sort would swap lines 2 and 3 (bar comes before foo), but I would like to keep lines (that have the same date/time) in their original order. How can I do this in Python?...

Sudo equivalent for Django user profiles

Is it possible to implement an equivalent of sudo for Django profiles ? I'm using the basic authentication system django.contrib.auth. Usecase: Sometimes, users report bugs which are only reproductible in their profile, so, each time, I change their password, log in, fix the bug and replaces the password with the original one. I tried...

Beginner extending C with Python (specifically Numpy)

I am working on a real time audio processing dynamically linked library where I have a 2 dimensional C array of floating point data which represents the audio buffer. One dimension is time (samples) and the other is channel. I would like to pass this to a python script as a numpy array for the DSP processing and then I would like to pass...

disabling "joining" when process shuts down

Is there a way to stop the multiprocessing Python module from trying to call & wait on join() on child processes of a parent process shutting down? 2010-02-18 10:58:34,750 INFO calling join() for process procRx1 I want the process to which I sent a SIGTERM to exit as quickly as possible (i.e. "fail fast") instead of waiting for several...

How to set pythonpath (python2.6) for tkinter on Ubuntu 9.04 (to use nltk)?

I'd like to use the nltk toolkit on my machine which runs Ubuntu 9.04. I installed python 2.6.4 and several additional packages (numpy, scipy, matplotlib and of course nltk). I can import nltk, but calling a few methods gives various error masseges, all contain "please install Tkinter library". Googling around I discovered from http://wi...

python more trouble importing modules

I asked a similar question yesterday, but have acquired a really odd problem since then. With this directory structure: app/ models/ __init__.py user.py other.py pages/ __init__.py pages.py The models/__init__.py file has this line: __all__ = ['user', 'other'] and the pages/__init__....

Accessing a Variable from Within a Doubly Nested Function in Python

The following code: x = 0 print "Initialization: ", x def f1(): x = 1 print "In f1 before f2:", x def f2(): global x x = 2 print "In f2: ", x f2() print "In f1 after f2: ", x f1() print "Final: ", x prints: Initialization: 0 In f1 before f2: 1 In f2: 2 In f1 aft...

Any utility function in python which returns value when passed object and attribute to it?

Like we have in Java Beans util where you pass object and the property name it gives you the value do we have anything similar in python: def attr(obj, attr) return obj.attr ...

Google visualization api on app engine not working... (python)

I'm trying to render the sortable table that's provided in Google visualization API in my app on app engine, but it's not working. The app is written in python and uses the django framework. When I copy the generated HTML/Javascript and save it as a plain html file locally, it works just fine. This leads me to believe that the problem i...

gdb python scripting: where has `parse_and_eval` gone?

I had some scripts in Python to help me debugging with GDB that used the function gdb.parse_and_eval (still documented) to get the inferior values from the arguments passed to a scripted command, and now the module doesn't seem to have any trace of that function. Doing python import gdb; print dir(gdb) from GDB clearly shows that this fu...

How to solve this using regex?

Given that string: \n \n text1\n \ttext2\n Message: 1st message\n some more text\n \n \n Message: 2dn message\n\n \t\t Message: 3rd message\n text3\n I want to extract messages from a multiline string (token is 'Message: '). What regex expression should I use to capture those 3 groups: group 1 : '1st message' group 2 : '2dn mes...

Python: how to get sorted count of items in a list?

In Python, I've got a list of items like: mylist = [a, a, a, a, b, b, b, d, d, d, c, c, e] And I'd like to output something like: a (4) b (3) d (3) c (2) e (1) How can I output a count and leaderboard of items in a list? I'm not too bothered about efficiency, just any way that works :) Thanks! ...

Can I use XPCOM to create and manipulate a Firefox window as I would use win32 COM with IE?

With win32 COM I create an Internet Explorer instance and control it almost fully from my python code (manipulate windows, DOM elements, etc). More specifically, using DispatchEx('InternetExplorer.Application'). Can I do the same using XPCOM and C++/python? I need to automate certain actions taken on the html ui of some websites, so no ...

is python variable assignment atomic?

Let's say I am using a signal handler for handling an interval timer. def _aHandler(signum, _): global SomeGlobalVariable SomeGlobalVariable=True Can I set SomeGlobalVariable without worrying that, in an unlikely scenario that whilst setting SomeGlobalVariable (i.e. the Python VM was executing bytecode to set the variable), that t...