python

How can I read Perl data structures from Python?

I've often seen people use Perl data structures in lieu of configuration files; i.e. a lone file containing only: %config = ( 'color' => 'red', 'numbers' => [5, 8], qr/^spam/ => 'eggs' ); What's the best way to convert the contents of these files into Python-equivalent data structures, using pure Python? For the time being...

Elegant ways to support equivalence ("equality") in Python classes

When writing custom classes it is often important to allow equivalence by means of the == and != operators. In Python, this is made possible by implementing the __eq__ and __ne__ special methods, respectively. The easiest way I've found to do this is the following method: class Foo: def __init__(self, item): self.item = item...

Interpreting Excel Currency Values

I am using python to read a currency value from excel. The returned from the range.Value method is a tuple that I don't know how to parse. For example, the cell appears as $548,982, but in python the value is returned as (1, 1194857614). How can I get the numerical amount from excel or how can I convert this tuple value into the numeri...

How do you debug Mako templates?

So far I've found it impossible to produce usable tracebacks when Mako templates aren't coded correctly. Is there any way to debug templates besides iterating for every line of code? Edit: Per the comments, it seems switching to Jinja2 might be the best solution. ...

Is there any built-in way to get the length of an iterable in python?

For example, files, in Python, are iterable - they iterate over the lines in the file. I want to count the number of lines. One quick way is to do this: lines = len(list(open(fname))) However, this loads the whole file into memory (at once). This rather defeats the purpose of an iterator (which only needs to keep the current line in...

Calling Application Methods from a wx Frame Class

I'm starting out with wxPython and have been working my way through every tutorial and example I can get my hands on. I've run into a slight problem, however, and it has to do with the wx.App versus the wx.Frame and which should contain specific methods. Just about every example I've seen don't go much beyond layouts/sizers and event h...

JavaScript parser in Python

There is a JavaScript parser at least in C and Java (Mozilla), in JavaScript (Mozilla again) and Ruby. Is there any currently out there for Python? I don't need a JavaScript interpreter, per se, just a parser that's up to ECMA-262 standards. A quick google search revealed no immediate answers, so I'm asking the SO community. ...

pyGTK Radio Button Help

Alright, I'll preface this with the fact that I'm a GTK and Python newb, but I haven't been able to dig up the information I needed. Basically what I have is a list of Radio Buttons, and based on which one is checked, I need to connect a button to a different function. I tried creating all my radio buttons, and then creating a disgusting...

Organising my Python project

I'm starting a Python project and expect to have 20 or more classes in it. As is good practice I want to put them in a separate file each. However, the project directory quickly becomes swamped with files (or will when I do this). If I put a file to import in a folder I can no longer import it. How do I import a file from another folder...

Python Optparse list

I'm using the python optparse module in my program, and I'm having trouble finding an easy way to parse an option that contains a list of values. For example: --groups one,two,three. I'd like to be able to access these values in a list format as options.groups[]. Is there an optparse option to convert comma seperated values into a lis...

wxPython and sharing objects between windows

I've been working with python for a while now and am just starting to learn wxPython. After creating a few little programs, I'm having difficulty understanding how to create objects that can be shared between dialogs. Here's some code as an example (apologies for the length - I've tried to trim): import wx class ExampleFrame(wx.Frame...

What are your (concrete) use-cases for metaclasses in Python?

I have a friend who likes to use metaclasses, and regularly offers them as a solution. I am of the mind that you almost never need to use metaclasses. Why? because I figure if you are doing something like that to a class, you should probably be doing it to an object. And a small redesign/refactor is in order. Being able to use metaclas...

Is there a Ruby/Python HTML reflow/layout library?

I'm looking for a library in Ruby or Python that would take some HTML and CSS as the input and return data that contains the positions and sizes of the elements. If it helps, I don't need the info for all the elements but just the major divs of the page. ...

Modify bound variables of a closure in Python

Is there any way to modify the bound value of one of the variables inside a closure? Look at the example to understand it better. def foo(): var_a = 2 var_b = 3 def _closure(x): return var_a + var_b + x return _closure localClosure = foo() # Local closure is now "return 2 + 3 + x" a = localClosure(1) # 2 + 3 + 1 == 6 # DO SO...

Good language to develop a game server in?

Hello, I was just wondering what language would be a good choice for developing a game server to support a large (thousands) number of users? I dabbled in python, but realized that it would just be too much trouble since it doesn't spawn threads across cores (meaning an 8 core server=1 core server). I also didn't really like the languag...

How to make a color from a number

I have a function that returns a float from 0 to 255. I would like to make a gradient in red color from this, but I need a string in "#FFFFFF" format. Is there a function for that? ...

Length of generator output

Python provides a nice method for getting length of an eager iterable, len(x) that is. But I couldn't find anything similar for lazy iterables represented by generator comprehensions and functions. Of course, it is not hard to write something like: def iterlen(x): n = 0 try: while True: next(x) n += 1 except StopIt...

Python sqlite3 and concurrency

I have a Python program that uses the "threading" module. Once every second, my program starts a new thread that fetches some data from the web, and stores this data to my hard drive. I would like to use sqlite3 to store these results, but I can't get it to work. The issue seems to be about the following line: conn = sqlite3.connect("my...

What values to use for FastCGI maxrequests, maxspare, minspare, maxchildren?

I'm running a Django app using FastCGI and lighttpd. Can somebody explain me what I should consider when deciding what value to use for maxrequests, maxspare, minspare, maxchildren? These options are not too well documented, but seem quite important. Don't just tell me what they do; I want to understand what implications they have and...

Django + FastCGI - randomly raising OperationalError

I'm running a Django application. Had it under Apache + mod_python before, and it was all OK. Switched to Lighttpd + FastCGI. Now I randomly get the following exception (neither the place nor the time where it appears seem to be predictable). Since it's random, and it appears only after switching to FastCGI, I assume it has something to ...