EnglishPal/app/pickle_idea2.py

90 lines
2.7 KiB
Python
Raw Permalink Normal View History

2021-04-06 16:22:03 +08:00
###########################################################################
# Copyright 2019 (C) Hui Lan <hui.lan@cantab.net>
# Written permission must be obtained from the author for commercial uses.
###########################################################################
# Purpose: dictionary & pickle as a simple means of database.
# Task: incorporate the functions into wordfreqCMD.py such that it will also show cumulative frequency.
# Note: unlike pick_idea.py, now the second item is not frequency, but a list of dates.
import pickle
from datetime import datetime
def lst2dict(lst, d):
'''
Store the information in list lst to dictionary d.
Note: nothing is returned.
'''
for x in lst:
word = x[0]
dates = x[1]
if not word in d:
d[word] = dates
else:
d[word] += dates
def deleteRecord(path,word):
with open(path, 'rb') as f:
db = pickle.load(f)
try:
db.pop(word)
except KeyError:
print("sorry")
with open(path, 'wb') as ff:
pickle.dump(db, ff)
2021-04-06 16:22:03 +08:00
def dict2lst(d):
if len(d) > 0:
keys = list(d.keys())
if isinstance(d[keys[0]], int):
lst = []
for k in d:
lst.append((k, [datetime.now().strftime('%Y%m%d%H%M')]))
return lst
Pylint部分: 1. 补充module docstring,即文档顶部的简要说明 (我做的说明很简陋,只是为了通过pylint的检查而为之) 2. 优化了if-else-return,即删除了不必要的elif或else 3. 拆分了所有在同一行的模块引用(不同模块的引用需要放在不同行,以便理解和阅读) 4. 删除了部分没有被使用的引用(有的引用在文件A中被使用了,但import却写在了文件B,且文件B没有使用这些引用,而文件A引用了文件B,导致文件B的所有引用也被A引用,这似乎并不合理) 5. 修改了引用的顺序(os、random等系统引用应放在自定义模块引用的上方) 6. 修改了过长的代码,注释除外(每行限制在100个字符以内) 7. 优化了条件判断的判断符号: is和is not 用于判断两个变量是否指向同一个位置 == 和 !=用于比较两个变量 8. 为每个py文件添加了"last new line",删除了多余的尾部空行 Pylint提示了但是没有进行的操作: 1. 将字符串改为f-string的格式 因为f-string需要python3.6以上的版本,为了防止出现版本问题,故不做修改 2. 将所有函数内的return内容改为一致的类型 因为原有的代码逻辑已经确定,修改return的类型会导致方法的无法使用,故不做修改 3. 将标识符的格式改为"snake_case naming style" 标识符的修改内容过于庞大,且可能出现程序不可预见的错误,在pylint中重要等级为0,故不做修改 JSLint Part: 1. 提高代码的可读性:将部分超过80个字符的代码行拆解开(由于文字注释的存在,很多语句仅注释就超过了80个字符,此类型的语句行不做修改) 2. 用===与!==代替==与!=的判断 3. 将i++改为i+=1 4. 分离for循环中的let与var到其他行中,以适应JSLint在循环语句中对var和let的偏好 5. 给三元表达式外围添加圆括号,避免出现优先级的问题 6. 将正则表达式修改为JSLint要求的多行模式 疑问: 形如: for (let i = 0; i < list.length; i+=1) { 的语句,JSLint报错: [JSLint was unable to finish] Unexpected 'let'. 但是网上搜索到的结果是ES6是支持let的使用的,暂且不做修改。
2023-05-26 22:18:11 +08:00
if isinstance(d[keys[0]], list):
2021-04-06 16:22:03 +08:00
return list(d.items()) # a list of (key, value) pairs
return []
def merge_frequency(lst1, lst2):
d = {}
lst2dict(lst1, d)
lst2dict(lst2, d)
return d
def load_record(pickle_fname):
f = open(pickle_fname, 'rb')
d = pickle.load(f)
f.close()
return d
def save_frequency_to_pickle(d, pickle_fname):
f = open(pickle_fname, 'wb')
exclusion_lst = ['one', 'no', 'has', 'had', 'do', 'that', 'have', 'by', 'not', 'but', 'we', 'this', 'my', 'him', 'so', 'or', 'as', 'are', 'it', 'from', 'with', 'be', 'can', 'for', 'an', 'if', 'who', 'whom', 'whose', 'which', 'the', 'to', 'a', 'of', 'and', 'you', 'i', 'he', 'she', 'they', 'me', 'was', 'were', 'is', 'in', 'at', 'on', 'their', 'his', 'her', 's', 'said', 'all', 'did', 'been', 'w']
d2 = {}
for k in d:
if not k in exclusion_lst and not k.isnumeric() and not len(k) < 2:
d2[k] = list(sorted(set(d[k])))
pickle.dump(d2, f)
f.close()
if __name__ == '__main__':
lst1 = [('apple',['201910251437', '201910251438']), ('banana',['201910251439'])]
d = {}
lst2dict(lst1, d) # d will change
save_frequency_to_pickle(d, 'frequency.p') # frequency.p is our database
lst2 = [('banana',['201910251439']), ('orange', ['201910251440', '201910251439'])]
d = load_record('frequency.p')
lst1 = dict2lst(d)
d = merge_frequency(lst2, lst1)
print(d)