Created
December 15, 2011 17:43
-
-
Save johtso/1482026 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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