Created
April 18, 2016 17:52
-
-
Save aeppert/ae0c799f6f9c0156433fce6f2e978409 to your computer and use it in GitHub Desktop.
Service Decorators
This file contains hidden or 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
| from functools import partial | |
| class test: | |
| def __init__(self): | |
| pass | |
| @staticmethod | |
| def stop_service(name): | |
| print 'stopping service - %s' % (name) | |
| @staticmethod | |
| def start_service(name): | |
| print 'starting service - %s' % (name) | |
| def _pseudo_decor(fun, argument): | |
| def ret_fun(*args, **kwargs): | |
| test.stop_service(argument) | |
| ret = fun(*args, **kwargs) | |
| test.start_service(argument) | |
| return ret | |
| return ret_fun | |
| t1_decorator = partial(_pseudo_decor, argument='t1') | |
| t2_decorator = partial(_pseudo_decor, argument='t2') | |
| @t1_decorator | |
| @t2_decorator | |
| def add(self, name): | |
| print 'adding service %s' % (name) | |
| if __name__ == '__main__': | |
| t = test() | |
| t.add('bar') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment