updated chapter 4 and appendix-A files
This commit is contained in:
28
04-text-byte/charfinder/cf.py
Executable file
28
04-text-byte/charfinder/cf.py
Executable file
@@ -0,0 +1,28 @@
|
||||
#!/usr/bin/env python3
|
||||
import sys
|
||||
import unicodedata
|
||||
|
||||
FIRST, LAST = ord(' '), sys.maxunicode # <1>
|
||||
|
||||
|
||||
def find(*query_words, first=FIRST, last=LAST): # <2>
|
||||
query = {w.upper() for w in query_words} # <3>
|
||||
count = 0
|
||||
for code in range(first, last + 1):
|
||||
char = chr(code) # <4>
|
||||
name = unicodedata.name(char, None) # <5>
|
||||
if name and query.issubset(name.split()): # <6>
|
||||
print(f'U+{code:04X}\t{char}\t{name}') # <7>
|
||||
count += 1
|
||||
print(f'({count} found)')
|
||||
|
||||
|
||||
def main(words):
|
||||
if words:
|
||||
find(*words)
|
||||
else:
|
||||
print('Please provide words to find.')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(sys.argv[1:])
|
||||
36
04-text-byte/charfinder/cf_tests.rst
Normal file
36
04-text-byte/charfinder/cf_tests.rst
Normal file
@@ -0,0 +1,36 @@
|
||||
Doctests for ``cf.py``
|
||||
======================
|
||||
|
||||
How to run the tests
|
||||
----------------------
|
||||
|
||||
Run the ``doctest`` module from the command line::
|
||||
|
||||
$ python3 -m doctest cf_tests.rst
|
||||
|
||||
|
||||
Tests
|
||||
-----
|
||||
|
||||
Import functions for testing::
|
||||
|
||||
>>> from cf import find, main
|
||||
|
||||
Test ``find`` with single result::
|
||||
|
||||
>>> find("sign", "registered") # doctest:+NORMALIZE_WHITESPACE
|
||||
U+00AE ® REGISTERED SIGN
|
||||
(1 found)
|
||||
|
||||
|
||||
Test ``find`` with two results::
|
||||
|
||||
>>> find("chess", "queen", last=0xFFFF) # doctest:+NORMALIZE_WHITESPACE
|
||||
U+2655 ♕ WHITE CHESS QUEEN
|
||||
U+265B ♛ BLACK CHESS QUEEN
|
||||
(2 found)
|
||||
|
||||
Test ``main`` with no words::
|
||||
|
||||
>>> main([])
|
||||
Please provide words to find.
|
||||
2
04-text-byte/charfinder/test.sh
Executable file
2
04-text-byte/charfinder/test.sh
Executable file
@@ -0,0 +1,2 @@
|
||||
#!/bin/bash
|
||||
python3 -m doctest cf_tests.rst $1
|
||||
Reference in New Issue
Block a user