Created
May 22, 2017 09:17
-
-
Save Kalimaha/9c0da18aad7450b372de983fb9225074 to your computer and use it in GitHub Desktop.
Decorators Experiment
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
| def state(state_value): | |
| def decorate(function): | |
| function.state = state_value | |
| return function | |
| return decorate | |
| class ConsumerTest(object): | |
| @property | |
| def states(self): | |
| for attr in dir(self): | |
| obj = getattr(self, attr) | |
| if callable(obj) and hasattr(obj, 'state'): | |
| yield obj | |
| class MyTest(ConsumerTest): | |
| @state('some books exist') | |
| def some_books_exist(self): | |
| print('THERE ARE FIVE BOOKS :)') | |
| @state('there are no books') | |
| def no_books_exist(self): | |
| print('THERE ARE ZERO BOOKS! :(') | |
| def test_annotations(): | |
| for foo in MyTest().states: | |
| print(foo.state) | |
| print(foo()) | |
| print('===') | |
| assert False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment