-
Notifications
You must be signed in to change notification settings - Fork 4
/
srp.py
47 lines (35 loc) · 997 Bytes
/
srp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class Journal:
def __init__(self):
self.entries = []
self.count = 0
def add_entry(self, text):
self.entries.append(f"{self.count}: {text}")
self.count += 1
def remove_entry(self, pos):
del self.entries[pos]
def __str__(self):
return "\n".join(self.entries)
# break SRP
def save(self, filename):
file = open(filename, "w")
file.write(str(self))
file.close()
def load(self, filename):
pass
def load_from_web(self, uri):
pass
class PersistenceManager:
def save_to_file(journal, filename):
file = open(filename, "w")
file.write(str(journal))
file.close()
j = Journal()
j.add_entry("I cried today.")
j.add_entry("I ate a bug.")
print(f"Journal entries:\n{j}\n")
p = PersistenceManager()
file = r'c:\temp\journal.txt'
p.save_to_file(j, file)
# verify!
with open(file) as fh:
print(fh.read())