python

Regular Expression Sub Problems

Okay so i have a semi weridish problem with re.sub. Take the following code: import re str_to_be_subbed = r'somefile.exe -i <INPUT>' some_str = r'C:\foobar' s = re.sub(r'\<INPUT\>', some_str, str_to_be_subbed) print s I would think it would give me: somefile.exe -i C:\\foobar But instead it gives me: somefile.exe -i C:♀oobar I ...

How do I refer to a class method outside a function body in Python?

I want to do a one time callback registration within Observer. I don't want to do the registration inside init or other function. I don't know if there is a class level equivalent for init class Observer: @classmethod def on_new_user_registration(new_user): #body of handler... # first I try ...

Python's sys.path value

Where is Python's sys.path initialized from? UPD: Python is adding some paths before refering to PYTHONPATH: >>> import sys >>> from pprint import pprint as p >>> p(sys.path) ['', 'C:\\Python25\\lib\\site-packages\\setuptools-0.6c9-py2.5.egg', 'C:\\Python25\\lib\\site-packages\\orbited-0.7.8-py2.5.egg', '...

Python equivalent of PHP's memory_get_usage()?

I've already found the following question, but I was wondering if there was a quicker and dirtier way of grabbing an estimate of how much memory the python interpreter is currently using for my script that doesn't rely on external libraries. I'm coming from PHP and used to use memory_get_usage() and memory_get_peak_usage() a lot for thi...

File I/O in the Python 3 C API

The C API in Python 3.0 has changed (deprecated) many of the functions for File Objects. Before, in 2.X, you could use PyObject* PyFile_FromString(char *filename, char *mode) to create a Python file object, e.g: PyObject *myFile = PyFile_FromString("test.txt", "r"); ...but such function no longer exists in Python 3.0. What would b...

Convert list of lists to delimited string.

Hi. How do I do the following in Python 2.2 using built-in modules only? I have a list of lists like this: [['dog', 1], ['cat', 2, 'a'], ['rat', 3, 4], ['bat', 5]] And from it I'd like to produce a string representation of a table like this where the columns are delimited by tabs and the rows by newlines. dog 1 cat 2 a rat 3 4 b...

Django shopping cart/basket solution (or should I DIM)?

Hi there. I'm about to build a site that has about half a dozen fairly similar products. They're all DVDs so they fit into a very "fixed" database very well. I was going to make a DVD model. Tag them up. All very simple. All very easy. But we need to be able to sell them. The current site outsources the whole purchasing system but that...

How can I detect if a file is binary (non-text) in python?

How can I tell if a file is binary (non-text) in python? I am searching through a large set of files in python, and keep getting matches in binary files. This makes the output look incredibly messy. I know I could use grep -I, but I am doing more with the data than what grep allows for. In the past I would have just searched for charac...

How do you set the text direction for a TextTable Cell in OpenOffice?

I want to set the text direction for some cells in a TextTable so that they are vertical (i.e., the text is landscape instead of portrait). You can do this in Writer by selecting the cell(s), and going to: Table - Text Properties - Text Flow - Text Direction However, I cannot figure out how to do this through the API. I tried using Char...

In Python how do I sort a list of dictionaries by a certain value of the dictionary + alphabetically?

Ok, here's what I'm trying to do... I know that itemgetter() sort could to alphabetical sort easy, but if I have something like this: [{'Name':'TOTAL', 'Rank':100}, {'Name':'Woo Company', 'Rank':15}, {'Name':'ABC Company', 'Rank':20}] And I want it sorted alphabetically (by Name) + include the condition that the one with Name:'...

How should I verify a log message when testing Python code under nose?

Hi all I'm trying to write a simple unit test that will verify that, under a certain condition, a class in my application will log an error via the standard logging API. I can't work out what the cleanest way to test this situation is. I know that nose already captures logging output through it's logging plugin, but this seems to be i...

Python: Write a list to a file

Is this the cleanest way to write a list to a file, since writelines() doesn't insert newline characters? file.writelines(["%s\n" % item for item in list]) It seems like there would be a standard way... Thanks Josh ...

Python-like list comprehension in Java

Since Java doesn't allow passing methods as parameters, what trick do you use to implement Python like list comprehension in Java ? I have a list (ArrayList) of Strings. I need to transform each element by using a function so that I get another list. I have several functions which take a String as input and return another String as outp...

How to find a file upwardly in Python?

What is the best way to find a file "upwardly" in Python? (Ideally it would work on Windows too). E.g., >>> os.makedirs('/tmp/foo/bar/baz/qux') >>> open('/tmp/foo/findme.txt', 'w').close() >>> os.chdir('/tmp/foo/bar/baz/qux') >>> findup('findme.txt') '/tmp/foo/findme.txt' As far as I can tell, there isn't anything in the Python stan...

Python, how to parse strings to look like sys.argv

I would like to parse a string like this: -o 1 --long "Some long string" into this: ["-o", "1", "--long", 'Some long string'] or similar. This is different than either getopt, or optparse, which start with sys.argv parsed input (like the output I have above). Is there a standard way to do this? Basically, this is "splitting" wh...

Bundling PyQwt with py2exe

I have a standard setup script for py2exe with which I bundle PyQt-based applications into Windows .exe files. Today I tried a simple script that uses the PyQwt module, and it doesn't seem to work. py2exe runs alright, but when I execute the .exe it creates, it dumps the following into a log file and doesn't run: Traceback (most recent...

Pad an integer using a regular expression

I'm using regular expressions with a python framework to pad a specific number in a version number: 10.2.11 I want to transform the second element to be padded with a zero, so it looks like this: 10.02.11 My regular expression looks like this: ^(\d{2}\.)(\d{1})([\.].*) If I just regurgitate back the matching groups, I use this st...

pywikipedia name wikiquote is not defined?

Hi guys, I'm writing a bot for Wikipedia but have a problem. When I want to get stuff from another Wikimedia site I get the error - error-name 'wikiquote' is not defined. This is when I start the code off like this- import wikipedia site = wikiquote.getSite() Yet if I was to start it with wikipedia written instead of wikiquote, it ...

Getting the caller function name inside another function in Python?

If you have 2 functions like: def A def B and A calls B, can you get who is calling B inside B, like: def A () : B () def B () : this.caller.name ...

Is there a high-level way to read in lines from an output file and have the types recognized by the structure of the contents?

Suppose I have an output file that I want to read and each line was created by joining several types together, prepending and appending the list braces, [('tupleValueA','tupleValueB'), 'someString', ('anotherTupleA','anotherTupleB')] I want to read the lines in. Now I can read them in, and operate on the string to assign values and ...