def RenderName(names, sex): debug = True t = PrettyTable() t.field_names = [ '序号', '简体', '繁体', '读音', 'ID', '百度', '百科', '打分', '五格分', '分析', '释义' ] t.align = 'l' for i, n in enumerate(names): word = unicode(n, 'utf-8')[-1] freq, comment = word_freq(word, 1) row = [ i, t2s(n), s2t(n), toPY(n), toID(n), Baidu(debug=debug).search(n).nums(), Baike(debug=debug).search(n).nums(), PCbaby(debug=debug).search(n, sex=sex).nums(), Httpcn(debug=debug).search(n, sex=sex).nums(), str(freq) + ' ' + comment, lookup(word)[:30].replace('\n', ''), ] t.add_row(row) return t
def t(self, name, sname): if sname is None: sname = name if py3k: [uname, bname] = [sname, sname.encode('UTF-8')] else: [uname, bname] = [sname.decode('UTF-8'), sname] sym = Symbol(name) x = Expression(sym) assert_is(x, Expression(x)) # __repr__(x) assert_repr(x, 'Expression({sym!r})'.format(sym=sym)) # value: v = x.value assert_equal(type(v), Symbol) assert_equal(v, sym) # lvalue: v = x.lvalue assert_equal(type(v), Symbol) assert_equal(v, sym) # __str__(): assert_equal(str(x), sname) assert_repr(x, repr(Expression.from_string(sname))) # __unicode__(): assert_equal(unicode(x), uname) assert_repr(x, repr(Expression.from_string(uname))) # __eq__(), __ne__(): assert_equal(x, Expression(sym)) assert_not_equal(x, Expression(name)) assert_not_equal(x, sym) # __hash__(): assert_equal(hash(x), hash(bname.strip(b'|'))) # pickle: assert_pickle_equal(x) return x
def test_nonexistent_ja(self): skip_unless_c_messages() skip_unless_translation_exists('ja_JP.UTF-8') path = '__nonexistent__' context = Context() try: with interim_locale(LC_ALL='ja_JP.UTF-8'): os.stat(path) except OSError as ex: c_message = ex.args[1] else: raise OSError(errno.EEXIST, os.strerror(errno.EEXIST), path) try: c_message.encode('ASCII') except UnicodeError: pass else: raise AssertionError( 'ja_JP error message is ASCII-only: {msg!r}'.format( msg=c_message)) with interim_locale(LC_ALL='ja_JP.UTF-8'): with assert_raises(JobFailed): context.new_document(FileUri(path)) message = context.get_message() assert_equal(type(message), ErrorMessage) assert_equal(type(message.message), unicode) assert_equal( message.message, u("[1-11711] Failed to open '{path}': {msg}.".format( path=path, msg=c_message))) assert_equal( str(message), "[1-11711] Failed to open '{path}': {msg}.".format( path=path, msg=c_message)) assert_equal(unicode(message), message.message)
def test_nonexistent_ja(self): skip_unless_c_messages() skip_unless_translation_exists('ja_JP.UTF-8') path = '__nonexistent__' context = Context() try: with interim_locale(LC_ALL='ja_JP.UTF-8'): os.stat(path) except OSError as ex: c_message = ex.args[1] else: raise OSError(errno.EEXIST, os.strerror(errno.EEXIST), path) try: c_message.encode('ASCII') except UnicodeError: pass else: raise AssertionError( 'ja_JP error message is ASCII-only: {msg!r}'.format(msg=c_message) ) with interim_locale(LC_ALL='ja_JP.UTF-8'): with assert_raises(JobFailed): context.new_document(FileUri(path)) message = context.get_message() assert_equal(type(message), ErrorMessage) assert_equal(type(message.message), unicode) assert_equal( message.message, u("[1-11711] Failed to open '{path}': {msg}.".format(path=path, msg=c_message)) ) assert_equal( str(message), "[1-11711] Failed to open '{path}': {msg}.".format(path=path, msg=c_message) ) assert_equal(unicode(message), message.message)
def t(self, n, x=None): if x is None: x = Expression(n) assert_is(x, Expression(x)) # __repr__(): assert_repr(x, 'Expression({n})'.format(n=int(n))) # value: v = x.value assert_equal(type(v), int) assert_equal(v, n) # lvalue: v = x.lvalue assert_equal(type(v), int) assert_equal(v, n) # __int__(): i = int(x) assert_equal(type(i), int) assert_equal(i, n) # __long__(): i = long(x) assert_equal(type(i), long) assert_equal(i, n) # __float__(): i = float(x) assert_equal(type(i), float) assert_equal(i, n) # __str__(): s = str(x) assert_equal(s, str(n)) # __unicode__(): s = unicode(x) assert_equal(s, str(n)) # __eq__(), __ne__(): assert_equal(x, Expression(n)) assert_not_equal(x, n) assert_not_equal(x, Expression(n + 37)) # __hash__(): assert_equal(hash(x), n) # __bool__() / __nonzero__(): obj = object() if n: assert_is(x and obj, obj) assert_is(x or obj, x) else: assert_is(x and obj, x) assert_is(x or obj, obj) # pickle: assert_pickle_equal(x)
def main(): ARGS = argparse.ArgumentParser(description=u'起名助手') ARGS.add_argument('--names', action='store', help=u'名字列表(","分隔)') ARGS.add_argument('--name_file', action='store', help=u'名字文件(每行一个)') ARGS.add_argument('--sex', action='store', type=int, default=1, help=u'性别: 0-女, 1-男') ARGS.add_argument('--word', action='store', help=u'查询字典') ARGS.add_argument('--topk', action='store', type=int, default=0, help=u'查看常用名字TopK') ARGS.add_argument('--test', action='store_true', dest='test', default=True, help=u'run a test (default)') args = ARGS.parse_args() if args.names: names = [name.strip() for name in args.names.split(',')] print(RenderName(names, args.sex)) return if args.name_file: f = open(args.name_file) names = [name.strip() for name in f.readlines()] names = [name for name in names if not name.startswith('#')] f.close() print(RenderName(names, args.sex)) return if args.word: print(lookup(unicode(args.word, 'utf-8'))) return if args.topk > 0: names = word_topk(args.topk, 1) for i, r in enumerate(names): print('{}. {}'.format(i + 1, r)) return if args.test: names = ['孙中山', '戴笠', '杨利伟', '曹植', '李小龙'] print(RenderName(names, args.sex)) return print('Use --help for command line help')
def t(self, name, sname=None): if sname is None: sname = name if py3k: [uname, bname] = [sname, sname.encode('UTF-8')] else: [uname, bname] = [sname.decode('UTF-8'), sname] symbol = Symbol(name) assert_equal(type(symbol), Symbol) assert_equal(symbol, Symbol(name)) assert_is(symbol, Symbol(name)) assert_equal(str(symbol), sname) assert_equal(unicode(symbol), uname) assert_not_equal(symbol, bname) assert_not_equal(symbol, uname) assert_equal(hash(symbol), hash(bname)) assert_pickle_equal(symbol) return symbol
def test_nonexistent(self): path = '__nonexistent__' try: os.stat(path) except OSError as ex: c_message = ex.args[1] else: raise OSError(errno.EEXIST, os.strerror(errno.EEXIST), path) c_message.encode('ASCII') skip_unless_c_messages() context = Context() with assert_raises(JobFailed): context.new_document(FileUri(path)) message = context.get_message() assert_equal(type(message), ErrorMessage) assert_equal(type(message.message), unicode) assert_equal( message.message, "[1-11711] Failed to open '{path}': {msg}.".format(path=path, msg=c_message)) assert_equal(str(message), message.message) assert_equal(unicode(message), message.message)
def test_nonexistent(self): path = '__nonexistent__' try: os.stat(path) except OSError as ex: c_message = ex.args[1] else: raise OSError(errno.EEXIST, os.strerror(errno.EEXIST), path) c_message.encode('ASCII') skip_unless_c_messages() context = Context() with assert_raises(JobFailed): context.new_document(FileUri(path)) message = context.get_message() assert_equal(type(message), ErrorMessage) assert_equal(type(message.message), unicode) assert_equal( message.message, "[1-11711] Failed to open '{path}': {msg}.".format(path=path, msg=c_message) ) assert_equal(str(message), message.message) assert_equal(unicode(message), message.message)
def t(self, name, sname): if sname is None: sname = name if py3k: [uname, bname] = [sname, sname.encode('UTF-8')] else: [uname, bname] = [sname.decode('UTF-8'), sname] sym = Symbol(name) x = Expression(sym) assert_is(x, Expression(x)) # __repr__(x) assert_repr(x, 'Expression({sym!r})'.format(sym=sym)) # value: v = x.value assert_equal(type(v), Symbol) assert_equal(v, sym) # lvalue: v = x.lvalue assert_equal(type(v), Symbol) assert_equal(v, sym) # __str__(): assert_equal(str(x), sname) assert_repr(x, repr(Expression.from_string(sname))) # __unicode__(): assert_equal(unicode(x), uname) assert_repr(x, repr(Expression.from_string(uname))) # __eq__(), __ne__(): assert_equal(x, Expression(sym)) assert_not_equal(x, Expression(name)) assert_not_equal(x, sym) # __hash__(): assert_equal( hash(x), hash(bname.strip(b'|')) ) # pickle: assert_pickle_equal(x) return x
def search(self, word, **kwargs): sex = self.get_sex(**kwargs) filename = self.get_filename("{}-{}".format(word, sex)) if self.debug and os.path.exists(filename): data = Helper.fetch_raw_data(filename) soup = BeautifulSoup(data, 'html.parser') else: url = 'http://m.life.httpcn.com/m/xingming/' uname = unicode(word, 'utf-8') data = { 'act': 'submit', 'xing': uname[0], 'ming': uname[1:], 'sex': sex, 'isbz': 0, } Helper.simple_download(url, filename) res = Downloader(url, 3, 3).post(data=data) soup = BeautifulSoup(res.text, 'html.parser') Helper.store_raw_data(soup.prettify(), filename) soup = soup.find('div', 'progress-bar') self.score = self.parse_num(self.get(soup)) return self