Created
September 9, 2013 19:47
-
-
Save dwcaraway/6500540 to your computer and use it in GitHub Desktop.
augment_data function in dictization_functions.py. I found the ##fill junk and extras portion confusing
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 augment_data(data, schema): | |
'''add missing, extras and junk data''' | |
flattented_schema = flatten_schema(schema) | |
key_combinations = get_all_key_combinations(data, flattented_schema) | |
full_schema = make_full_schema(data, schema) | |
new_data = copy.copy(data) | |
## fill junk and extras | |
for key, value in new_data.items(): | |
if key in full_schema: | |
continue | |
## check if any thing naugthy is placed against subschemas | |
initial_tuple = key[::2] | |
if initial_tuple in [initial_key[:len(initial_tuple)] | |
for initial_key in flattented_schema]: | |
if data[key] <> []: | |
raise DataError('Only lists of dicts can be placed against ' | |
'subschema %s, not %s' % (key,type(data[key]))) | |
if key[:-1] in key_combinations: | |
extras_key = key[:-1] + ('__extras',) | |
extras = new_data.get(extras_key, {}) | |
extras[key[-1]] = value | |
new_data[extras_key] = extras | |
else: | |
junk = new_data.get(("__junk",), {}) | |
junk[key] = value | |
new_data[("__junk",)] = junk | |
new_data.pop(key) | |
## add missing | |
for key, value in full_schema.items(): | |
if key not in new_data and not key[-1].startswith("__"): | |
new_data[key] = missing | |
return new_data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment