model.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import re
  2. from .exceptions import RequiredArgumentException, ValidateException
  3. class TeaModel:
  4. _map = None
  5. def validate(self):
  6. pass
  7. def to_map(self):
  8. return self._map
  9. def from_map(self, map=None):
  10. pass
  11. @staticmethod
  12. def validate_required(prop, prop_name):
  13. if prop is None:
  14. raise RequiredArgumentException(prop_name)
  15. @staticmethod
  16. def validate_max_length(prop, prop_name, max_length):
  17. if len(prop) > max_length:
  18. raise ValidateException(f'{prop_name} is exceed max-length: {max_length}')
  19. @staticmethod
  20. def validate_min_length(prop, prop_name, min_length):
  21. if len(prop) < min_length:
  22. raise ValidateException(f'{prop_name} is less than min-length: {min_length}')
  23. @staticmethod
  24. def validate_pattern(prop, prop_name, pattern):
  25. match_obj = re.search(pattern, str(prop), re.M | re.I)
  26. if not match_obj:
  27. raise ValidateException(f'{prop_name} is not match: {pattern}')
  28. @staticmethod
  29. def validate_maximum(num, prop_name, maximum):
  30. if num > maximum:
  31. raise ValidateException(f'{prop_name} is greater than the maximum: {maximum}')
  32. @staticmethod
  33. def validate_minimum(num, prop_name, minimum):
  34. if num < minimum:
  35. raise ValidateException(f'{prop_name} is less than the minimum: {minimum}')
  36. def __str__(self):
  37. s = self.to_map()
  38. if s:
  39. return str(s)
  40. else:
  41. return object.__str__(self)