Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Interpret unicode character numbers.
- 34 --> QUOTATION MARK
- 0x34, x34 --> DIGIT FOUR
- 12 --> That is not a Unicode character, or unicodedata doesn't know about it
- hello --> Bad input
- exit, quit, e, q
- --> (exits the loop)
- lc, lower, lowercase, lower case
- --> (character names will be output in lowercase afterwards)
- uc, upper, uppercase, upper case, caps, capital, capitals
- --> (character names will be output in uppercase afterwards)
- cb, clipboard, clip board, copy
- --> (the most recent character name will be copied to the Windows clipboard)
- '''
- from __future__ import print_function
- from sys import version_info as _py_version
- from unicodedata import name as ud_name
- _bad_input_message = "Bad input"
- _not_a_character_message = "That is not a Unicode character, or unicodedata doesn't know about it"
- _lowercase_message = "Output will now be in lowercase"
- _uppercase_message = "Output will now be in uppercase"
- _nothing_to_copy_message = "Nothing to copy to the clipboard"
- _copied_to_clipboard_message = "Copied to the clipboard"
- _quit_synonyms = ("exit", "quit", "e", "q")
- _lowercase_synonyms = ("lc", "lower", "lowercase", "lower case")
- _uppercase_synonyms = ("uc", "upper", "uppercase", "upper case", "caps", "capital", "capitals")
- _clipboard_synonyms = ("cb", "clipboard", "clip board", "copy")
- if _py_version >= (3, 0):
- _chr = chr
- _input = input
- from tkinter import Tk as _Tk
- else:
- _chr = unichr
- _input = raw_input
- from Tkinter import Tk as _Tk
- def indent (string):
- return(" " + string)
- def interpret (char_num, lowercase = False):
- try:
- char_num = str(char_num).strip().lower()
- if char_num[:1] in ("x", "+"):
- base = 16
- char_num = char_num[1:]
- elif char_num[:2] in ("0x", "u+"):
- base = 16
- char_num = char_num[2:]
- else:
- base = 10
- char_num = int(char_num, base)
- char = _chr(char_num)
- char_num = "U+" + hex(char_num)[2:].upper().zfill(4)
- except (ValueError, OverflowError):
- return _bad_input_message
- try:
- character_name = ud_name(char)
- return char_num + " " + (character_name.lower() if lowercase else character_name)
- except ValueError:
- return _not_a_character_message
- def loop_interpret (lowercase = False):
- last_result = None
- tk = _Tk()
- tk.withdraw()
- while True:
- user_input = _input().lower()
- if user_input in _quit_synonyms:
- break
- elif user_input in _lowercase_synonyms:
- lowercase = True
- print(indent(_lowercase_message))
- elif user_input in _uppercase_synonyms:
- lowercase = False
- print(indent(_uppercase_message))
- elif user_input in _clipboard_synonyms:
- if (last_result is None):
- print(indent(_nothing_to_copy_message))
- else:
- tk.clipboard_clear()
- tk.clipboard_append(last_result)
- print(indent(_copied_to_clipboard_message))
- else:
- last_result = interpret(user_input, lowercase)
- print(indent(last_result))
- if last_result in (_bad_input_message, _not_a_character_message):
- last_result = None
- tk.destroy()
- if __name__ == "__main__":
- print("Ready to interpret", end = "\n\n")
- loop_interpret()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement