[Tutor] parsing chemical formula

Christian Tismer tismer@appliedbiometrics.com
Tue, 30 Mar 1999 18:03:48 +0200


Martijn Faassen wrote:
> 
> Christian Tismer wrote:
> >
> > Martijn Faassen wrote:
> [enum thoughts]
> > :-))

[me, with an enum impl by strings]

> Cool, I hadn't thought of using string.split to reduce the amount of
> quotes (and commas!). Still not exactly of the elegance of:
> 
>     ONE, TWO, THREE = range(3) # this *looks* like Python code (as it is
> :)
> 
> but almost there, and no counting!

Now, if you must, how 'bout this:

>>> def make_enum(func, base):
... 	g = globals()
... 	for i in range(func.func_code.co_argcount):
... 	    g[func.func_code.co_varnames[i]] = i+base
... 	del g[func.__name__]
... 
>>> # after this, you can fake an enum like this
>>> def enum(ONE, TWO, THREE) : pass
>>> # and you activate it so:
>>> make_enum(enum, 1)
>>> # here we are:
>>> ONE, TWO, THREE
(1, 2, 3)
>>> 

I think I have to duck and cover if Timbot watches me :-)

ciao - chris

-- 
Christian Tismer             :^)   <mailto:tismer@appliedbiometrics.com>
Applied Biometrics GmbH      :     Have a break! Take a ride on Python's
Kaiserin-Augusta-Allee 101   :    *Starship* http://starship.python.net
10553 Berlin                 :     PGP key -> http://wwwkeys.pgp.net
PGP Fingerprint       E182 71C7 1A9D 66E9 9D15  D3CC D4D7 93E2 1FAE F6DF
     we're tired of banana software - shipped green, ripens at home