Package pypat :: Package behavioral :: Module memento
[hide private]
[frames] | no frames]

Source Code for Module pypat.behavioral.memento

 1  from copy import deepcopy 
2 3 4 -class Memento(object):
5 """ 6 Memento class as part of the Memento design pattern. 7 8 - External Usage documentation: U{https://github.com/tylerlaberge/PyPatterns/wiki/Behavioral-Pattern-Usage} 9 - External Memento Pattern documentation: U{https://en.wikipedia.org/wiki/Memento_pattern} 10 """
11 - def __init__(self, state):
12 """ 13 Initialize a new Memento instance. 14 15 @param state: The state to save in this Memento. 16 @type state: dict 17 """ 18 self.__state = state
19 20 @property
21 - def state(self):
22 return self.__state
23
24 25 -class Originator(object):
26 """ 27 Originator base class as part of the Memento design pattern. 28 29 - External Usage documentation: U{https://github.com/tylerlaberge/PyPatterns/wiki/Behavioral-Pattern-Usage} 30 - External Mediator Pattern documentation: U{https://en.wikipedia.org/wiki/Memento_pattern} 31 """
32 - def commit(self):
33 """ 34 Commit this objects state to a memento. 35 36 @return: A memento instance with this objects state. 37 """ 38 return Memento(deepcopy(self.__dict__))
39
40 - def rollback(self, memento):
41 """ 42 Rollback this objects state to a previous state. 43 44 @param memento: The memento object holding the state to rollback to. 45 @type memento: Memento 46 """ 47 self.__dict__ = memento.state
48