apache_conf python上下文?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了apache_conf python上下文?相关的知识,希望对你有一定的参考价值。

from functools import wraps


def print_method_name(f):
    @wraps(f)
    def decorated(*args, **kwargs):
        print("enter function: {0}, {1}, {2}".format(f.__name__, args, kwargs))
        return f(*args, **kwargs)
    return decorated


class Settings(object):
    _singleton = {}

    # attributes with defaults
    __attrs__ = ('timeout', 'verbose')

    @print_method_name
    def __init__(self, **kwargs):
        super(Settings, self).__init__()

        self.__dict__ = self._singleton

    @print_method_name
    def __call__(self, *args, **kwargs):
        # new instance of class to call
        r = self.__class__()

        # cache previous settings for __exit__
        r.__cache = self.__dict__.copy()
        map(self.__cache.setdefault, self.__attrs__)

        # set new settings
        self.__dict__.update(*args, **kwargs)

        return r

    @print_method_name
    def __enter__(self):
        pass

    @print_method_name
    def __exit__(self, *args):
        # restore cached copy

        self.__dict__.update(self.__cache.copy())
        del self.__cache

    @print_method_name
    def __getattribute__(self, key):
        if key in object.__getattribute__(self, '__attrs__'):
            try:
                return object.__getattribute__(self, key)
            except AttributeError:
                return None
        return object.__getattribute__(self, key)


settings = Settings()

以上是关于apache_conf python上下文?的主要内容,如果未能解决你的问题,请参考以下文章