Python Pseudo-Switch

J

James Stroud

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/
 
D

Dan Bishop

James said:
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.
.... exec 'def _(%s): return %s' % (','.join(arg_names), expression)
.... return _
....7
 
J

Jason Mobarak

James said:
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/
 
J

Jason Mobarak

# -*- 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")
 

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
473,770
Messages
2,569,588
Members
45,093
Latest member
Vinaykumarnevatia00

Latest Threads

Top