Advertisement
Guest User

Untitled

a guest
Apr 18th, 2015
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. class ld( list):
  2. def __init__(self, x):
  3. list.__init__(self, x)
  4. self.__init_check()
  5.  
  6. def __init_check(self):
  7. for record in self:
  8. if isinstance( record, dict) and "a" in record:
  9. pass
  10. else:
  11. raise TypeError("not all entries are dictionaries or have the key 'a'")
  12. return
  13.  
  14. tt = ld( [{"a": 1, "b":2}, {"a":4}, {"a":6, "c":67}])
  15. type( tt)
  16.  
  17. ld( [{"w":1}])
  18. ld( [1,2,3])
  19.  
  20. type( tt[:2])
  21.  
  22. class ld( list):
  23. def __init__(self, x, safe=True):
  24. list.__init__(self, x)
  25. self.__init_check( safe)
  26.  
  27. def __init_check(self, is_safe):
  28. if not is_safe:
  29. return
  30. for record in self:
  31. if isinstance( record, dict) and "a" in record:
  32. pass
  33. else:
  34. raise TypeError("not all entries are dictionaries or have the key 'a'")
  35. return
  36.  
  37. def __getslice__(self, i, j):
  38. return ld( list.__getslice__( self, i, j), safe=False)
  39.  
  40. def make_verified_list(items):
  41. """
  42. :type items: list[object]
  43. :rtype: list[dict]
  44. """
  45. new_list = []
  46. for item in items:
  47. if not verify_item(item):
  48. raise new InvalidItemError(item)
  49. new_list.append(item)
  50. return new_list
  51.  
  52. def verify_item(item):
  53. """
  54. :type item: object
  55. :rtype: bool
  56. """
  57. return isinstance(item, dict) and "a" in item
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement