python

Need regular expression expert: round bracket within stringliteral...

I'm searching for strings within strings using Regex. The pattern is a string literal that ends in (, e.g. # pattern " before the bracket (" # string this text is before the bracket (and this text is inside) and this text is after the bracket I know the pattern will work if I escape the character with a backslash, i.e.: # pattern " ...

Is it safe to replace MacOS X default python interpreter ?

Hi guys, i'm new to the Mac OS X world so i have to ask you this. I have the default python 2.6.1 installed as /usr/bin/python and the 3.1.2 as /usr/local/bin/python3.1 . Considering that i use only 3.x syntax, is it safe to replace the default interpreter (2.6) with the 3.1 one (python-config included) using symlinks (and removing old ...

Operating on a file's content despite a failure in the 'with' block

I've just written a utility in Python to do something I need (irrelevant, but it's to generate a ctags-compatible tag file for an in-house DSL). Anyway- I'm opening and reading the file in the context of a with statement, and I'm curious, how do people tend to handle failures in that process? My solution is with open(filename, 'rt') a...

Python: compiling regexp problems

I have a interesting problem. I have a list of lists, and I want all except the first element of each list compiled into a regular expression. And then put back into the list. The lists start as strings. The following code doesn't work. It doesn't raise an error, it just doesn't seem to do anything. I think I have identified the problem...

Form from Model with File Upload

I'm trying to mimic the admin interface for the Photologue app on the front end. To achieve this, I have thus far created a bit of code in the view: def galleryuploader(request): GalleryFormSet = modelformset_factory(GalleryUpload) if request.method == 'POST': formset = GalleryFormSet(request.POST, request.FILES) ...

Is there a neater alternative to `except: pass`?

I had a function that returned a random member of several groups in order of preference. It went something like this: def get_random_foo_or_bar(): "I'd rather have a foo than a bar." if there_are_foos(): return get_random_foo() if there_are_bars(): return get_random_bar() raise IndexError, "No foos, no...

C data structures

Is there a C data structure equatable to the following python structure? data = {'X': 1, 'Y': 2} Basically I want a structure where I can give it an pre-defined string and have it come out with an integer. ...

Creating a high and low game in python and need a lot of help!

Here is the Pseudocode Print instructions to the user Start with the variables high = 1000, low = 1, and tries = 1 While high is greater than low Guess the average of high and low Ask the user to respond to the guess Handle the four possible outcomes: If the guess was right, print a message that tries guesses were required and quit th...

list(y) behavior is "wrong" on first call

I have an iterator with a __len__ method defined. Questions: If you call list(y) and y has a __len__ method defined, then __len__ is called.    1) Why? In my output, you will see that the len(list(y)) is 0 on the first try. If you look at the list output, you will see that on the first call, I receive an empty list, and on the seco...

why i can't get the data form Model.all() using google app engine.

this is my code in main.py class marker_data(db.Model): geo_pt = db.GeoPtProperty() class HomePage(BaseRequestHandler): def get(self): a=marker_data() a.geo_pt=db.GeoPt(-34.397, 150.644) a.put() datas=marker_data.all() self.render_template('3.1.html',{'datas':datas}) and in the html is :...

Anyone can crash this code segment?

if the end user input anything else except 1, the program will exit, it will not be considered as crash ###substraction program print 'Subtraction program, v0.0.3 (beta)' loop = 1 while loop == 1: try: a = input('Enter a number to subtract from > ') b = input('Enter the number to subtract > ') except NameError: ...

Preserving styles using python's xlrd,xlwt, and xlutils.copy

Hi Folks, I'm using xlrd, xlutils.copy, and xlwt to open up a template file, copy it, fill it with new values, and save it. However, there doesn't seem to be any easy way to preserve the formatting of the cells; it always gets blown away and set to blank. Is there any simple way I can do this? Thanks! /YGA A sample script: from xlr...

How can I make this code Pythonic

So I have this code for an object. That object being a move you can make in a game of rock papers scissor. Now, the object needs to be both an integer (for matching a protocol) and a string for convenience of writing and viewing. class Move: def __init__(self, setMove): self.numToName = {0:"rock", 1:"paper",2:"scissors"} ...

python: timing of __new__ in metaclass

The following code doesn't compile; it says NameError: name 'fields' is not defined in the last line. Is it because __new__ isn't called until after the fields assignment is reached? What should I do? class Meta(type): def __new__(mcs, name, bases, attr): attr['fields'] = {} return type.__new__(mcs, name, bas...

Storing incoming telnet request into a list using twisted

I want to store http request line from telnet or browser into a list in python. I am using basic.LineReceiver protocol ...

How can I store testing data for python nosetests?

I want to write some tests for a python MFCC feature extractor for running with nosetest. As well as some lower-level tests, I would also like to be able to store some standard input and expected-output files with the unit tests. At the moment we are hard-coding the paths to the files on our servers, but I would prefer the testing file...

Storing and retrieving a list of Tuples using ConfigParser

I would like store some configuration data in a config file. Here's a sample section: [URLs] Google, www.google.com Hotmail, www.hotmail.com Yahoo, www.yahoo.com Is it possible to read this into a list of tuples using the ConfigParser module? If not, what do I use? ...

Practical example of Polymorphism

Can anyone please give me a real life, practical example of Polymorphism? My professor tells me the same old story I have heard always about the + operator. a+b = c and 2+2 = 4, so this is polymorphism. I really can't associate myself with such a definition, since I have read and re-read this in many books. What I need is a real world e...

Python subprocess how to determine if child process hangs?

Hello, How do I know is there my child process got hang while operating? ...

Django model form with selected rows

Hi All, I have a django model roughly as shown below: class Event(db.Model): creator = db.ReferenceProperty(User, required= True) title = db.TextProperty(required = True) description = db.TextProperty(required = True) class Ticket(db.Model): user = db.ReferenceProperty(User, required = True) event = db.ReferencePropert...