Created
May 19, 2017 01:44
-
-
Save Kalimaha/ed016f431b3103b05ade6feb11885926 to your computer and use it in GitHub Desktop.
Python 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
| import inspect | |
| class Pact(object): | |
| has_pact_with = None | |
| state = None | |
| def __str__(self): | |
| return "\n==> Provider Pact\n\tHas pact with: " + str(self.has_pact_with) + "\n\tState: " + str(self.state) | |
| p = Pact() | |
| def setup_state(state): | |
| print('State: ' + state) | |
| p.state = state | |
| def setup_has_pact_with(has_pact_with_value): | |
| print('Has Pact with: ' + has_pact_with_value) | |
| p.has_pact_with = has_pact_with_value | |
| def state(state_value): | |
| def decorator(fn): | |
| def decorated(*args,**kwargs): | |
| if not state_value: raise Exception('Missing state') | |
| setup_state(state_value) | |
| return fn(*args,**kwargs) | |
| return decorated | |
| return decorator | |
| def has_pact_with(has_pact_with_value): | |
| def decorator(fn): | |
| def decorated(*args,**kwargs): | |
| if not has_pact_with_value: raise Exception('Missing has_pact_with') | |
| setup_has_pact_with(has_pact_with_value) | |
| return fn(*args,**kwargs) | |
| return decorated | |
| return decorator | |
| @has_pact_with('Library App') | |
| @state('some books exist') | |
| def test_args(): | |
| print(p) | |
| assert False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment