Merge pull request 'pickle_idea.py: Make funciton unfamiliar() more robust' (#181) from Bug518-Hui into Alpha-snapshot20240618

Reviewed-on: #181
Bug511-Hui
mrlan 2024-09-03 11:43:10 +08:00
commit 370a215d08
1 changed files with 8 additions and 5 deletions

View File

@ -6,6 +6,7 @@
# Purpose: dictionary & pickle as a simple means of database. # Purpose: dictionary & pickle as a simple means of database.
# Task: incorporate the functions into wordfreqCMD.py such that it will also show cumulative frequency. # Task: incorporate the functions into wordfreqCMD.py such that it will also show cumulative frequency.
import os
import pickle import pickle
from datetime import datetime from datetime import datetime
@ -55,11 +56,13 @@ def save_frequency_to_pickle(d, pickle_fname):
f.close() f.close()
def unfamiliar(path,word): def unfamiliar(path,word):
f = open(path,"rb") if not os.path.exists(path):
dic = pickle.load(f) return None
dic[word] += [datetime.now().strftime('%Y%m%d%H%M')] with open(path,"rb") as f:
fp = open(path,"wb") dic = pickle.load(f)
pickle.dump(dic,fp) dic[word] += [datetime.now().strftime('%Y%m%d%H%M')]
with open(path,"wb") as fp:
pickle.dump(dic,fp)
def familiar(path,word): def familiar(path,word):
f = open(path,"rb") f = open(path,"rb")