Re: Python Pseudo-Switch
# -*- python -*- import sys def switchFor (target): sw = '__switch_table__' # please pretend the frame hack is not here, # it happens to work for classes and messing with # frame stack in a try/except is probably okay try: raise Exception() except: defs = sys.exc_info()[2].tb_frame.f_back.f_locals if sw not in defs: defs[sw] = {} table = defs[sw] def _(meth): table[target] = meth return meth return _ class SwitchMixin (object): def __init__ (self): self.__switch_table__ = self.__switch_table__.copy() def switchOn (self, target, *args, **kw): return self.__switch_table__[target](self, *args, **kw) if __name__ == '__main__': class SwitchTest(SwitchMixin): @switchFor("foo") def switch (self, arg): print arg * 3 @switchFor("bar") def switch (self, arg): print "__%s__" % (arg,) @switchFor("baz") def switch (self, arg): print arg + ''.join(reversed(arg)) st = SwitchTest() st.switchOn("foo", "oof") st.switchOn("bar", "rab") st.switchOn("baz", "zab") -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Pseudo-Switch
James Stroud wrote: > Hello All, > > Because of my poorly designing a database, I have recently found it necessary > to explore the wonders of the Python pseudo-switch: > > do_case = { "A" : lambda x: x["bob"], > "B" : lambda x: x["carol"], > "C" : lambda x: "Ted", > "D" : lambda x: do_something(x) } > class CaseThing: def pref_A (self, x): return x["bob"] def pref_B (self, x): return x["carol"] def pref_C (self, x); return "Ted" def pref_D (self, x) return do_something(x) def getThing (self, x): attr = getattr(self, 'pref_%s' % (x,)) if attr is not None: return attr else: raise SomeError("Thing %s does not exist" % (x,)) my_thing = CaseThing().getThing(get_value_from_thin_air)(adict) You can do something similar with method decorators for more complex strings than what's allowed for python method names. > my_thing = do_case[get_value_from_thin_air()](adict) > > > How to handle this kind of thing when lambda is removed from the language, > beside the obvious def'ing those tiny little functions? > > James > > -- > James Stroud > UCLA-DOE Institute for Genomics and Proteomics > Box 951570 > Los Angeles, CA 90095 > > http://www.jamesstroud.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Pseudo-Switch
James Stroud wrote: > Hello All, > > Because of my poorly designing a database, I have recently found it necessary > to explore the wonders of the Python pseudo-switch: > > do_case = { "A" : lambda x: x["bob"], > "B" : lambda x: x["carol"], > "C" : lambda x: "Ted", > "D" : lambda x: do_something(x) } > > my_thing = do_case[get_value_from_thin_air()](adict) > > > How to handle this kind of thing when lambda is removed from the language, > beside the obvious def'ing those tiny little functions? You can always re-invent lambda. >>> def lambda2(arg_names, expression): ...exec 'def _(%s): return %s' % (','.join(arg_names), expression) ...return _ ... >>> add = lambda2(('x', 'y'), 'x + y') >>> add(3, 4) 7 -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Pseudo-Switch
"James Stroud" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Because of my poorly designing a database, I have recently found it > necessary > to explore the wonders of the Python pseudo-switch: > > do_case = { "A" : lambda x: x["bob"], >"B" : lambda x: x["carol"], >"C" : lambda x: "Ted", >"D" : lambda x: do_something(x) } > > my_thing = do_case[get_value_from_thin_air()](adict) > > How to handle this kind of thing when lambda is removed from the > language, > beside the obvious def'ing those tiny little functions? I don't believe any cast-in-concrete decision about lambda has been made yet. I expect there will be a PEP for removals when appropriate. Comments such as this will bolster the case against removal without replacement if then resubmitted TJR -- http://mail.python.org/mailman/listinfo/python-list
Python Pseudo-Switch
Hello All, Because of my poorly designing a database, I have recently found it necessary to explore the wonders of the Python pseudo-switch: do_case = { "A" : lambda x: x["bob"], "B" : lambda x: x["carol"], "C" : lambda x: "Ted", "D" : lambda x: do_something(x) } my_thing = do_case[get_value_from_thin_air()](adict) How to handle this kind of thing when lambda is removed from the language, beside the obvious def'ing those tiny little functions? James -- James Stroud UCLA-DOE Institute for Genomics and Proteomics Box 951570 Los Angeles, CA 90095 http://www.jamesstroud.com/ -- http://mail.python.org/mailman/listinfo/python-list