import unicodedata import string import re import struct def unicode_test(value): name = unicodedata.name(value) value2 = unicodedata.lookup(name) print('value = "%s", name = "%s", value2 = "%s"' % (value, name, value2)) def unicode_test2(): u_umlaut = '\N{LATIN SMALL LETTER U WITH DIAERESIS}' drink = 'Gew' + u_umlaut + 'rztraminer' print(drink) def encode_test(): snowman = '\u2603' ds = snowman.encode('utf-8') print(ds) def decode_test(): place = 'caf\u00e9' place_bytes = place.encode('utf-8') print(place_bytes) print(place_bytes.decode('utf-8')) # print(place.decode()) # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ def re_test(): printable =string.printable print(printable) l1 = re.findall('\w', printable) print(l1) l2 = re.findall('\s', printable) print(l2) # Шаблоны регулярных выражений def re_template_1(): source = '''I wish I may, I wish I might ... Have a dish of fish tonight.''' # поиск в любом месте строки 'wish' print(re.findall('wish', source)) # поиск в любом месте строк 'wish' или 'fish' print(re.findall('wish|fish', source)) # строка 'wish' в начале текства print(re.findall('^wish', source)) # строка 'I wish' в начале текства print(re.findall('^I wish', source)) # строка 'fish tonight.' в конце текства # сочетание '.$' - любой символ в конце строки # сочетание '\.$' - только точка в конце строки print(re.findall('fish tonight.$', source)) # поиск сочетаний символов w, s и h print(re.findall('[wsh]+', source)) # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Структуры данных # '>LL' : # > - целые числа в формате big-endian # L - целое число типа unsigned long def test_struct_1(): valid_png_header = b'\x89PNG\r\n\x1a\n' data = b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR' + \ b'\x00\x00\x00\x9a\x00\x00\x00\x8d\x08\x02\x00\x00\x00\xc0' if data[:8] == valid_png_header: width, height = struct.unpack('>LL', data[16:24]) print('Valid PNG, width', width, 'height', height) else: print('Not a valid PNG') # def test_struct_2(): print(struct.pack('>L', 154)) # Регулярные выражения # re_test() # re_template_1() # Структуры данных # test_struct_1() test_struct_2() # encode_test() # decode_test() # unicode_test('A') # unicode_test('$') # unicode_test('\u00a2') # unicode_test('\u20ac') # unicode_test('\u2603') # unicode_test2()