refactored charfinder examples
This commit is contained in:
parent
ca0566df16
commit
fe0db0fa7b
@ -167,22 +167,23 @@ class UnicodeNameIndex:
|
||||
for code in self.find_codes(query):
|
||||
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):
|
||||
index = UnicodeNameIndex()
|
||||
query = ' '.join(args)
|
||||
counter = 0
|
||||
for line in index.find_descriptions(query):
|
||||
for n, line in enumerate(index.find_descriptions(query), 1):
|
||||
print(line)
|
||||
counter += 1
|
||||
if counter == 0:
|
||||
msg = 'No match'
|
||||
elif counter == 1:
|
||||
msg = '1 match'
|
||||
else:
|
||||
msg = '{} matches'.format(counter)
|
||||
print('({} for {!r})'.format(msg, query))
|
||||
|
||||
print('({})'.format(index.status(query, n)))
|
||||
|
||||
if __name__ == '__main__':
|
||||
if len(sys.argv) > 1:
|
||||
|
@ -45,8 +45,7 @@ def handle(request):
|
||||
if query:
|
||||
lines = list(index.find_descriptions(query))
|
||||
res = '\n'.join(lines)
|
||||
plural = 'es' if len(lines) > 1 else ''
|
||||
msg = '{} match{} for {!r}'.format(len(lines), plural, query)
|
||||
msg = index.status(query, len(lines))
|
||||
else:
|
||||
lines = []
|
||||
res = ''
|
||||
|
@ -10,14 +10,6 @@ PROMPT = b'?> '
|
||||
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
|
||||
def handle_queries(reader, writer):
|
||||
while True:
|
||||
@ -28,21 +20,16 @@ def handle_queries(reader, writer):
|
||||
query = data.decode().strip()
|
||||
except UnicodeDecodeError:
|
||||
query = '\x00'
|
||||
if ord(query[:1]) < 32:
|
||||
break
|
||||
client = writer.get_extra_info('peername')
|
||||
print('Received from {}: {}'.format(client, query))
|
||||
lines = list(index.find_descriptions(query))
|
||||
if lines:
|
||||
writeln(writer, lines)
|
||||
plural = 'es' if len(lines) > 1 else ''
|
||||
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()
|
||||
print('Received from {}: {!r}'.format(client, query))
|
||||
if query:
|
||||
if ord(query[:1]) < 32:
|
||||
break
|
||||
lines = list(index.find_descriptions(query))
|
||||
if lines:
|
||||
writer.writelines(line.encode() + CRLF for line in lines)
|
||||
writer.write(index.status(query, len(lines)).encode() + CRLF)
|
||||
yield from writer.drain()
|
||||
|
||||
print('Close the client socket')
|
||||
writer.close()
|
||||
|
Loading…
x
Reference in New Issue
Block a user