unicode_number.html


001| """Unicode Number.
001| 
001| Convert between Unicode characters and UTF-* byte strings.
001| Also a trivially simple text encoder for alpha characters.
001| 
001| Functions:
001|     encode_text
001|     decode_text
001| 
001|     random_word
001| 
001|     unicode_char_to_utf8
001|     utf8_to_unicode_char
001| 
001|     encode_text_unicode
001|     decode_text_unicode
001| 
001|     demo_unicode
001|     main
001| 
001| """

002| ####################################################################################################
003| from sys import argv
004| from os import path
005| from math import log
006| from random import randint
007| ####################################################################################################
008| 
009| codes = {chr(ord('A')+n):n+1 for n in range(26)}
010| rcodes = {v:k for k,v in codes.items()}
011| 
012| 
013| ##================================================================================================##
014| def encode_text (text:str='Goodbye') -> list[(str,int)]:
015|     "Create (char,code) tuples from a string."
016| 
017|     return [(c,codes[c]) for c in text.upper() if c in codes]
018| 
019| ##================================================================================================##
020| def decode_text (nums:list[int]):
021|     "Create string from list of codes."
022| 
023|     return ''.join(rcodes[n] for n in nums).lower()
024| 
025| 
026| ##================================================================================================##
027| def random_word (length:int=7) -> str:
028|     "Generate a random word of a given length."
029| 
030|     randlist = [randint(1,26) for _ in range(length)]
031|     randword = decode_text(randlist)
032| 
033|     return randword
034| 
035| 
036| ##================================================================================================##
037| def unicode_char_to_utf8 (char:str, encoding='utf8') -> bytes:
038|     "Convert a Unicode character to UTF bytes."
039| 
040|     return bytes(char, encoding=encoding)
041| 
042| ##================================================================================================##
043| def utf8_to_unicode_char (bs:bytes, encoding='utf8') -> str:
044|     "Convert UTF bytes to its Unicode character."
045| 
046|     return str(bs, encoding=encoding)
047| 
048| 
049| ##================================================================================================##
050| def encode_text_unicode (text:str='Wyrd Smythe', encoding='utf8') -> int:
051|     "Return text's Unicode number."
052| 
053|     bs = bytes(text, encoding=encoding)
054|     num = int.from_bytes(bs, byteorder='big')
055| 
056|     print(f'"{text}" = {num:,}')
057|     return num
058| 
059| ##================================================================================================##
060| def decode_text_unicode (num:int, encoding='utf8') -> str:
061|     "Convert an integer to text."
062| 
063|     nb = (num.bit_length() + 7) // 8
064|     bs = num.to_bytes(nb, byteorder='big')
065|     text = str(bs, encoding=encoding)
066| 
067|     print(f'"{text}" ({nb})')
068|     return text
069| 
070| 
071| ##================================================================================================##
072| def demo_unicode ():
073|     "Demonstrate text to integer."
074| 
075|     n = encode_text_unicode()
076|     print()
077| 
078|     s = '@'
079|     n = encode_text_unicode(s)
080|     print(f'{s} = {n:,}')
081|     print()
082| 
083|     s = '千里之行,始于足下'
084|     n = encode_text_unicode(s)
085|     print(f'{s} = {n:,}')
086|     print()
087| 
088| 
089| ##================================================================================================##
090| def main (*args):
091| 
092|     #demo_unicode()
093| 
094|     txt = args[0] if 0 < len(args) else 'Logos con Carne'
095|     enc = encode_text_unicode(txt)
096|     dec = decode_text_unicode(enc)
097| 
098|     return (enc, dec, txt)
099| 
100| ####################################################################################################
101| if __name__ == '__main__':
102|     print(argv[0])
103|     print()
104|     enc,dec,txt = main(*argv[1:])
105|     print()
106| ####################################################################################################
107| '''eof'''
108|