best way to have enum-like identifiers?

M

mh

I currently have a 2-dim hash, indexed by two strings:

template['py']['funcpre']
template['py']['funcpost']
...

but I would prefer to have these indexed by constants of
some kind, since this seems a bit more natural:

template[py][funcpre]
template[py][funcpost]
...

Currently I'm just putting this at the top of the file:

py=1
funcpre=2
funcpost=3
...

but I'm curious if there's a better way of doing this,
some kind of enum-like thing or somesuch.

Many TIA!
Mark
 
B

Ben Finney

Currently I'm just putting this at the top of the file:

py=1
funcpre=2
funcpost=3
...

Slightly better is:

py = object()
funcpre = object()
funcpost = object()

Thus, those names are all bound to unique objects, that won't be
unexpectedly duplicated by some other value.
but I'm curious if there's a better way of doing this, some kind of
enum-like thing or somesuch.

Please try the 'enum' package in the Cheeseshop:

<URL:http://pypi.python.org/pypi/enum>
 
C

castironpi

[enums snip]
Thus, those names are all bound to unique objects, that won't be
unexpectedly duplicated by some other value.


Please try the 'enum' package in the Cheeseshop:

Am I the only one superstitious about putting identifiers in strings?
 When a well-packaged web of lies has been sold to the masses
... someone sold them.
c= type('Enum',(),{})
[ setattr( c, v, object() ) for v in 'py funcpre funcpost'.split() ] [None, None, None]
c.funcpost
c.funcpre
<object object at 0x00A37470>

You don't need them to be ints or anything, do you, like USER_BASE+ 1?
 
P

Peter Otten

I currently have a 2-dim hash, indexed by two strings:

template['py']['funcpre']
template['py']['funcpost']
...

but I would prefer to have these indexed by constants of
some kind, since this seems a bit more natural:

template[py][funcpre]
template[py][funcpost]
...

Currently I'm just putting this at the top of the file:

py=1
funcpre=2
funcpost=3
...
but I'm curious if there's a better way of doing this,
some kind of enum-like thing or somesuch.

A dictionary with a fixed set of keys is better spelt as a class, e. g.
instead of the inner dictionary you can use something like

class Hooks(object):
def __init__(self, pre=None, post=None):
self.pre = pre
self.post = post

....
def before(): print "before"
def after(): print "after"

template["py"] = Hooks(before, after)

Peter
 
P

Pete Forman

> Currently I'm just putting this at the top of the file:

> py=1
> funcpre=2
> funcpost=3
> ...

That can be done more compactly with

py, funcpre, funcpost = range(3)

give or take 1.

> but I'm curious if there's a better way of doing this,
> some kind of enum-like thing or somesuch.

https://launchpad.net/munepy describes itself as yet another Python
enum implementation. Its author is Barry Warsaw.
 
T

Tim Chase

Currently I'm just putting this at the top of the file:
That can be done more compactly with

py, funcpre, funcpost = range(3)

I've harbored a hope that a combination of PEP 3132[1] ("Extended
Iterable unpacking") and itertools.count()[2] would be available
for doing something like this:

py, funcpre, funcpost, *unexhausted_iterator = count()

which would theoretically allow me to just add new enum names to
the LHS without having to update constant passed to range() on
the RHS.

Unfortunately, it looks like this wasn't a desirable behavior
because the PEP describes the "*unexhausted_iterator" notation
unpacking as a list, not as an iterable.

My desired syntax would work well for bit-mask fields as well:

def bit_iter(i=0):
assert i >= 0
while True:
yield 1 << i
i += 1
read, write, execute, *_ = bit_iter()

and I'm sure there are other use-cases I've not yet considered.

Diez Roggisch hacked together a disturbingly (in the "that hurts
my brain to sniff the stack" way) beautiful/functional
decorator[3] that does something like this in Python2.4+ and can
just be used something like

@variably_unpack
def just_enough(): return itertools.count()
read, write, execute = just_enough()

which is a fabulous syntax, IMHO.

-tkc

[1]
http://www.python.org/dev/peps/pep-3132/

[2]
http://docs.python.org/dev/library/itertools.html#itertools.count

[3]
http://groups.google.com/group/comp...474e196adac/d98522d9bedae946#d98522d9bedae946
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top