[PEAK] Enumeration instances --> values
Phillip J. Eby
pje at telecommunity.com
Mon Mar 21 10:46:17 EST 2005
At 04:58 PM 03/20/2005 -0500, Erik Rose wrote:
>I'm evaluating PEAK's domain modeling machinery for a project at work and
>have run into a question that's got me stumped. I have this Enumeration:
>
>class HatColor(Enumeration):
> red = model.enum('REDX')
> vermillion = model.enum('VERM')
>
>I also have a model that uses it and a mostly-finished toy DM that will
>pull that model in and out of an RDBMS. My trouble is that I want to store
>the four-letter codes 'REDX' and 'VERM' in the DB but can't find a way to
>map from HatColor.red back to 'REDX'. The bulletins example punts and just
>shoves the string 'HatColor.red' in the DB, which I'd obviously rather not
>do. Do I have to build my own machinery for this, or will the stock
>framework somehow let me map Enumeration instances (like HatColor.red)
>back to values? (I do realize that the main intent of passing values to
>enum() is to enable compares between values and instances, but mapping
>from instances to values sure would be nice as well.)
I've just checked in a new 'value' attribute so that you can do this. E.g.
'HatColor.red.value' would be 'REDX'. I'd been planning to do that for a
while and never got around to it, but it was easier to just add it now than
to explain why I hadn't gotten around to it yet. :)
More information about the PEAK
mailing list