faqts : Computers : Programming : Languages : Python : Snippets

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

1 of 6 people (17%) answered Yes
Recently 0 of 5 people (0%) answered Yes

Entry

Enumerations (was: parsing chemical formula)

Jul 5th, 2000 10:00
Nathan Wallace, Hans Nowak, Snippet 131, Christian Tismer


"""
Packages: basic_applications.enumeration;miscellaneous.sick_and_twisted
"""
"""
Sorry, tutor list,
but I could not resist.
I will stop this now. (Ahem)
Martijn Faassen wrote:
> 
> Christian Tismer wrote:
> 
> > Now, if you must, how 'bout this:
[second try. But watch out...]
> Hm, this is too many lines [whine, nag, never-pleased-ly yours]. As
> kludge trickery though, it's way cool!
> 
> > I think I have to duck and cover if Timbot watches me :-)
> 
> Indeed, Timbot is sure to catch us at this soon!
Now I will him give a final reason to pursue me.
"""
def enum(func):
    g = globals() ; code = func.func_code
    base = apply(func, (0,)*code.co_argcount) or 0
    for i in range(code.co_argcount):
        g[code.co_varnames[i]] = i+base
"""
Yeees, this is hefty.
But now watch out what I got for the application:
"""
enum(lambda ONE, TWO, THREE, FOUR, FIVE:1)
# this gives us an enumeration, based from 1. Believe me.
print ONE, TWO, THREE, FOUR, FIVE
# (1, 2, 3, 4, 5)
"""
Naaah? Oh yeah, now we really have a "useful" lambda example :-)))
ciao - chris.ducked.and.covered.for.the.rest.of....
"""