Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import warnings
- from copy import deepcopy
- from operator import setitem
- UNDEFINED = object()
- constraints = [
- ('age must be atleast 18', lambda new_state: new_state.get('age', UNDEFINED) is UNDEFINED or new_state['age'] >= 18),
- ]
- state = {}
- changes = [lambda state: setitem(state, 'age', 16)]
- new_state = deepcopy(state)
- for change in changes:
- change(new_state)
- commit = True
- for description, check in constraints:
- if not check(new_state):
- commit = False
- warnings.warn('constraint "{description}" broken'.format(description=description))
- if commit:
- for change in changes:
- change(state)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement