utilClass.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. from ConfigParser import ConfigParser
  31. class ConfigParse(ConfigParser):
  32. """
  33. rewrite ConfigParser, for support upper option
  34. """
  35. def __init__(self):
  36. ConfigParser.__init__(self)
  37. def optionxform(self, optionstr):
  38. return optionstr
  39. class Singleton(type):
  40. """
  41. Singleton Metaclass
  42. """
  43. _inst = {}
  44. def __call__(cls, *args, **kwargs):
  45. if cls not in cls._inst:
  46. cls._inst[cls] = super(Singleton, cls).__call__(*args)
  47. return cls._inst[cls]