Skip to content

Instantly share code, notes, and snippets.

@groner
Created August 15, 2013 19:05
Show Gist options
  • Save groner/6243774 to your computer and use it in GitHub Desktop.
Save groner/6243774 to your computer and use it in GitHub Desktop.
class Tuple(Dictifier):
def __init__(self, ordered_field_types):
super(Tuple, self).__init__()
self.ordered_field_types = tuple(ordered_field_types)
def dictify(self, value, **kw):
if value is None:
return None
try:
if len(value) != len(self.ordered_field_types):
raise Invalid('bad_len')
except TypeError:
raise Invalid('not_iterable')
return tuple(
f.dictify(v, **kw)
for f,v in zip(self.ordered_field_types, value) )
def undictify(self, value, **kw):
if value is None:
return None
try:
if len(value) != len(self.ordered_field_types):
raise Invalid('bad_len')
except TypeError:
raise Invalid('not_iterable')
return tuple(
f.undictify(v, **kw)
for f,v in zip(self.ordered_field_types, value) )
def validate(self, value, **kw):
error_agg = ErrorAggregator(autoraise = kw.get('fail_early', False))
with error_agg.checking():
super(SchemaMapping, self).validate(value, **kwargs)
try:
if len(value) != len(self.ordered_field_types):
raise Invalid('bad_len')
except TypeError:
raise Invalid('not_iterable')
for field,value in zip(self.ordered_field_types, value):
with error_agg.checking_sub(field):
field.validate(value)
error_agg.raise_if_any()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment