TelenkovDmitry 1 year ago
parent
commit
73b583d6b1
4 changed files with 207 additions and 36 deletions
  1. 0 36
      exer/data/ex.py
  2. 43 0
      exer/data_time.py
  3. 101 0
      exer/ex.py
  4. 63 0
      exer/time_1.py

+ 0 - 36
exer/data/ex.py

@@ -1,36 +0,0 @@
-import unicodedata
-
-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())
-
-
-# encode_test()
-decode_test()
-
-# unicode_test('A')
-# unicode_test('$')
-# unicode_test('\u00a2')
-# unicode_test('\u20ac')
-# unicode_test('\u2603')
-
-# unicode_test2()
-

+ 43 - 0
exer/data_time.py

@@ -0,0 +1,43 @@
+import calendar
+from datetime import date, timedelta, time, datetime
+
+def test_1():
+    print(calendar.isleap(1900))
+    print(calendar.isleap(2000))
+    print(calendar.isleap(1901))
+
+def test_2():
+    halloween = date(2019, 10, 31)
+    print(halloween)
+    print(date.today())
+
+def test_3():
+    one_day = timedelta(days=1)
+    now = date.today()
+    tomorrow = now + one_day
+    print(tomorrow)
+    print(now + 17*one_day)
+
+def test_4():
+    noon = time(12, 0, 0)
+    print(noon)
+
+def test_5():
+    some_day = datetime(2019, 1, 2, 3, 4, 5, 6)
+    print(some_day)
+    print(datetime.now())
+
+def test_6():
+    noon = time(12)
+    this_day = date.today()
+    noon_today = datetime.combine(this_day, noon)
+    print(noon_today)
+    print(noon_today.date())
+    print(noon_today.time())
+
+# test_1()
+# test_2()
+# test_3()
+# test_4()
+# test_5()
+test_6()

+ 101 - 0
exer/ex.py

@@ -0,0 +1,101 @@
+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()
+

+ 63 - 0
exer/time_1.py

@@ -0,0 +1,63 @@
+import time
+import locale
+from datetime import date
+
+def test_1():
+    now = time.time()
+    print(now)
+
+    # Преобразование epoch в строку
+    print(time.ctime(now))
+
+    # Время в текущем часовом поясе
+    print(time.localtime())
+
+    # Время в UTC
+    print(time.gmtime())
+
+    # Преобразование в формат epoch
+    print(time.mktime(time.localtime()))
+
+def test_2():
+    now = time.localtime()
+    print(now[0])
+    print(list(now[x] for x in range(9)))
+
+# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+# Формат ввода/вывода времени и даты
+    
+# Время в строку
+def test_3():
+    fmt = "It's %A, %B, %d, %Y, local time %I:%M:%S%p"
+    t = time.localtime()
+    print(t)
+    print(time.strftime(fmt, t))
+
+# Строку во время
+def test_4():
+    fmt = "%Y-%m-%d"
+    print(time.strptime("2019-01-29", fmt))
+
+# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+    
+# Настройка локальных параметров даты и времени
+    
+def test_5():
+    # print(locale.locale_alias.keys())
+    ru = [name for name in locale.locale_alias.keys() if name.startswith('ru')]
+    print(ru)
+    day = date(2024, 7, 31)
+    print(day)
+    print(day.strftime('%A, %B %d'))
+    locale.setlocale(locale.LC_TIME, ru[0])
+    print(day.strftime('%A, %B %d'))
+
+
+
+
+# test_1()
+# test_2()
+# test_3()
+# test_4()
+test_5()