def query(dictionary, word): global config, dict_cache enable_cache = config['cache']['enable'] if config else True if enable_cache: cache_expire = (config['cache']['expire'] if config else 24) * 3600 now = time.time() # dict_cache.set('last lookup time', now, float('inf')) # time.sleep(1) # if dict_cache.get('last lookup time') != now: # return clean_time = dict_cache.get('last clean time') if clean_time is None or now - clean_time > cache_expire: dict_cache.set('last clean time', now, float('inf')) dict_cache.clean_expired() cache_name = '{}@{}'.format(word, dictionary) cache = dict_cache.get(cache_name) if cache: return cache options = config['options'] if config else {} dict_name = cndict.get_full_name(dictionary) options = options.get(dict_name, {}) result = cndict.lookup(dictionary, word, **options) if result: result = [item.decode('utf-8') for item in result] if enable_cache: dict_cache.set(cache_name, result, cache_expire) return result
def query(dictionary, word, dict_cache): now = time.time() # dict_cache.set('last lookup time', now, float('inf')) # time.sleep(1) # if dict_cache.get('last lookup time') != now: # return clean_time = dict_cache.get('last clean time') if clean_time is None or now - clean_time > 3600 * 24: dict_cache.set('last clean time', now, float('inf')) dict_cache.clean_expired() cache_name = '{}@{}'.format(word, dictionary) cache = dict_cache.get(cache_name) if cache: return cache result = cndict.lookup(dictionary, word) if result: result = [item.decode('utf-8') for item in result] dict_cache.set(cache_name, result, 3600 * 24) return result
#!/usr/bin/env python # -*- coding: utf-8 -*- import sys import os sys.path.append(os.path.join(os.path.dirname(__file__), '..')) from cndict import lookup if len(sys.argv) < 3: print 'usage: python cndict <dict> <word>' sys.exit(1) result = lookup(*(sys.argv[1:])) print '\n'.join(result)