Created
June 2, 2013 05:02
-
-
Save Suor/5692674 to your computer and use it in GitHub Desktop.
apply_schema.py
This file contains hidden or 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
from funcy import join | |
def apply_el(data, el): | |
if isinstance(el, str): | |
return {el: data.get(el)} | |
elif isinstance(el, dict): | |
return { | |
k: apply_schema(data.get(k), v) | |
for k, v in el.items() | |
} | |
elif isinstance(el, list): | |
return [apply_schema(item, el) for item in data] | |
else: | |
raise ValueError("Don't know how to apply schema element %s" % el) | |
def apply_schema(data, schema): | |
return join(apply_el(data, el) for el in schema) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment