【原创】shadowebdict开发日记:基于linux的简明英汉字典(四)
全系列目录:
- 【原创】shadowebdict开发日记:基于linux的简明英汉字典(一)
- 【原创】shadowebdict开发日记:基于linux的简明英汉字典(二)
- 【原创】shadowebdict开发日记:基于linux的简明英汉字典(三)
- 【原创】shadowebdict开发日记:基于linux的简明英汉字典(四)
- 项目的github地址
实习的工作都这么忙,真是的。。
好不容易有时间写点博客,一鼓作气完成算了
承接上文
本文完成对本地数据库模块的开发。
由于只是非常轻量级的应用,就不劳mysql大驾了,来个sqlite就可以了,本地db文件也好读取。
这里我们可以多实现一个功能:难词系统。
定义一个难词为查询多次达到某个阀值的词,并记录本地数据库中所有词汇的查询次数。
当有词语达到这个标准时,将该词添加到本地数据库中的难词表,并提供一个方法供其他模块查询难词表。
可以看出,难词系统决定了本地数据模块这个部分和response模块不同,需要提供多个模块供上层操作,包括:
0、提供查询难词表的接口。
1、提供更新每个词汇查询次数的接口。
2、提供插入词汇的接口。
3、和response模块一样,给定一个词汇,查询其含义的接口。
具体代码如下:
# -*- coding:utf-8 -*-
__author__ = 'wmydx' import sqlite3 class LocalDict:
def __init__(self):
self.con = None
self.limit = 4
self.setup_connect() def setup_connect(self):
self.con = sqlite3.connect('./word.db')
create_table = '''CREATE TABLE IF NOT EXISTS words
(
word text,
explain text,
net_explain text,
sentence text,
times int
);
'''
self.con.execute(create_table)
create_table = '''CREATE TABLE IF NOT EXISTS hard
(
word text,
explain text,
net_explain text,
sentence text,
times int
);
'''
self.con.execute(create_table)
self.con.text_factory = str def is_a_hard_word(self, diction):
print diction['times'] == self.limit
return diction['times'] == self.limit # prevent mutiple insert, so use == instead of >= def update_word_times(self, diction):
curs = self.con.cursor()
update_sql = '''
UPDATE words SET times=? WHERE word=?
'''
curs.execute(update_sql,(diction['times'], diction['word'])) def get_hard_word(self):
curs = self.con.cursor()
select_sql = '''
SELECT word FROM hard;
'''
curs.execute(select_sql)
names = [d[0] for d in curs.description]
rows = [dict(zip(names, row)) for row in curs.fetchall()]
return rows def get_eng_word_from_db(self, word):
curs = self.con.cursor()
select_sql = '''
SELECT * FROM words WHERE word=\'%s\';
''' % word
curs.execute(select_sql)
names = [d[0] for d in curs.description]
rows = [dict(zip(names, row)) for row in curs.fetchall()]
return rows def process_dict(self, diction):
for key in diction.keys():
if diction[key] == -1:
diction[key] = ''
return diction # before pass diction to this method, u need to add word and times in diction
def insert_word_to_db(self, diction, table):
diction = self.process_dict(diction)
insert_sql = '''
INSERT INTO %s (word,explain,net_explain,sentence,times) VALUES
(?,?,?,?,?);
''' % table
self.con.execute(insert_sql, (diction['word'], diction['explain'],
diction['net_explain'], diction['sentence'], diction['times'])) def turn_off_db(self):
self.con.commit()
self.con.close() if __name__ == '__main__':
db = LocalDict()
db.setup_connect()
到此为止,我们开发了一个完整的webdict,平常看英文原著的时候,快捷命令一打就随手查吧
免得开浏览器然后。。。=_+
【原创】shadowebdict开发日记:基于linux的简明英汉字典(四)的更多相关文章
- 【原创】shadowebdict开发日记:基于linux的简明英汉字典(三)
全系列目录: [原创]shadowebdict开发日记:基于linux的简明英汉字典(一) [原创]shadowebdict开发日记:基于linux的简明英汉字典(二) [原创]shadowebdic ...
- 【原创】shadowebdict开发日记:基于linux的简明英汉字典(二)
全系列目录: [原创]shadowebdict开发日记:基于linux的简明英汉字典(一) [原创]shadowebdict开发日记:基于linux的简明英汉字典(二) [原创]shadowebdic ...
- 【原创】shadowebdict开发日记:基于linux的简明英汉字典(一)
全系列目录: [原创]shadowebdict开发日记:基于linux的简明英汉字典(一) [原创]shadowebdict开发日记:基于linux的简明英汉字典(二) [原创]shadowebdic ...
- Go 语言开发的基于 Linux 虚拟服务器的负载平衡平台 Seesaw
负载均衡系统 Seesaw Seesaw是由我们网络可靠性工程师用 Go 语言开发的基于 Linux 虚拟服务器的负载平衡平台,就像所有好的项目一样,这个项目也是为了解决实际问题而产生的. Seesa ...
- 构建一个基本的前端自动化开发环境 —— 基于 Gulp 的前端集成解决方案(四)
通过前面几节的准备工作,对于 npm / node / gulp 应该已经有了基本的认识,本节主要介绍如何构建一个基本的前端自动化开发环境. 下面将逐步构建一个可以自动编译 sass 文件.压缩 ja ...
- 嵌入式Linux驱动开发日记
嵌入式Linux驱动开发日记 主机硬件环境 开发机:虚拟机Ubuntu12.04 内存: 1G 硬盘:80GB 目标板硬件环境 CPU: SP5V210 (开发板:QT210) SDRAM: 512M ...
- 用VSCode开发一个基于asp.net core 2.0/sql server linux(docker)/ng5/bs4的项目(1)
最近使用vscode比较多. 学习了一下如何在mac上使用vscode开发asp.netcore项目. 这里是我写的关于vscode的一篇文章: https://www.cnblogs.com/cgz ...
- Linux系统启动那些事—基于Linux 3.10内核【转】
转自:https://blog.csdn.net/shichaog/article/details/40218763 Linux系统启动那些事—基于Linux 3.10内核 csdn 我的空间的下载地 ...
- 轻易实现基于linux或win运行的聊天服务端程序
对于不了解网络编程的开发人员来说,编写一个良好的服务端通讯程序是一件比较麻烦的事情.然而通过EC这个免费组件你可以非常简单地构建一个基于linux或win部署运行的网络服务程序.这种便利性完全得益于m ...
随机推荐
- 与众不同 windows phone (20) - Device(设备)之位置服务(GPS 定位), FM 收音机, 麦克风, 震动器
原文:与众不同 windows phone (20) - Device(设备)之位置服务(GPS 定位), FM 收音机, 麦克风, 震动器 [索引页][源码下载] 与众不同 windows phon ...
- codeforces 598B Queries on a String
题目链接:http://codeforces.com/problemset/problem/598/B 题目分类:字符串 题意:给定一个串,然后n次旋转,每次给l,r,k,表示区间l到r的字符进行k次 ...
- Facial Landmark Detection
源地址:http://www.learnopencv.com/facial-landmark-detection/#comment-2471797375 OCTOBER 18, 2015 BY SAT ...
- UIButton return(textField textView)
首先设置TextField 或 TextView的 delegate /////UITextView - (BOOL)textView:(UITextView *)textView shouldCha ...
- 算法 《秦九韶算法java实践》
[历史背景] 秦九韶算法是中国南宋时期的数学家秦九韶表述求解一元高次多项式的值的算法--正负开方术.它也能够配合牛顿法用来求解一元高次多项式的根.在西方被称作霍纳算法(Horner algorithm ...
- [置顶] github简单使用
git的介绍可以看这里 http://zh.wikipedia.org/wiki/GitHub 安装和使用参考的这个 http://www.cnblogs.com/cocowool/arch ...
- spring Annotation 组分注塑
spring 注意分类 启动spring自己主动扫描功能 <context:component-scan/> 1.@Repository: 它用于将数据訪问层 (DAO 层 ) 的类标识为 ...
- HBase经常使用操作之namespace
1.介绍 在HBase中,namespace命名空间指对一组表的逻辑分组,类似RDBMS中的database,方便对表在业务上划分.Apache HBase从0.98.0, 0.95.2两个版本号開始 ...
- AIX常用命令略记
■ 初始化端末时可能需要确认服务器端和端末时间是否匹配 ●cal 显示日历 ●date 显示服务前当前时间 ■ 显示当前目录,即显示当前所在目录的adress ●pwd(print workin ...
- js使用栈来实现10进制转8进制 js取除数 余数
function ten2eight(x){ var s=[]; var r=''; while(x>0){ s.push(x%8); x=parseInt(x/8); } while(s.le ...