46 lines
1.0 KiB
Python
46 lines
1.0 KiB
Python
# -*- coding: utf-8 -*-
|
||
# !/usr/bin/env python
|
||
"""
|
||
-------------------------------------------------
|
||
File Name: utilClass.py
|
||
Description : tool class
|
||
Author : JHao
|
||
date: 2016/12/3
|
||
-------------------------------------------------
|
||
Change Activity:
|
||
2016/12/3: Class LazyProperty
|
||
-------------------------------------------------
|
||
"""
|
||
__author__ = 'JHao'
|
||
|
||
|
||
class LazyProperty(object):
|
||
"""
|
||
LazyProperty
|
||
explain: http://www.spiderpy.cn/blog/5/
|
||
"""
|
||
|
||
def __init__(self, func):
|
||
self.func = func
|
||
|
||
def __get__(self, instance, owner):
|
||
if instance is None:
|
||
return self
|
||
else:
|
||
value = self.func(instance)
|
||
setattr(instance, self.func.__name__, value)
|
||
return value
|
||
|
||
|
||
class Singleton(type):
|
||
"""
|
||
Singleton Metaclass
|
||
"""
|
||
|
||
_inst = {}
|
||
|
||
def __call__(cls, *args, **kwargs):
|
||
if cls not in cls._inst:
|
||
cls._inst[cls] = super(Singleton, cls).__call__(*args)
|
||
return cls._inst[cls]
|