speaklater.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. class LazyString(object):
  2. def __init__(self, func, *args, **kwargs):
  3. self._func = func
  4. self._args = args
  5. self._kwargs = kwargs
  6. def __getattr__(self, attr):
  7. if attr == "__setstate__":
  8. raise AttributeError(attr)
  9. string = str(self)
  10. if hasattr(string, attr):
  11. return getattr(string, attr)
  12. raise AttributeError(attr)
  13. def __repr__(self):
  14. return "l'{0}'".format(str(self))
  15. def __str__(self):
  16. return str(self._func(*self._args, **self._kwargs))
  17. def __len__(self):
  18. return len(str(self))
  19. def __getitem__(self, key):
  20. return str(self)[key]
  21. def __iter__(self):
  22. return iter(str(self))
  23. def __contains__(self, item):
  24. return item in str(self)
  25. def __add__(self, other):
  26. return str(self) + other
  27. def __radd__(self, other):
  28. return other + str(self)
  29. def __mul__(self, other):
  30. return str(self) * other
  31. def __rmul__(self, other):
  32. return other * str(self)
  33. def __lt__(self, other):
  34. return str(self) < other
  35. def __le__(self, other):
  36. return str(self) <= other
  37. def __eq__(self, other):
  38. return str(self) == other
  39. def __ne__(self, other):
  40. return str(self) != other
  41. def __gt__(self, other):
  42. return str(self) > other
  43. def __ge__(self, other):
  44. return str(self) >= other
  45. def __html__(self):
  46. return str(self)
  47. def __hash__(self):
  48. return hash(str(self))
  49. def __mod__(self, other):
  50. return str(self) % other
  51. def __rmod__(self, other):
  52. return other + str(self)