好久没有更新blog了,一方面因为还在学习python,又在看linux的网络内核,真心忙不过来。学习python有那么一段时间了,感觉它的内容真的很多阿,还得继续努力。以前用bash写过一个终端下的在线词典的东西。后来学习python就一直想过用python重构一下。下面是源码,基本思路都一样,只是还没有想到一种好的数据结构进行历史内容的存储。其实有想过用pickle,但是每次都得将其全部导入内存,就像fcitx的词库一样。PS:我重新编译过fcitx的词库,导致其占用内存很大。唉,任何事情都是两面的,每一种所谓的最佳解决办法,其实就是一种折中罢了。所以,我就使用了原来扒取的xml,这样再次在本地读取的时候就少了很多事。
对了还有一点就是,本来有个比较好的想法的,就是利用和OS一样查找寄存器和内存的并行操作来解决,查找本地历史记录和网上结果,谁先给出结果就kill掉另一个,但是threading只是提供创建线程,而且它忽略了函数返回值,我想过用一个全局变量来作为一个标志,但是当时用锁的时候,其他申请锁的线程都阻塞了,这样还是没有达到我的想法。如果你有好的思路希望能分享一下。愿与君共勉之。
- #!/usr/bin/python
- #encoding=utf-8
- #===============================================================================
- #
- # FILE: pdic
- #
- # USAGE: pdic [word]
- #
- # DESCRIPTION: The dictionary based on Terminal
- #
- # OPTIONS: ---
- # REQUIREMENTS: ---
- # BUGS: ---
- # NOTES: ---
- # AUTHOR: Eric Yu , linuxer.yu@gmail.com
- # LICENCE: GPL
- # CREATED:
- # REVISION: ---
- #===============================================================================
- from xml.etree import ElementTree
- import threading
- import re
- import os
- import sys
- import urllib2
- '''The word you want to search\
- and the place thar stored the word directory'''
- word = ''
- word_dir = r'word_dir'
- #content = urllib2.urlopen(r'%s'% word)
- #mean = content.read()
- #file = open('file','w+')
- #file.write(mean)
- #file.close()
- '''Use to replace , '''
- parleft = re.compile(r'')
- parright = re.compile(r'')
- def print_node(node):
- '''Show the meaning of the word\
- and replace , '''
- string = re.sub(parleft,'<',node.text)
- text = re.sub(parright,'>',string)
- print '%s'% text
- #read_xml(mean)
- def Judge():
- '''判断是否存在字典文件夹,不存在建立'''
- global word_dir
- if os.path.exists(word_dir):
- if os.path.isdir(word_dir):
- pass
- else:
- print "There is file named %s"% word_dir
- return 0
- else:
- try:
- os.mkdir(word_dir)
- except:
- print "Fail to create the directory"
- return 0
- else:
- print "First Use:Created the directory"
- return 1
- def Usage():
- '''判断输入是否正确'''
- if len(sys.argv) != 2:
- print "Usage: %s [word]"% sys.argv[0]
- return 0
- else:
- global word
- word = sys.argv[1]
- class Word:
- def __init__(self,word):
- self.word = word
- self.url = r'%s'% self.word
- def GetUrl(self):
- global word_dir
- file = word_dir + '/' + self.word + '.xml'
- try:
- content = urllib2.urlopen(self.url)
- except:
- print "Fail to download"
- return 0
- self.mean = content.read()
- if re.search(r'|Not Found',self.mean):
- return 1
- else:
- wf = open(file,'w')
- wf.write(self.mean)
- wf.close()
- if self.mean == '':
- print "Fail to download"
- return 0
- def Read_xml(self):
- root = ElementTree.fromstring(self.mean)
- '''root.find() 返回的为None。'''
- node_find = root.getiterator('def')
- if not node_find:
- pass
- else:
- for i in node_find:
- print_node(i)
- lst_node = root.getiterator("sent")
- if not lst_node:
- pass
- else:
- for i in lst_node:
- child = i.getchildren()
- for j in child:
- lst_node_child = j
- print_node(lst_node_child)
- node_sugg = root.getiterator('sugg')
- if not node_sugg:
- pass
- else:
- for i in node_sugg:
- print_node(i)
- def local_xml(self):
- '''Read the word file local'''
- global word_dir
- file = word_dir + '/' + self.word + '.xml'
- if os.path.exists(file) and os.path.isfile(file):
- wf = open(file)
- self.mean = wf.read()
- if not len(self.mean):
- return 0
- wf.close()
- return 1
- return 0
- def main():
- if Usage() == 0:
- sys.exit()
- if Judge() == 0:
- sys.exit()
- tran = Word(word)
- if tran.local_xml() == 0:
- if tran.GetUrl() == 0:
- print 'Error'
- sys.exit()
- else:
- tran.Read_xml()
- else:
- tran.Read_xml()
- return 1
- if __name__ == '__main__':
- main()
阅读(2079) | 评论(0) | 转发(0) |