ex.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import unicodedata
  2. import string
  3. import re
  4. import struct
  5. def unicode_test(value):
  6. name = unicodedata.name(value)
  7. value2 = unicodedata.lookup(name)
  8. print('value = "%s", name = "%s", value2 = "%s"' % (value, name, value2))
  9. def unicode_test2():
  10. u_umlaut = '\N{LATIN SMALL LETTER U WITH DIAERESIS}'
  11. drink = 'Gew' + u_umlaut + 'rztraminer'
  12. print(drink)
  13. def encode_test():
  14. snowman = '\u2603'
  15. ds = snowman.encode('utf-8')
  16. print(ds)
  17. def decode_test():
  18. place = 'caf\u00e9'
  19. place_bytes = place.encode('utf-8')
  20. print(place_bytes)
  21. print(place_bytes.decode('utf-8'))
  22. # print(place.decode())
  23. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  24. def re_test():
  25. printable =string.printable
  26. print(printable)
  27. l1 = re.findall('\w', printable)
  28. print(l1)
  29. l2 = re.findall('\s', printable)
  30. print(l2)
  31. # Шаблоны регулярных выражений
  32. def re_template_1():
  33. source = '''I wish I may, I wish I might
  34. ... Have a dish of fish tonight.'''
  35. # поиск в любом месте строки 'wish'
  36. print(re.findall('wish', source))
  37. # поиск в любом месте строк 'wish' или 'fish'
  38. print(re.findall('wish|fish', source))
  39. # строка 'wish' в начале текства
  40. print(re.findall('^wish', source))
  41. # строка 'I wish' в начале текства
  42. print(re.findall('^I wish', source))
  43. # строка 'fish tonight.' в конце текства
  44. # сочетание '.$' - любой символ в конце строки
  45. # сочетание '\.$' - только точка в конце строки
  46. print(re.findall('fish tonight.$', source))
  47. # поиск сочетаний символов w, s и h
  48. print(re.findall('[wsh]+', source))
  49. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  50. # Структуры данных
  51. # '>LL' :
  52. # > - целые числа в формате big-endian
  53. # L - целое число типа unsigned long
  54. def test_struct_1():
  55. valid_png_header = b'\x89PNG\r\n\x1a\n'
  56. data = b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR' + \
  57. b'\x00\x00\x00\x9a\x00\x00\x00\x8d\x08\x02\x00\x00\x00\xc0'
  58. if data[:8] == valid_png_header:
  59. width, height = struct.unpack('>LL', data[16:24])
  60. print('Valid PNG, width', width, 'height', height)
  61. else:
  62. print('Not a valid PNG')
  63. #
  64. def test_struct_2():
  65. print(struct.pack('>L', 154))
  66. # Регулярные выражения
  67. # re_test()
  68. # re_template_1()
  69. # Структуры данных
  70. # test_struct_1()
  71. test_struct_2()
  72. # encode_test()
  73. # decode_test()
  74. # unicode_test('A')
  75. # unicode_test('$')
  76. # unicode_test('\u00a2')
  77. # unicode_test('\u20ac')
  78. # unicode_test('\u2603')
  79. # unicode_test2()