python

Writing a Domain Specific Language for selecting rows from a table

I'm writing a server that I expect to be run by many different people, not all of whom I will have direct contact with. The servers will communicate with each other in a cluster. Part of the server's functionality involves selecting a small subset of rows from a potentially very large table. The exact choice of what rows are selected wil...

Regular expressions but for writing in the match

When using regular expressions we generally, if not always use them to extract some kind of information. What I need is to replace the match value with some other value... Right now I'm doing this... def getExpandedText(pattern, text, replaceValue): """ One liner... really ugly but it's only used in here. """ retu...

Using locale.setlocale in embedded Python without breaking file parsing in C thread

We're using a third-party middleware product that allows us to write code in an embedded Python interpreter, and which exposes an API that we can call into. Some of these API calls allow us to load various kinds of file, and the loading code is implemented in C. File loading happens in a separate thread, and calls back into Python when t...

Authenticating against active directory using python + ldap

How do I authenticate against AD using Python + LDAP. I'm currently using the python-ldap library and all it is producing is tears. I can't even bind to perform a simple query: import sys import ldap Server = "ldap://my-ldap-server" DN, Secret, un = sys.argv[1:4] Base = "dc=mydomain,dc=co,dc=uk" Scope = ldap.SCOPE_SUBTREE Filter = "...

Looking for File Traversal Functions in Python that are Like Java's

In Java you can do File.listFiles() and receive all of the files in a directory. You can then easily recurse through directory trees. Is there an analogous way to do this in Python? ...

How to list only top level directories in Python?

I want to be able to list only the directories inside some folder. This means I don't want filenames listed, nor do I want additional sub-folders. Let's see if an example helps. In the current directory we have: >>> os.listdir(os.getcwd()) ['cx_Oracle-doc', 'DLLs', 'Doc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'mod_p ython-wininst.lo...

How do I find what is using memory in a Python process in a production system?

My production system occasionally exhibits a memory leak I have not been able to reproduce in a development environment. I've used a Python memory profiler (specifically, Heapy) with some success in the development environment, but it can't help me with things I can't reproduce, and I'm reluctant to instrument our production system with...

How can I create a status bar item with Cocoa and Python (PyObjC)?

I have created a brand new project in XCode and have the following in my AppDelegate.py file: from Foundation import * from AppKit import * class MyApplicationAppDelegate(NSObject): def applicationDidFinishLaunching_(self, sender): NSLog("Application did finish launching.") statusItem = NSStatusBar.systemStatusBar().stat...

How do I wrap a string in a file in Python?

How do I create a file-like object (same duck time as File) with the contents of a string? ...

Is there an easy way to populate SlugField from CharField?

class Foo(models.Model): title = models.CharField(max_length=20) slug = models.SlugField() Is there a built-in way to get the slug field to autopopulate based on the title? Perhaps in the Admin and outside of the Admin. ...

overloading __init__ in python

Let's say I have a class that has a member called data which is a list. I want to be able to initialize the class with, for example, a filename (which contains data to initialize the list) or with an actual list. What's your technique for doing this? Do you just check the type by looking at __class__? Is there some trick I might be...

What limitations have closures in Python compared to language X closures?

Where X is any programming language (C#, Javascript, Lisp, Perl, Ruby, Scheme, etc) which supports some flavour of closures. Some limitations are mentioned in the Closures in Python (compared to Ruby's closures), but the article is old and many limitations do not exist in modern Python any more. Seeing a code example for a concrete l...

Socket programming for mobile phones in Python

I've written code for communication between my phone and comp thru TCP sockets. When I type out the code line by line in the interactive console it works fine. However, when i try running the script directly through filebrowser.py it just wont work. I'm using Nokia N95. Is there anyway I can run this script directly without using filebro...

How do I dump an entire Python process for later debugging inspection?

I have a Python application in a strange state. I don't want to do live debugging of the process. Can I dump it to a file and examine its state later? I know I've restored corefiles of C programs in gdb later, but I don't know how to examine a Python application in a useful way from gdb. (This is a variation on my question about debu...

How do I get the key value of a db.ReferenceProperty without a database hit?

Is there a way to get the key (or id) value of a db.ReferenceProperty, without dereferencing the actual entity it points to? I have been digging around - it looks like the key is stored as the property name preceeded with an _, but I have been unable to get any code working. Examples would be much appreciated. Thanks. EDIT: Here is ...

Python: How to make a cross-module variable?

The __debug__ variable is handy in part because it affects every module. If I want to create another variable that works the same way, how would I do it? The variable (let's be original and call it 'foo') doesn't have to be truly global, in the sense that if I change foo in one module, it is updated in others. I'd be fine if I could set...

How do I upgrade python 2.5.2 to python 2.6rc2 on ubuntu linux 8.04?

Hi, I'd like to how to upgrade the default python installation(2.5.2) supplied with ubuntu 8.04 to python 2.6rc2. I'd like to make 2.6 the default python version on the system and migrate all the other useful installed python libraries installed on 2.5.2 to python 2.6rc2. Please let me know how I can achieve this. Thanks Dirk ...

Does Python have a bitfield type?

I need a compact representation of an array of booleans, does Python have a builtin bitfield type or will I need to find an alternate solution? ...

Best Python book for a lifelong PHP developer?

I've been coding in PHP for almost ten years, but I've lately become interested in Python. What Python book would you recommend? ...

Drag and drop onto Python script in Windows Explorer

I would like to drag and drop my data file onto a Python script and have it process the file and generate output. The Python script accepts the name of the data file as a command-line parameter, but Windows Explorer doesn't allow the script to be a drop target. Is there some kind of configuration that needs to be done somewhere for this...