python

Problem sub-classing BaseException in Python

I wanted to create my own Python exception class, like this: class MyException(BaseException): def __init__(self, errno, address): if errno == 10048: mess = str(address) + ' is already in use' else: mess = 'Unable to open ' + str(address) BaseException.__init__(mess) but when the pro...

Idiomatic asynchronous design

Are there any sorts of useful idioms I can make use of when writing an API that is asynchronous? I would like to standardize on something as I seem to be using a few different styles throughout. It seems hard to make asynchronous code simple; I suppose this is because asynchronous operations are anything but. At the most basic level, th...

How do I create a Python class in C?

I have a legacy C library that creates a tree of objects. I would like to convert the tree into a pre-existing Python class. How do I create the PyObject for that class? ...

Getting python to work, Internal Server Error

Hi, I'm trying to get Python scripts, called from a web browser, to work. I keep getting the error: 500 Internal Server Error When I check my error logs I see the message Premature end of script headers The only documentation of this error online says that it can be the result of having improper line return characters in your sc...

What is the best (idiomatic) way to check the type of a Python variable?

I need to know if a variable in Python is a string or a dict. Is there anything wrong with the following code? if type(x) == type(str()): do_something_with_a_string(x) elif type(x) == type(dict()): do_somethting_with_a_dict(x) else: raise ValueError Update: I accepted avisser's answer (though I will change my mind if some...

What does the []-esque decorator syntax in Python mean?

Here's a snippet of code from within TurboGears 1.0.6: [dispatch.generic(MultiorderGenericFunction)] def run_with_transaction(func, *args, **kw): pass I can't figure out how putting a list before a function definition can possibly affect it. In dispatch.generic's docstring, it mentions: Note that when using older Python versi...

How to integrate the StringTemplate engine into the CherryPy web server

I love the StringTemplate engine, and I love the CherryPy web server, and I know that they can be integrated. Who has done it? How? EDIT: The TurboGears framework takes the CherryPy web server and bundles other related components such as a template engine, data access tools, JavaScript kit, etc. I am interested in MochiKit, demand C...

What are these tags @ivar @param and @type in python docstring?

The ampoule project uses some tags in docstring, like the javadoc ones. For example from pool.py line 86: def start(self, ampChild=None): """ Starts the ProcessPool with a given child protocol. @param ampChild: a L{ampoule.child.AMPChild} subclass. @type ampChild: L{ampoule.child.AMPChild} subclass """ What are ...

How much slower is a wxWidget written in Python versus C++?

I'm looking into writing a wxWidget that displays a graphical node network, and therefore does a lot of drawing operations. I know that using Python to do it is going to be slower, but I'd rather get it working and port it later when its functional. Ideally, if the performance hit isn't too great, I'd prefer to keep the codebase in Pyt...

Python - Parse String to Float or Int

This should be simple - In python, how can I parse a numeric string like "545.2222" to its corresponding float value, 542.2222 or "31" to an integer, 31? EDIT: I just wanted to know how to parse a float string to a float, and (separately) an int string to an int. Sorry for the confusing phrasing/original examples on my part. At any r...

How do you make a class attribute that isn't a standard data type?

I have one class that needs to grab an attribute that is set in another. It's not a standard data type though. Here's the code; class graphics: def __init__(self, Fullscreen = False, Width = 640, Height = 480): print "Graphics Init" SCREEN_SIZE = (Width, Height) pygame.init() if Fullscreen: self.screen = pygame.d...

How do I respond to mouse clicks on sprites in PyGame?

What is the canonical way of making your sprites respond to mouse clicks in PyGame ? Here's something simple, in my event loop: for event in pygame.event.get(): if event.type == pygame.QUIT: exit_game() [...] elif ( event.type == pygame.MOUSEBUTTONDOWN and pygame.mouse.get_pressed()[0]): for sp...

How do I get the whole text of an element using ElementTree?

That is, all text and subtags, without the tag of an element itself? Having <p>blah <b>bleh</b> blih</p> I want blah <b>bleh</b> blih element.text returns "blah " and etree.tostring(element) returns: <p>blah <b>bleh</b> blih</p> ...

How do I get PIL to work when built on mingw/cygwin?

I'm trying to build PIL 1.1.6 against cygwin or mingw whilst running against a windows install of python. When I do either the build works but I get the following failure when trying to save files. $ python25 Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "li...

How to do this - python dictionary traverse and search

I have nested dictionaries: {'key0': {'attrs': {'entity': 'p', 'hash': '34nj3h43b4n3', 'id': '4130'}, u'key1': {'attrs': {'entity': 'r', 'hash': '34njasd3h43b4n3', 'id': '4130-1'}, u'key2': {'attrs': {'entity': 'c', ...

Python: single instance of program

So, is there a Pythonic way to have only one instance of program running? The only reasonable solution I've come up with is trying to run it as a server on some port, then second program trying to bind to same port - fails. But it's not really a great idea, maybe there's something more lightweight than this? (Take into consideration...

Psyco x64?

Is there a way to get the same sort of speedup on x64 architecture as you can get from psyco on 32 bit processors? ...

Comparing massive lists of dictionaries in python

I never actually thought I'd run into speed-issues with python, but I have. I'm trying to compare really big lists of dictionaries to each other based on the dictionary values. I compare two lists, with the first like so biglist1=[{transaction:"somevalue", id:"somevalue", date:"somevalue" ...}, {transaction:"somevalue", id:"somevalue", ...

Online Java Book

Is there an online java book like Dive into Python for learning Python? Other resources online besides the standard Java Documentation (which is awesome but almost too technical) thx ...

When would you use Infinity?

So in Ruby there is a trick to specify infinity: 1.0/0 => Infinity I believe in Python you can do something like this float('inf') These are just examples though, I'm sure most languages have infinity in some capacity. When would you actually use this construct in the real world? Why would using it in a range be better than just us...