Skip to content

Commit e0ab1b3

Browse files
committed
Add save() and restore() capability
1 parent c916cc9 commit e0ab1b3

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

pubsub/pubsub.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from heapq import merge
1414
from bisect import bisect
1515
from sys import intern
16+
import pickle
1617
import re
1718

1819
User = str
@@ -101,3 +102,12 @@ def age(post: Post) -> str:
101102
divisor, unit = time_units[bisect(time_unit_cuts, seconds)]
102103
units = seconds // divisor
103104
return '%d %s ago' % (units, unit + ('' if units==1 else 's'))
105+
106+
def save():
107+
with open('pubsub.pickle', 'wb') as f:
108+
pickle.dump([posts, user_posts, hashtag_index, following, followers, user_info], f)
109+
110+
def restore():
111+
global posts, user_posts, hashtag_index, following, followers, user_info
112+
with open('pubsub.pickle', 'rb') as f:
113+
posts, user_posts, hashtag_index, following, followers, user_info = pickle.load(f)

pubsub/session.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@
3434
follow('raymondh', followed_user='barry')
3535

3636
if __name__ == '__main__':
37+
import pubsub
38+
39+
pubsub.save()
40+
del pubsub.posts, pubsub.user_posts, pubsub.hashtag_index,
41+
del pubsub.followers, pubsub.following, pubsub.user_info
42+
pubsub.restore()
43+
3744
from pubsub import posts, followers, following, user_info, hashtag_index
3845
from pubsub import posts_by_user, posts_for_user, search
3946
from pubsub import get_followers, get_followed, get_user, check_user, age

0 commit comments

Comments
 (0)