|
1 | 1 | #!/usr/bin/env python |
2 | 2 | # Written by: DGC |
3 | | -# undo |
4 | 3 |
|
5 | 4 | import copy |
6 | 5 |
|
7 | 6 | #============================================================================== |
8 | | -class something(object): |
| 7 | +class Memento(object): |
| 8 | + |
| 9 | + def __init__(self, data): |
| 10 | + # make a deep copy of every variable in the given class |
| 11 | + for attribute in vars(data): |
| 12 | + # mechanism for using properties without knowing their names |
| 13 | + setattr(self, attribute, copy.deepcopy(getattr(data, attribute))) |
| 14 | + |
| 15 | +#============================================================================== |
| 16 | +class Undo(object): |
9 | 17 |
|
10 | 18 | def __init__(self): |
11 | | - self.a = 0 |
12 | | - self.undo_helper = UndoHelper(self) |
| 19 | + # each instance keeps the latest saved copy so that there is only one |
| 20 | + # copy of each in memory |
| 21 | + self.__last = None |
| 22 | + |
| 23 | + def save(self): |
| 24 | + self.__last = Memento(self) |
13 | 25 |
|
14 | 26 | def undo(self): |
15 | | - #print("revert %i" %(self.undo_helper.revert().a)) |
16 | | - self = self.undo_helper.revert() |
17 | | - |
18 | | - def add(self, number): |
19 | | - self.a += number |
20 | | - self.undo_helper.append(self) |
| 27 | + for attribute in vars(self): |
| 28 | + # mechanism for using properties without knowing their names |
| 29 | + setattr(self, attribute, getattr(self.__last, attribute)) |
21 | 30 |
|
22 | 31 | #============================================================================== |
23 | | -class UndoHelper(object): |
24 | | - |
25 | | - def __init__(self, something): |
26 | | - self.history = [something] |
27 | | - |
28 | | - def revert(self): |
29 | | - #print("revert"), |
30 | | - #for i in self.history: |
31 | | - # print(i.a), |
32 | | - # print("") |
33 | | - print(self.history) |
34 | | - return self.history[0] |
| 32 | +class Data(Undo): |
35 | 33 |
|
36 | | - def append(self, item): |
37 | | - self.history.append(copy.deepcopy(item)) |
| 34 | + def __init__(self): |
| 35 | + super(Data, self).__init__() |
| 36 | + self.numbers = [] |
38 | 37 |
|
39 | 38 | #============================================================================== |
40 | 39 | if (__name__ == "__main__"): |
41 | | - s = something() |
42 | | - s.add(5) |
43 | | - print(s.a) |
44 | | - s.add(5) |
45 | | - print(s.a) |
46 | | - s.add(5) |
47 | | - print(s.a) |
48 | | - s.undo() |
49 | | - print(s.a) |
| 40 | + d = Data() |
| 41 | + repeats = 10 |
| 42 | + # add a number to the list in data repeat times |
| 43 | + print("Adding.") |
| 44 | + for i in range(repeats): |
| 45 | + print("0" + str(i) + " times: " + str(d.numbers)) |
| 46 | + d.save() |
| 47 | + d.numbers.append(i) |
| 48 | + d.save() |
| 49 | + print("10 times: " + str(d.numbers)) |
| 50 | + print("") |
| 51 | + |
| 52 | + # now undo repeat times |
| 53 | + print("Using Undo.") |
| 54 | + for i in range(repeats): |
| 55 | + print("0" + str(i) + " times: " + str(d.numbers)) |
| 56 | + d.undo() |
| 57 | + print("10 times: " + str(d.numbers)) |
0 commit comments