drime

French rhyme dictionary with web and CLI interface
git clone https://a3nm.net/git/drime/
Log | Files | Refs | README

commit 00ddac0224b135d0b1719eb6646bf791e4173d2f
parent d28f7ef401b5a6a992b93b47e2dedf9eda78f886
Author: Antoine Amarilli <a3nm@a3nm.net>
Date:   Fri, 30 Dec 2011 14:45:59 +0100

fix indent and encoding

Diffstat:
query.py | 66+++++++++++++++++++++++++++++++++++-------------------------------
1 file changed, 35 insertions(+), 31 deletions(-)

diff --git a/query.py b/query.py @@ -1,10 +1,10 @@ #!/usr/bin/python -O import sys +import codecs import operator from db_mysql import run_query from common import from_xsampa, to_xsampa -import simplejson as json PAGESIZE = 500 @@ -171,34 +171,38 @@ def do_query(word, phon, minsyll, maxsyll, elide, gender, offset, size): return {'keys': [key], 'result': result2}, count if __name__ == '__main__': - def usage(): - print ("Usage: %s QUERY [NSYL [GENDER]]" % sys.argv[0]) - try: - r, c = query(*sys.argv[1:]) - except BadValues: - print ("Bad values passed as arguments.") - usage() - sys.exit(4) - except TypeError: - usage() - sys.exit(4) - if len(r['keys']) == 0: - print ("No interpretation found for %s" % sys.argv[1]) - sys.exit(1) - if len(r['keys']) > 1: - print ("Multiple interpretations found for %s:" % sys.argv[1]) - for k in r['keys']: - print (" - %s" % k[2]) - print ("Please rerun with a more specific query") - sys.exit(2) - result = [["word", "phon", "pr", "wr", "freq", "orig"]] + [ - (x['word'], - x['phon'], - str(x['phon_rhyme']), - str(x['word_rhyme']), - str(x['freq']), - x['orig']) for x in r['result']] - #print ("Displaying result for %s" % r['keys'][0][2]) - for x in result: - print '|'.join([y.encode('utf-8') for y in x]) + # work around encoding issues + sys.argv = [x.decode('utf-8') for x in sys.argv] + sys.stdout = codecs.getwriter('utf8')(sys.stdout) + + def usage(): + print ("Usage: %s QUERY [NSYL [GENDER]]" % sys.argv[0]) + try: + r, c = query(*sys.argv[1:]) + except BadValues: + print ("Bad values passed as arguments.") + usage() + sys.exit(4) + except TypeError: + usage() + sys.exit(4) + if len(r['keys']) == 0: + print ("No interpretation found for %s" % sys.argv[1]) + sys.exit(1) + if len(r['keys']) > 1: + print ("Multiple interpretations found for %s:" % sys.argv[1]) + for k in r['keys']: + print (" - %s" % k[2]) + print ("Please rerun with a more specific query") + sys.exit(2) + result = [["word", "phon", "pr", "wr", "freq", "orig"]] + [ + (x['word'], + x['phon'], + str(x['phon_rhyme']), + str(x['word_rhyme']), + str(x['freq']), + x['orig']) for x in r['result']] + #print ("Displaying result for %s" % r['keys'][0][2]) + for x in result: + print '|'.join(x)