Last active
December 16, 2015 16:08
Revisions
-
kirpit revised this gist
Aug 21, 2013 . 1 changed file with 9 additions and 9 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -7,8 +7,9 @@ class FieldsDict(dict): FIELDS = None REQUIRED = None def __init__(self, seq=None, **kwargs): # create a single dict seq = seq or {} mapping = dict(seq, **kwargs) # make sure all fields are expected try: @@ -20,14 +21,13 @@ def __init__(self, seq, **kwargs): # and check the required fields if self.REQUIRED: try: req = next(r for r in self.REQUIRED if r not in mapping) except StopIteration: pass else: raise ValueError("The field '%s' is required" % req) super(FieldsDict, self).__init__(mapping) # use as: class Person(FieldsDict): FIELDS = ( @@ -39,16 +39,16 @@ class Person(FieldsDict): 'birthday', ) REQUIRED = ( 'title', 'first_name', 'last_name', ) # should give ValueError("The field 'title' is required") me = Person(first_name='Roy') # should give ValueError("The field 'last_name' is required") me = Person({'title': 'MR', 'first_name': 'Roy'}) # should give ValueError("The field 'level' was not expected") -
kirpit revised this gist
Apr 30, 2013 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -8,8 +8,8 @@ class FieldsDict(dict): REQUIRED = None def __init__(self, seq, **kwargs): # create a single dict mapping = dict(seq, **kwargs) # make sure all fields are expected try: unexpected = next(key for key in mapping if key not in self.FIELDS) -
kirpit revised this gist
Apr 29, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -9,7 +9,7 @@ class FieldsDict(dict): def __init__(self, seq, **kwargs): # clear empty fields mapping = dict([(k, v) for k, v in dict(seq, **kwargs).iteritems()]) # make sure all fields are expected try: unexpected = next(key for key in mapping if key not in self.FIELDS) -
kirpit renamed this gist
Apr 26, 2013 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
kirpit created this gist
Apr 25, 2013 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,60 @@ #! /usr/bin/env python from abc import ABCMeta class FieldsDict(dict): __metaclass__ = ABCMeta FIELDS = None REQUIRED = None def __init__(self, seq, **kwargs): # clear empty fields mapping = dict([(k, v) for k, v in dict(seq, **kwargs).iteritems() if v]) # make sure all fields are expected try: unexpected = next(key for key in mapping if key not in self.FIELDS) except StopIteration: pass else: raise ValueError("The field '%s' was not expected" % unexpected) # and check the required fields if self.REQUIRED: try: req = next(r for r in self.REQUIRED if r[0] not in mapping) except StopIteration: pass else: raise ValueError(req[1]) super(FieldsDict, self).__init__(mapping) # use as: class Person(FieldsDict): FIELDS = ( 'title', 'first_name', 'last_name', 'email', 'phone', 'birthday', ) REQUIRED = ( ('title', "Title is required"), ('first_name', "First name is required"), ('last_name', "Last name is required"), ) # should give ValueError("Title is required") me = Person(first_name='Roy') # should give ValueError("Last name is required") me = Person({'title': 'MR', 'first_name': 'Roy'}) # should give ValueError("The field 'level' was not expected") me = Person({'level': 'PRO', 'first_name': 'Roy'}) # should be fine me = Person({'title': 'MR', 'first_name': 'Roy', 'last_name': 'Enjoy', 'email': '[email protected]'}) # or that is fine too me = Person(title='MR', first_name='Roy', last_name='Enjoy', phone=123456)