utilClass.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # -*- coding: utf-8 -*-
  2. """
  3. -------------------------------------------------
  4. File Name: utilClass.py
  5. Description : tool class
  6. Author : JHao
  7. date: 2016/12/3
  8. -------------------------------------------------
  9. Change Activity:
  10. 2016/12/3: Class LazyProperty
  11. 2016/12/4: rewrite ConfigParser
  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. from ConfigParser import ConfigParser
  30. class ConfigParse(ConfigParser):
  31. """
  32. rewrite ConfigParser, for support upper option
  33. """
  34. def __init__(self):
  35. ConfigParser.__init__(self)
  36. def optionxform(self, optionstr):
  37. return optionstr
  38. class Singleton(type):
  39. """
  40. Singleton Metaclass
  41. """
  42. _inst = {}
  43. def __call__(cls, *args, **kwargs):
  44. if cls not in cls._inst:
  45. cls._inst[cls] = super(Singleton, cls).__call__(*args)
  46. return cls._inst[cls]