Skip to content

Instantly share code, notes, and snippets.

@xuanyu-h
Last active December 22, 2017 02:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xuanyu-h/95a30b613c7471dcc06c7fcde1045aea to your computer and use it in GitHub Desktop.
Save xuanyu-h/95a30b613c7471dcc06c7fcde1045aea to your computer and use it in GitHub Desktop.
python context??
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()
@xuanyu-h
Copy link
Author

import config

config.settings(timeout="0.1")
print(config.settings.timeout)

with config.settings(timeout="0.2"):
    print(config.settings.timeout)

print(config.settings.timeout)

###################################
# enter function: __init__, (<config.Settings object at 0x108854e48>,), {}
# enter function: __getattribute__, (<config.Settings object at 0x108854e48>, '_singleton'), {}

# enter function: __call__, (<config.Settings object at 0x108854e48>,), {'timeout': '0.1'}
# enter function: __getattribute__, (<config.Settings object at 0x108854e48>, '__class__'), {}
# enter function: __init__, (<config.Settings object at 0x108827d68>,), {}
# enter function: __getattribute__, (<config.Settings object at 0x108827d68>, '_singleton'), {}
# enter function: __getattribute__, (<config.Settings object at 0x108854e48>, '__dict__'), {}
# enter function: __getattribute__, (<config.Settings object at 0x108854e48>, '_Settings__cache'), {}
# enter function: __getattribute__, (<config.Settings object at 0x108854e48>, '__attrs__'), {}
# enter function: __getattribute__, (<config.Settings object at 0x108854e48>, '__dict__'), {}
# enter function: __getattribute__, (<config.Settings object at 0x108854e48>, 'timeout'), {}
# 0.1
# enter function: __call__, (<config.Settings object at 0x108854e48>,), {'timeout': '0.2'}
# enter function: __getattribute__, (<config.Settings object at 0x108854e48>, '__class__'), {}
# enter function: __init__, (<config.Settings object at 0x108827d68>,), {}
# enter function: __getattribute__, (<config.Settings object at 0x108827d68>, '_singleton'), {}
# enter function: __getattribute__, (<config.Settings object at 0x108854e48>, '__dict__'), {}
# enter function: __getattribute__, (<config.Settings object at 0x108854e48>, '_Settings__cache'), {}
# enter function: __getattribute__, (<config.Settings object at 0x108854e48>, '__attrs__'), {}
# enter function: __getattribute__, (<config.Settings object at 0x108854e48>, '__dict__'), {}
# why
# enter function: __enter__, (<config.Settings object at 0x108827d68>,), {}
# enter function: __getattribute__, (<config.Settings object at 0x108854e48>, 'timeout'), {}
# 0.2
# enter function: __exit__, (<config.Settings object at 0x108827d68>, None, None, None), {}
# enter function: __getattribute__, (<config.Settings object at 0x108827d68>, '__dict__'), {}
# enter function: __getattribute__, (<config.Settings object at 0x108827d68>, '_Settings__cache'), {}
# why
# enter function: __getattribute__, (<config.Settings object at 0x108854e48>, 'timeout'), {}
# 0.1
###################################

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment