Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

_factories.py 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. from datetime import timedelta
  2. class _TzSingleton(type):
  3. def __init__(cls, *args, **kwargs):
  4. cls.__instance = None
  5. super(_TzSingleton, cls).__init__(*args, **kwargs)
  6. def __call__(cls):
  7. if cls.__instance is None:
  8. cls.__instance = super(_TzSingleton, cls).__call__()
  9. return cls.__instance
  10. class _TzFactory(type):
  11. def instance(cls, *args, **kwargs):
  12. """Alternate constructor that returns a fresh instance"""
  13. return type.__call__(cls, *args, **kwargs)
  14. class _TzOffsetFactory(_TzFactory):
  15. def __init__(cls, *args, **kwargs):
  16. cls.__instances = {}
  17. def __call__(cls, name, offset):
  18. if isinstance(offset, timedelta):
  19. key = (name, offset.total_seconds())
  20. else:
  21. key = (name, offset)
  22. instance = cls.__instances.get(key, None)
  23. if instance is None:
  24. instance = cls.__instances.setdefault(key,
  25. cls.instance(name, offset))
  26. return instance
  27. class _TzStrFactory(_TzFactory):
  28. def __init__(cls, *args, **kwargs):
  29. cls.__instances = {}
  30. def __call__(cls, s, posix_offset=False):
  31. key = (s, posix_offset)
  32. instance = cls.__instances.get(key, None)
  33. if instance is None:
  34. instance = cls.__instances.setdefault(key,
  35. cls.instance(s, posix_offset))
  36. return instance