model.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # -*- coding: utf-8 -*-
  2. import re
  3. from .exceptions import RequiredArgumentException
  4. class TeaModel(object):
  5. def validate(self):
  6. pass
  7. def to_map(self):
  8. pass
  9. def from_map(self, m=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 Exception('%s is exceed max-length: %s' % (
  19. prop_name, max_length
  20. ))
  21. @staticmethod
  22. def validate_min_length(prop, prop_name, min_length):
  23. if len(prop) < min_length:
  24. raise Exception('%s is less than min-length: %s' % (
  25. prop_name, min_length
  26. ))
  27. @staticmethod
  28. def validate_pattern(prop, prop_name, pattern):
  29. match_obj = re.search(pattern, str(prop), re.M | re.I)
  30. if not match_obj:
  31. raise Exception('%s is not match: %s' % (
  32. prop_name, pattern
  33. ))
  34. @staticmethod
  35. def validate_maximum(num, prop_name, maximum):
  36. if num > maximum:
  37. raise Exception('%s is greater than the maximum: %s' % (
  38. prop_name, maximum
  39. ))
  40. @staticmethod
  41. def validate_minimum(num, prop_name, minimum):
  42. if num < minimum:
  43. raise Exception('%s is less than the minimum: %s' % (
  44. prop_name, minimum
  45. ))
  46. def __str__(self):
  47. s = self.to_map()
  48. if s:
  49. return str(s)
  50. else:
  51. return object.__str__(self)