Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ld( list):
- def __init__(self, x):
- list.__init__(self, x)
- self.__init_check()
- def __init_check(self):
- for record in self:
- if isinstance( record, dict) and "a" in record:
- pass
- else:
- raise TypeError("not all entries are dictionaries or have the key 'a'")
- return
- tt = ld( [{"a": 1, "b":2}, {"a":4}, {"a":6, "c":67}])
- type( tt)
- ld( [{"w":1}])
- ld( [1,2,3])
- type( tt[:2])
- class ld( list):
- def __init__(self, x, safe=True):
- list.__init__(self, x)
- self.__init_check( safe)
- def __init_check(self, is_safe):
- if not is_safe:
- return
- for record in self:
- if isinstance( record, dict) and "a" in record:
- pass
- else:
- raise TypeError("not all entries are dictionaries or have the key 'a'")
- return
- def __getslice__(self, i, j):
- return ld( list.__getslice__( self, i, j), safe=False)
- def make_verified_list(items):
- """
- :type items: list[object]
- :rtype: list[dict]
- """
- new_list = []
- for item in items:
- if not verify_item(item):
- raise new InvalidItemError(item)
- new_list.append(item)
- return new_list
- def verify_item(item):
- """
- :type item: object
- :rtype: bool
- """
- return isinstance(item, dict) and "a" in item
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement