Skip to content

Instantly share code, notes, and snippets.

@Kalimaha
Created May 22, 2017 09:17
Show Gist options
  • Select an option

  • Save Kalimaha/9c0da18aad7450b372de983fb9225074 to your computer and use it in GitHub Desktop.

Select an option

Save Kalimaha/9c0da18aad7450b372de983fb9225074 to your computer and use it in GitHub Desktop.
Decorators Experiment
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