[Chicago] Creating property() members programmatically

Tim Gebhardt tim at gebhardtcomputing.com
Sat Feb 7 16:27:11 CET 2009


Hello Chipy,

Does anyone know of a way to create property() members on an object
programmatically?  Anything I've Googled with "property" and
"programmatically" doesn't really come up with what I'm looking for.

Here's my use case:

I'm creating a ctypes wrapper for the LAME MP3 library.  There are quite a
few parameters that someone can set when encoding an MP3, such as the
bitrate, number of audio channels, high pass frequency, blah, blah, blah.
There's about 70.  The issue is that there's 3 places in my wrapper where I
have to declare them: once in the global LAME state object, once in setting
up the argument types and return types of the LAME getter/setter functions,
and once in my high level Python "Encoder" object.

So to make things easy and less error prone I declared a list of the
parameters and their types so I could reuse the data I hand-typed in about
these properties.  I also have a small dictionary of overrides for
parameters that aren't gettable or settable or that need to be handled with
special circumstances.  This worked fine for my first 2 places where I
needed the data, but I'm struggling to get it to work for the 3rd part, my
high-level Encoder object.

I need to be able to create a property() on my Encoder object because
setting a LAME parameter on an MP3 stream actually needs to call the LAME
library, and if the return value's not good then I need to throw an
exception.

The best I've been able to do is use my data about the LAME parameters,
output the Python code I need to a text file, and copy/paste that into my
object's declaration.

Does anyone know how I could create a bunch of property() items on an object
based on a list of data?

Thanks,

-Tim Gebhardt
tim at gebhardtcomputing.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chicago/attachments/20090207/a5b908da/attachment.htm>


More information about the Chicago mailing list