Guest User

Untitled

a guest
Mar 18th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. # TODO build a decorator
  2.  
  3.  
  4. def validate_dict(obj, template):
  5. if isinstance(template, dict):
  6. for k, v in obj.items():
  7. validate(v, template.get(k))
  8. elif not isinstance(obj, template):
  9. raise TypeError(f"Object {obj} is not instance of {template}")
  10.  
  11.  
  12. def validate_list(obj, template):
  13. if isinstance(template, list):
  14. for o in obj:
  15. validate(o, template[0])
  16. elif not isinstance(obj, template):
  17. raise TypeError(f"Object {obj} is not instance of {template}")
  18.  
  19.  
  20. def validate_obj(obj, template):
  21. if not isinstance(obj, template):
  22. raise TypeError(f"Object {obj} is not instance of {template}")
  23.  
  24.  
  25. def validate(obj, template):
  26. if isinstance(obj, dict):
  27. validate_dict(obj, template)
  28. elif isinstance(obj, list):
  29. validate_list(obj, template)
  30. else:
  31. validate_obj(obj, template)
Add Comment
Please, Sign In to add comment