Skip to content

Instantly share code, notes, and snippets.

@johtso johtso/gist:1482026
Created Dec 15, 2011

Embed
What would you like to do?
class MultiLoader(object):
def __init__(self):
self.d = {}
def load(self, values):
for k, v in zip(self.keys, values):
if k:
self.d[k] = self.process(k, v)
def process(self, key, value):
action = getattr(self, 'proc_'+key, self.default_action)
return action(value)
class MultiLoaderPlus(MultiLoader):
def load(self, values):
pairs = []
for ks, v in zip(self.keys, values):
if isinstance(ks, basestring):
self.d[ks] = self.process(ks, v)
else:
for k in ks:
self.d[k] = self.process(k, v)
class MyLoader(MultiLoaderPlus):
keys = ['aaa', ('bbb', 'ccc'), 'ddd']
default_action = lambda s, x: x.upper()
proc_ccc = lambda s, x: x*5
if __name__ == '__main__':
loader = MyLoader()
loader.load(['foo', 'bar', 'baz'])
print loader.d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.