Skip to content

Instantly share code, notes, and snippets.

@ChristinaMeno
Created June 30, 2011 16:03
Show Gist options
  • Save ChristinaMeno/1056549 to your computer and use it in GitHub Desktop.
Save ChristinaMeno/1056549 to your computer and use it in GitHub Desktop.
def _build_coherent_name(field, parent):
return parent and '.'.join((parent, field)) or field
def _verify_fields_recursive(expected, data, parent=None):
analysis = {}
for field, field_info in expected.items():
coherent_name = _build_coherent_name(field, parent)
status = field in data and 'found' or 'missing',
required = field_info.get('required') and 'required' or 'optional',
analysis[coherent_name] = {
'class': '%(status)s_%(required)s' % locals(),
'message': "%(status)s %(required)s field '%(coherent_name)s'" % locals(),
}
if field in data and '_keys' in field_info:
analysis.update(_verify_fields_recursive(field_info['_keys'], data[field],
parent=coherent_name))
for field in set(data) - set(analysis):
coherent_name = _build_coherent_name(field, parent)
analysis[coherent_name] = {
'class': 'unknown_field',
'message': "'%s' is an unknown field." % coherent_name,
}
return analysis
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment