utilClass.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. """
  4. -------------------------------------------------
  5. File Name: utilClass.py
  6. Description : tool class
  7. Author : JHao
  8. date: 2016/12/3
  9. -------------------------------------------------
  10. Change Activity:
  11. 2016/12/3: Class LazyProperty
  12. -------------------------------------------------
  13. """
  14. __author__ = 'JHao'
  15. class LazyProperty(object):
  16. """
  17. LazyProperty
  18. explain: http://www.spiderpy.cn/blog/5/
  19. """
  20. def __init__(self, func):
  21. self.func = func
  22. def __get__(self, instance, owner):
  23. if instance is None:
  24. return self
  25. else:
  26. value = self.func(instance)
  27. setattr(instance, self.func.__name__, value)
  28. return value
  29. class Singleton(type):
  30. """
  31. Singleton Metaclass
  32. """
  33. _inst = {}
  34. def __call__(cls, *args, **kwargs):
  35. if cls not in cls._inst:
  36. cls._inst[cls] = super(Singleton, cls).__call__(*args)
  37. return cls._inst[cls]