python

Which exception to raise if a given string does not match some format?

This is a follow up to an older question. Given a ISBN number, e.g. 3-528-03851-5 which exception type should I raise if the passed in string doesn't match the format X-XXX-XXXXX-X? ...

parallel file parsing, multiple CPU cores

I asked a related but very general question earlier (see especially this response). This question is very specific. This is all the code I care about: result = {} for line in open('input.txt'): key, value = parse(line) result[key] = value The function parse is completely self-contained (i.e., doesn't use any shared resources). ...

plotting a parabola within part of a repeating signal using numpy

I have a repeating signal that varies a little bit with each cycle of a process that repeats roughly every second, though the duration and the contents of each cycle vary from each other a little bit within some parameters. There are a thousand x,y coordinates for every second of my signal data. A small, but important, segment of the d...

Python: unit testing socket-based code?

I'm writing a Python client+server that uses gevent.socket for communication. SAre there any good ways of testing the socket-level operation of the code (for example, verifying that SSL connections with an invalid certificate will be rejected)? Or is it simplest to just spawn a real server? Edit: I don't believe that "naive" mocking wil...

Does FastCGI or Apache2 limit upload sizes?

I'm having a problem with file uploading. I'm using FastCGI on Apache2 (unix) to run a WSGI-compliant application. File uploads, in the form of images, are begin saved in a MySQL database. However, larger images are being truncated at 65535 bytes. As far as I can tell, nothing should be limiting the size of the files and I'm not sure whi...

limit_choice_to all objects of a particular model classe in a ForeignKey

Example class Base(): pass class A(Base) parent=models.Foreignkey("self", limit_choices_to=(all members of the B class) class B(Base) parent=models.Foreignkey("self", limit_choices_to=(all members of the A class) What would be the query syntax for limit_choices_to, to get only the objects of a certain class?) ...

What about this mess? Ideas to better rewrite it?

This mess is working well, but if somebody has any ideas to make it look/perform better, it would be greatly appreciated! def OnButtonClick(b, e, f="none"): if b == Gui["goleft"] and e == viz.UP: do_Cam([1.475, 7.862, 10.293]) if b == Gui["gocenter"] and e == viz.UP: do_Cam([0, 1, 52]) if b == Gui["goright"] and e == viz....

Python: How do I promote an object from a base class to a derived class?

Hope this isn't too basic, but... Suppose I have a base class pa(), and I have derived classes pai(), paj(), etc, that inherit from the base class. I would like to instantiate an object from base class pa(): >>> from pamod import * >>> mypa = pa() >>> mypa <pamod.pa object at 0x28d4fd0> >>> ... and then promote it (cast it) to a der...

Python "Value Error: cannot delete array elements" -- Why am I getting this?

Hi Everyone, I would really appreciate some help on this issue, I haven't been able to find anything about this value error online and I am at a complete loss as to why my code is illiciting this response. I have a large dictionary of something like 50 keys. The value associated with each key is a 2D array of many elements of the form...

What is the most popular GUI library for Python in Windows ?

What is the most popular GUI library for Python in Windows ? ...

Differences in regex syntax in Python and Java

Hi, I have a following working regex in Python and I am trying to convert it to Java, I thought that regex works the same in both languages, but obviously it doesn't. Python regex: ^\d+;\d+-\d+ My Java attempt: ^\\d+;\\d+-\\d+ Example strings that should be matched: 3;1-2,2-3 68;12-15,1-16,66-1,1-2 What is the right solution in Ja...

textarea to list

textarea returns this [u'a\r\nb\r\nc\r\nd\r\ne'] what is the best way to turn that into a list ['a', 'b', 'c', 'd', 'e'] Thanks! ...

include hash # in url with httplib2

I'm trying to make an http request using httplib2: import httplib2, time, re, urllib` conn = httplib2.Http(".cache") page = conn.request(u"http://www.mydomain.com/search?q=cars#p=100","GET") The response is ok, but the "#p=100" does not get passed over. Does anyone know how to pass this over with httplib2? thanks ...

How can I discover if a program is running from command line or from web?

I have a python script and I wanna know if the request is from web or from command line. How can I do this? Thanks ...

Pygtk and segmentation fault

Why I get this segmentation fault?: >>> import gtk >>> a = gtk.Window() >>> a.show() >>> b = gtk.Dialog("hellooo") >>> b.show() # here the dialog appears >>> b.show() Segmentation fault ...

Python - A Question on String Editing

Hello, I am trying to make a simple text-based game in Python. The idea of which revolves around a deck of playing cards, representing the card face values with A,2,3,4,5,6,7,8,9,10,J,Q, and K (Joker is not used). The suit of the cards will be represented by s,d,h, and c. As an example, "Ace of Hearts" would be represented as Ah. I am ...

parsing options in a config module

I use config module to store variables global to all modules. Is it a good place to parse the script arguments? (Note: config module is my own module, it just contains a bunch of global variables.) ----- config.py ----- from optparse import OptionParser parser = OptionParser() parser.add_option("-t", "--test", action = "store_true", de...

tic tac toe game in python

can any one tell me how to make tic tac toe game in python i just trying to find out but it is really not coming to my mind......... ...

Python function to convert seconds into minutes, hours, and days

Question: Write a program that asks the user to enter a number of seconds, and works as follows: There are 60 seconds in a minute. If the number of seconds entered by the user is greater than or equal to 60, the program should display the number of minutes in that many seconds. There are 3600 seconds in an hour. If the number of secon...

How to automatically move specific MySQL tables from one machine to another?

I have a MySQL database with tables in the form of "shard_0", "shard_1", "shard_2", etc. These are virtual shards. Now I want to add another DB server and move the even-numbered shards ("shard_0", "shard_2", "shard_4", ...) to the new machine. What is the best way to do that? There are many tables so ideally I wouldn't have to type out...