对了还有一点就是,本来有个比较好的想法的,就是利用和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()