Skip to content

Instantly share code, notes, and snippets.

@SimplyAhmazing
Created October 12, 2016 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SimplyAhmazing/1ad8f8b5d9e09f8dc3c1df3de92a761f to your computer and use it in GitHub Desktop.
Save SimplyAhmazing/1ad8f8b5d9e09f8dc3c1df3de92a761f to your computer and use it in GitHub Desktop.
import copy
from pprint import pprint as pp
def Memento(obj, deep=False):
state = (copy.copy, copy.deepcopy)[bool(deep)](obj.__dict__)
def Restore():
obj.__dict__.clear()
obj.__dict__.update(state)
return Restore
class Transaction:
"""A transaction guard. This is realy just
syntactic suggar arount a memento closure.
"""
deep = False
def __init__(self, *targets):
self.targets = targets
self.Commit()
def Commit(self):
self.states = [Memento(target, self.deep) for target in self.targets]
def Rollback(self):
for state in self.states:
state()
class GamePlayer:
health_points = 100
def drink_potion(self):
self.health_points += 10
def take_damage(self):
self.health_points -= 20
def power_goes_out(self):
self.health_points = self.health_points/0
if __name__ == '__main__':
zelda = GamePlayer()
t = Transaction(zelda)
# import pdb; pdb.set_trace()
try:
print('Zelda is about to go through the Marshlands')
zelda.take_damage()
zelda.take_damage()
zelda.take_damage()
zelda.drink_potion()
t.Commit()
print('Zelda goes through the Marshlands')
except:
t.Rollback()
pass
print('Zelda looks like: ')
pp(zelda.__dict__)
try:
print('Zelda going through the Dungeon')
zelda.drink_potion()
zelda.drink_potion()
zelda.power_goes_out()
t.Commit()
print('Zelda goes through the dungeon')
except:
t.Rollback()
pass
print('Zelda looks like: ')
pp(zelda.__dict__)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment