Advertisement
Guest User

Untitled

a guest
Jun 24th, 2014
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. '''
  2. Interpret unicode character numbers.
  3.  
  4. 34 --> QUOTATION MARK
  5. 0x34, x34 --> DIGIT FOUR
  6. 12 --> That is not a Unicode character, or unicodedata doesn't know about it
  7. hello --> Bad input
  8.  
  9. exit, quit, e, q
  10. --> (exits the loop)
  11. lc, lower, lowercase, lower case
  12. --> (character names will be output in lowercase afterwards)
  13. uc, upper, uppercase, upper case, caps, capital, capitals
  14. --> (character names will be output in uppercase afterwards)
  15. cb, clipboard, clip board, copy
  16. --> (the most recent character name will be copied to the Windows clipboard)
  17. '''
  18.  
  19. from __future__ import print_function
  20. from sys import version_info as _py_version
  21. from unicodedata import name as ud_name
  22.  
  23. _bad_input_message = "Bad input"
  24. _not_a_character_message = "That is not a Unicode character, or unicodedata doesn't know about it"
  25. _lowercase_message = "Output will now be in lowercase"
  26. _uppercase_message = "Output will now be in uppercase"
  27. _nothing_to_copy_message = "Nothing to copy to the clipboard"
  28. _copied_to_clipboard_message = "Copied to the clipboard"
  29.  
  30. _quit_synonyms = ("exit", "quit", "e", "q")
  31. _lowercase_synonyms = ("lc", "lower", "lowercase", "lower case")
  32. _uppercase_synonyms = ("uc", "upper", "uppercase", "upper case", "caps", "capital", "capitals")
  33. _clipboard_synonyms = ("cb", "clipboard", "clip board", "copy")
  34.  
  35. if _py_version >= (3, 0):
  36. _chr = chr
  37. _input = input
  38. from tkinter import Tk as _Tk
  39. else:
  40. _chr = unichr
  41. _input = raw_input
  42. from Tkinter import Tk as _Tk
  43.  
  44. def indent (string):
  45. return(" " + string)
  46.  
  47. def interpret (char_num, lowercase = False):
  48. try:
  49. char_num = str(char_num).strip().lower()
  50. if char_num[:1] in ("x", "+"):
  51. base = 16
  52. char_num = char_num[1:]
  53. elif char_num[:2] in ("0x", "u+"):
  54. base = 16
  55. char_num = char_num[2:]
  56. else:
  57. base = 10
  58. char_num = int(char_num, base)
  59. char = _chr(char_num)
  60. char_num = "U+" + hex(char_num)[2:].upper().zfill(4)
  61. except (ValueError, OverflowError):
  62. return _bad_input_message
  63. try:
  64. character_name = ud_name(char)
  65. return char_num + " " + (character_name.lower() if lowercase else character_name)
  66. except ValueError:
  67. return _not_a_character_message
  68.  
  69. def loop_interpret (lowercase = False):
  70. last_result = None
  71. tk = _Tk()
  72. tk.withdraw()
  73. while True:
  74. user_input = _input().lower()
  75. if user_input in _quit_synonyms:
  76. break
  77. elif user_input in _lowercase_synonyms:
  78. lowercase = True
  79. print(indent(_lowercase_message))
  80. elif user_input in _uppercase_synonyms:
  81. lowercase = False
  82. print(indent(_uppercase_message))
  83. elif user_input in _clipboard_synonyms:
  84. if (last_result is None):
  85. print(indent(_nothing_to_copy_message))
  86. else:
  87. tk.clipboard_clear()
  88. tk.clipboard_append(last_result)
  89. print(indent(_copied_to_clipboard_message))
  90. else:
  91. last_result = interpret(user_input, lowercase)
  92. print(indent(last_result))
  93. if last_result in (_bad_input_message, _not_a_character_message):
  94. last_result = None
  95. tk.destroy()
  96.  
  97. if __name__ == "__main__":
  98. print("Ready to interpret", end = "\n\n")
  99. loop_interpret()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement