Created
September 16, 2014 10:33
-
-
Save alexmuller/391f9984d2a2829afb1b to your computer and use it in GitHub Desktop.
WTForms serialisation of modules
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
def select_modules(dictionary): | |
return {k: v for k, v in mydict.items() if k.startswith('modules-')} | |
def module_key(item): | |
return item[0].split('-')[1] | |
def sort_modules(modules): | |
return sorted(modules.iteritems(), key=module_key) | |
def group_modules(modules): | |
return (module[1] for module in groupby(modules, key=module_key)) | |
def serialise_group(module): | |
return dict([(key.split('-')[2], value) for key, value in module]) | |
map(serialise_group, group_modules(sort_modules(select_modules(modules)))) |
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
d = {'modules-0-k1': 'one', 'modules-0-k2': 'two', 'modules-1-k1': 'three', 'modules-1-k2': 'four'} | |
output = defaultdict(dict) | |
for k, v in d.items(): | |
ks = k.split('-') | |
output[ks[1]][ks[2]] = v | |
[v for k, v in sorted(output.items())] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment