Created
August 15, 2013 19:05
-
-
Save groner/6243774 to your computer and use it in GitHub Desktop.
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
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