Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. import warnings
  2.  
  3. from copy import deepcopy
  4. from operator import setitem
  5.  
  6. UNDEFINED = object()
  7.  
  8. constraints = [
  9. ('age must be atleast 18', lambda new_state: new_state.get('age', UNDEFINED) is UNDEFINED or new_state['age'] >= 18),
  10. ]
  11. state = {}
  12. changes = [lambda state: setitem(state, 'age', 16)]
  13.  
  14. new_state = deepcopy(state)
  15. for change in changes:
  16. change(new_state)
  17.  
  18.  
  19. commit = True
  20. for description, check in constraints:
  21. if not check(new_state):
  22. commit = False
  23. warnings.warn('constraint "{description}" broken'.format(description=description))
  24.  
  25.  
  26. if commit:
  27. for change in changes:
  28. change(state)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement