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,22 +167,23 @@ 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 status(query, counter):
if counter == 0:
msg = 'No match'
elif counter == 1:
msg = '1 match'
else:
msg = '{} matches'.format(counter)
return '{} for {!r}'.format(msg, query)
def main(*args): def main(*args):
index = UnicodeNameIndex() index = UnicodeNameIndex()
query = ' '.join(args) query = ' '.join(args)
counter = 0 for n, line in enumerate(index.find_descriptions(query), 1):
for line in index.find_descriptions(query):
print(line) print(line)
counter += 1 print('({})'.format(index.status(query, n)))
if counter == 0:
msg = 'No match'
elif counter == 1:
msg = '1 match'
else:
msg = '{} matches'.format(counter)
print('({} for {!r})'.format(msg, query))
if __name__ == '__main__': if __name__ == '__main__':
if len(sys.argv) > 1: if len(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,21 +20,16 @@ def handle_queries(reader, writer):
query = data.decode().strip() query = data.decode().strip()
except UnicodeDecodeError: except UnicodeDecodeError:
query = '\x00' query = '\x00'
if ord(query[:1]) < 32:
break
client = writer.get_extra_info('peername') client = writer.get_extra_info('peername')
print('Received from {}: {}'.format(client, query)) print('Received from {}: {!r}'.format(client, query))
lines = list(index.find_descriptions(query)) if query:
if lines: if ord(query[:1]) < 32:
writeln(writer, lines) break
plural = 'es' if len(lines) > 1 else '' lines = list(index.find_descriptions(query))
msg = '({} match{} for {!r})'.format(len(lines), plural, query) if lines:
writeln(writer, msg) writer.writelines(line.encode() + CRLF for line in lines)
print('Sent: {} lines + total'.format(len(lines))) writer.write(index.status(query, len(lines)).encode() + CRLF)
else: yield from writer.drain()
writeln(writer, '(No match for {!r})'.format(query))
print('Sent: 1 line, no match')
yield from writer.drain()
print('Close the client socket') print('Close the client socket')
writer.close() writer.close()