utilClass.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. 2016/12/4: rewrite ConfigParser
  13. -------------------------------------------------
  14. """
  15. __author__ = 'JHao'
  16. class LazyProperty(object):
  17. """
  18. LazyProperty
  19. explain: http://www.spiderpy.cn/blog/5/
  20. """
  21. def __init__(self, func):
  22. self.func = func
  23. def __get__(self, instance, owner):
  24. if instance is None:
  25. return self
  26. else:
  27. value = self.func(instance)
  28. setattr(instance, self.func.__name__, value)
  29. return value
  30. try:
  31. from configparser import ConfigParser # py3
  32. except:
  33. from ConfigParser import ConfigParser # py2
  34. class ConfigParse(ConfigParser):
  35. """
  36. rewrite ConfigParser, for support upper option
  37. """
  38. def __init__(self):
  39. ConfigParser.__init__(self)
  40. def optionxform(self, optionstr):
  41. return optionstr
  42. class Singleton(type):
  43. """
  44. Singleton Metaclass
  45. """
  46. _inst = {}
  47. def __call__(cls, *args, **kwargs):
  48. if cls not in cls._inst:
  49. cls._inst[cls] = super(Singleton, cls).__call__(*args)
  50. return cls._inst[cls]