python

List all the classes that currently exist

I'm creating a simple API that creates typed classes based on JSON data that has a mandatory 'type' field defined in it. It uses this string to define a new type, add the fields in the JSON object, instantiate it, and then populate the fields on the instance. What I want to be able to do is allow for these types to be optionally pre-de...

XPath in XmlStream.addObserver doesn't work the way it should

What I want to do is to react only on specified root elements. For example, if user sends XmlStream that looks like: <auth> <login>user</login> <pass>dupa.8</pass> </auth> My method ._auth should be executed. I've done it with addObserver method called inside connectionMade method. self.addObserver("/auth", self._auth) AFAI...

Python regex for MD5 hash

I've come up with: re.findall("([a-fA-F\d]*)", data) but it's not very fool proof, is there a better way to grab all MD5-hash codes? ...

Close an easygui Python script with the standard 'close' button

I've created a very simple app, which presents an easygui entrybox() and continues to loop this indefinitely as it receives user input. I can quit the program using the Cancel button as this returns None, but I would also like to be able to use the standard 'close' button to quit the program. (ie. top right of a Windows window, top left...

Suggestions for a Cron like scheduler in Python?

I'm looking for a library in Python which will provide at and cron like functionality. I'd quite like have a pure Python solution, rather than relying on tools installed on the box; this way I run on machines with no cron. For those unfamiliar with cron: you can schedule tasks based upon an expression like: 0 2 * * 7 /usr/bin/run-ba...

How do I get the UTC time of "midnight" for a given timezone?

The best I can come up with for now is this monstrosity: >>> datetime.utcnow() \ ... .replace(tzinfo=pytz.UTC) \ ... .astimezone(pytz.timezone("Australia/Melbourne")) \ ... .replace(hour=0,minute=0,second=0,microsecond=0) \ ... .astimezone(pytz.UTC) \ ... .replace(tzinfo=None) datetime.datetime(2008, 12, 16, 13, 0) I.e., in ...

Geocoding libraries

Hi, I'm using python and I need to map locations like "Bloomington, IN" to GPS coordinates so I can measure distances between them. What Geocoding libraries/APIs do you recommend? Solutions in other languages are also welcome. Best, Bruno ...

Secure, sandboxable user exposed programming language / environment?

Beyond offering an API for my website, I'd like to offer users the ability to write simple scripts that would run on my servers . The scripts would have access to objects owned by the user and be able to manipulate, modify, and otherwise process their data. I'd like to be able to limit resources taken by these scripts at a fine level (e...

split string on a number of different characters

I'd like to split a string using one or more separator characters. E.g. "a b.c", split on " " and "." would give the list ["a", "b", "c"]. At the moment, I can't see anything in the standard library to do this, and my own attempts are a bit clumsy. E.g. def my_split(string, split_chars): if isinstance(string_L, basestring): ...

Running interactive commands in Paramiko

Hi, I'm trying to run an interactive command through paramiko. The cmd execution tries to prompt for a password but I do not know how to supply the password through paramiko's exec_command and the execution hangs. Is there a way to send values to the terminal if a cmd execution expects input interactively? ssh = paramiko.SSHClient...

Convert C++ Header Files To Python

I have a C++ header that contains #define statements, Enums and Structures. I have tried using the h2py.py script that is included with Python to no avail (except giving me the #defines converted). Any help would be greatly appreciated. ...

Why doesn't Python have a switch statement?

What is the reason Python doesn't have switch statement? ...

How to retrieve the parent node using cElementTree?

for the xml <grandparent> <parent1> <child>data1</child> </parent1> <parent2> <child>data2</child> </parent2> </grandparent> I need the list containing tuples of parent,data for each parent in xml. Is there a way to do it USING cElementTree? I am able to do it for child,data, but unfortunately child is identical in...

conversion of unicode string in python

I need to convert unicode strings in Python to other types such as unsigned and signed int 8 bits,unsigned and signed int 16 bits,unsigned and signed int 32 bits,unsigned and signed int 64 bits,double,float,string,unsigned and signed 8 bit,unsigned and signed 16 bit, unsigned and signed 32 bit,unsigned and signed 64 bit. I need help fro...

Where is a python real project to be used as example for the unit-test part?

I'm looking for a python project to use as example to copy the design of the unit test parts. The project should have these features: its code is almost fully unit tested the code is distributed in many packages, there are more that one level of packages all the test can be run with a single command, for example with python test.py ...

need help-variable creation in Python

I want to create varibales as a1,a2,a3,...a10. For that i used a for loop.As the variable in loop increments..i need var gettng created as above . Can anyone give me an idea? At the same time of creation i need to be able to assign to them There i m gettng syntax error ...

How can I find all the subsets of a set, with exactly n elements?

I am writing a program in Python, and I realized that a problem I need to solve requires me, given a set S with n elements (|S|=n), to test a function on all possible subsets of a certain order m (i.e. with m number of elements). To use the answer to produce a partial solution, and then try again with the next order m=m+1, until m=n. I ...

Should I use get_/set_ prefixes in Python method names?

In Python properties are used instead of the Java-style getters, setters. So one rarely sees get... or set.. methods in the public interfaces of classes. But in cases were a property is not appropriate one might still end up with methods that behave like getters or setters. Now my questions: Should these method names start with get_ / s...

How do you test a file.read() error in Python?

I have the following code (adapted from an example given in Dive Into Python) that reads the entire contents of a file into a buffer. buffer = "" try: file = open(postFileName, 'rU') try: # Read the entire POST log file into a buffer buffer += file.read() finally: file.close() except IOError: buf...

How to send clip names using LiveAPI (of Ableton Live)

When an audio or midi clip is played (triggered), its name needs to be sent using OSC to another application. LiveAPI is an interface which allows one to explore and automate Ableton Live using python scripts. The code to do this must be written in a python script, which must be placed in a specific folder where Ableton Live can find i...