refactored charfinder examples

This commit is contained in:
Luciano Ramalho
2015-01-19 15:27:46 -02:00
parent ca0566df16
commit fe0db0fa7b
3 changed files with 22 additions and 35 deletions

View File

@@ -167,23 +167,24 @@ class UnicodeNameIndex:
for code in self.find_codes(query): for code in self.find_codes(query):
yield self.describe(code) yield self.describe(code)
@staticmethod # not an instance method due to concurrency
def main(*args): def status(query, counter):
index = UnicodeNameIndex()
query = ' '.join(args)
counter = 0
for line in index.find_descriptions(query):
print(line)
counter += 1
if counter == 0: if counter == 0:
msg = 'No match' msg = 'No match'
elif counter == 1: elif counter == 1:
msg = '1 match' msg = '1 match'
else: else:
msg = '{} matches'.format(counter) msg = '{} matches'.format(counter)
print('({} for {!r})'.format(msg, query)) return '{} for {!r}'.format(msg, query)
def main(*args):
index = UnicodeNameIndex()
query = ' '.join(args)
for n, line in enumerate(index.find_descriptions(query), 1):
print(line)
print('({})'.format(index.status(query, n)))
if __name__ == '__main__': if __name__ == '__main__':
if len(sys.argv) > 1: if len(sys.argv) > 1:
main(*sys.argv[1:]) main(*sys.argv[1:])

View File

@@ -45,8 +45,7 @@ def handle(request):
if query: if query:
lines = list(index.find_descriptions(query)) lines = list(index.find_descriptions(query))
res = '\n'.join(lines) res = '\n'.join(lines)
plural = 'es' if len(lines) > 1 else '' msg = index.status(query, len(lines))
msg = '{} match{} for {!r}'.format(len(lines), plural, query)
else: else:
lines = [] lines = []
res = '' res = ''

View File

@@ -10,14 +10,6 @@ PROMPT = b'?> '
index = None # a UnicodeNameIndex instance index = None # a UnicodeNameIndex instance
def writeln(writer, arg):
if isinstance(arg, str):
lines = [arg]
else:
lines = arg
writer.writelines(line.encode() + CRLF for line in lines)
@asyncio.coroutine @asyncio.coroutine
def handle_queries(reader, writer): def handle_queries(reader, writer):
while True: while True:
@@ -28,20 +20,15 @@ def handle_queries(reader, writer):
query = data.decode().strip() query = data.decode().strip()
except UnicodeDecodeError: except UnicodeDecodeError:
query = '\x00' query = '\x00'
client = writer.get_extra_info('peername')
print('Received from {}: {!r}'.format(client, query))
if query:
if ord(query[:1]) < 32: if ord(query[:1]) < 32:
break break
client = writer.get_extra_info('peername')
print('Received from {}: {}'.format(client, query))
lines = list(index.find_descriptions(query)) lines = list(index.find_descriptions(query))
if lines: if lines:
writeln(writer, lines) writer.writelines(line.encode() + CRLF for line in lines)
plural = 'es' if len(lines) > 1 else '' writer.write(index.status(query, len(lines)).encode() + CRLF)
msg = '({} match{} for {!r})'.format(len(lines), plural, query)
writeln(writer, msg)
print('Sent: {} lines + total'.format(len(lines)))
else:
writeln(writer, '(No match for {!r})'.format(query))
print('Sent: 1 line, no match')
yield from writer.drain() yield from writer.drain()
print('Close the client socket') print('Close the client socket')