Skip to content

Commit 9d7e5e4

Browse files
committed
Fixed Memento
Memento pattern now works as it should, added test to the makefile so that it can run all the python scripts.
1 parent 759fa71 commit 9d7e5e4

3 files changed

Lines changed: 51 additions & 41 deletions

File tree

Adapter.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,6 @@ class Adapter(object):
77
def __init__(self):
88
print("initalised")
99

10-
def test(value, message):
11-
""" This asserts against the value printing for either pass or fail. """
12-
assert value, message
13-
print("PASSED: %s" %(message))
14-
print("")
15-
1610
#==============================================================================
1711
if (__name__ == "__main__"):
18-
test(True, "This is a passing test")
19-
test(False, "This is a failing test")
12+
pass

Memento.py

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,57 @@
11
#!/usr/bin/env python
22
# Written by: DGC
3-
# undo
43

54
import copy
65

76
#==============================================================================
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):
917

1018
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)
1325

1426
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))
2130

2231
#==============================================================================
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):
3533

36-
def append(self, item):
37-
self.history.append(copy.deepcopy(item))
34+
def __init__(self):
35+
super(Data, self).__init__()
36+
self.numbers = []
3837

3938
#==============================================================================
4039
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))

makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# Targets
22
TEX_NAME = Design_Patterns_In_Python
33

4+
#==============================================================================
5+
test: FRC
6+
@for program in *.py; \
7+
do \
8+
echo $$program; \
9+
./$$program; \
10+
echo ""; \
11+
done
12+
413
include $(DROPBOX)/Coding/MakeFiles/latex.mk
514
# override OUTPUT_NAME here

0 commit comments

Comments
 (0)