diff --git a/prime_gen004(change).py b/prime_gen004(change).py new file mode 100644 index 0000000..36fa2fc --- /dev/null +++ b/prime_gen004(change).py @@ -0,0 +1,27 @@ +def prime_generator(top = 0): + primes: list = [] + number = 1 + while number < top: + number += 1 + if len(primes) == 0 and number == 2: + primes.append(number) + yield number + j = len(primes) - 1 + while j >= 0: + if number % primes[j] == 0: + break + j -= 1 + if j == -1: + primes.append(number) + yield number + + + +'''Проверка выражений''' +prime_list = [n for n in prime_generator(258)] +prime_gen_object = (n for n in prime_generator(123)) + +print(prime_list) +print(prime_gen_object) +for i in prime_gen_object: + print(i, end=", ") diff --git a/task1.py b/task1.py new file mode 100644 index 0000000..211f06b --- /dev/null +++ b/task1.py @@ -0,0 +1,20 @@ +def implicit_int(cls): + def wrapper(self, item): + if hasattr(object, item): + return object.__getattr__(self, attr) + else: + return 0 + cls.__getattr__ = wrapper + return cls + +'''Тесты''' + +@implicit_int +class A: + x = -1 + +a = A() +print(a) +print(a.x) +print(a.e) +print(a.e + 589) # Вывод: 589 diff --git a/task2.py b/task2.py new file mode 100644 index 0000000..a2c4716 --- /dev/null +++ b/task2.py @@ -0,0 +1,31 @@ +import time + +'''Вспомогательная функция для тестов''' +def Sum(m): + s = 0 + for i in range(0, m): + s += i + return s + +'''Менеджер контекста''' +class Timer(): + def __init__(self): + self.start = None + + def __enter__(self): + self.start = time.time() + return self + + def __exit__(self, exc_type, exc_value, exc_traceback): + print(time.time() - self.start) + +'''Проверка Менеджера контекста''' +with Timer(): + with Timer(): + print(Sum(1000)) + + with Timer(): + print('sum: ' + str(Sum(10000000)), end=', time: ') + + with Timer(): + time.sleep(1.4)