Last active
May 12, 2017 07:44
-
-
Save oglops/3f36be7222955c42e41caa9fd4f5f3e8 to your computer and use it in GitHub Desktop.
test classmethod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from abc import ABCMeta, abstractmethod | |
from functools import wraps | |
import time | |
class ContextDecorator(object): | |
_built = False | |
def __init__(self, *args, **kwargs): | |
self.args = args | |
self.__dict__.update(kwargs) | |
self._contextExists = False | |
self._rebuild = kwargs.get('rebuild', False) | |
@abstractmethod | |
def _build(self): | |
pass | |
@abstractmethod | |
def _destroy(self): | |
pass | |
def clear_built(self): | |
ContextDecorator._built = False | |
def __enter__(self): | |
if not ContextDecorator._built or self._rebuild: | |
try: | |
self._build() | |
except: | |
self.clear_built() | |
raise | |
ContextDecorator._built = True | |
print 'call _build first time' | |
else: | |
print 'skip _build' | |
self._contextExists = True | |
return self | |
def __exit__(self, typ, val, traceback): | |
if not self._contextExists or self._rebuild: | |
try: | |
self._destroy() | |
except: | |
self.clear_built() | |
raise | |
self.clear_built() | |
print 'call _destroy first time' | |
else: | |
print 'skip _destroy' | |
self._contextExists = False | |
def __call__(self, f): | |
self.function = f | |
@wraps(f) | |
def wrapper(*args, **kw): | |
with self: | |
try: | |
return f(*args, **kw) | |
except: | |
self.clear_built() | |
raise | |
return wrapper | |
class CustomContext(ContextDecorator): | |
def __init__(self, *args, **kwargs): | |
super(CustomContext, self).__init__(*args, **kwargs) | |
def _build(self): | |
pass | |
def _destroy(self): | |
pass | |
print 'context manager test' | |
try: | |
with CustomContext(): | |
for i in range(3): | |
with CustomContext(): | |
time.sleep(0.01) | |
except: | |
print 'caught exception' | |
print CustomContext._built | |
print '-' * 10 | |
print 'context manager test always rebuild' | |
try: | |
with CustomContext(rebuild=True): | |
for i in range(3): | |
with CustomContext(rebuild=True): | |
time.sleep(0.01) | |
except: | |
print 'caught exception' | |
print CustomContext._built | |
print '-' * 10 | |
print 'decorator test', CustomContext._built, ContextDecorator._built | |
# print CustomContext._built | |
@CustomContext() | |
@CustomContext() | |
def test(): | |
print 'in side test func' | |
try: | |
test() | |
except: | |
print 'caught exception', CustomContext._built | |
print '-' * 10 | |
print 'decorator test rebuild=True', CustomContext._built, ContextDecorator._built | |
@CustomContext(rebuild=True) | |
@CustomContext(rebuild=True) | |
def test1(): | |
print 'in side test func' | |
try: | |
test1() | |
except: | |
print 'caught exception', CustomContext._built | |
# raise |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment