find-uname.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/usr/bin/env python3
  2. """
  3. For each argument on the command line, look for it in the set of all Unicode
  4. names. Arguments are treated as case-insensitive regular expressions, e.g.:
  5. % find-uname 'small letter a$' 'horizontal line'
  6. *** small letter a$ matches ***
  7. LATIN SMALL LETTER A (97)
  8. COMBINING LATIN SMALL LETTER A (867)
  9. CYRILLIC SMALL LETTER A (1072)
  10. PARENTHESIZED LATIN SMALL LETTER A (9372)
  11. CIRCLED LATIN SMALL LETTER A (9424)
  12. FULLWIDTH LATIN SMALL LETTER A (65345)
  13. *** horizontal line matches ***
  14. HORIZONTAL LINE EXTENSION (9135)
  15. """
  16. import unicodedata
  17. import sys
  18. import re
  19. def main(args):
  20. unicode_names = []
  21. for ix in range(sys.maxunicode+1):
  22. try:
  23. unicode_names.append((ix, unicodedata.name(chr(ix))))
  24. except ValueError: # no name for the character
  25. pass
  26. for arg in args:
  27. pat = re.compile(arg, re.I)
  28. matches = [(y,x) for (x,y) in unicode_names
  29. if pat.search(y) is not None]
  30. if matches:
  31. print("***", arg, "matches", "***")
  32. for match in matches:
  33. print("%s (%d)" % match)
  34. if __name__ == "__main__":
  35. main(sys.argv[1:])