Compare commits
3 Commits
supermaste
...
supermaste
Author | SHA1 | Date |
---|---|---|
MexTest | 27bdd6d34a | 2 years ago |
MexTest | 2c865b78b1 | 2 years ago |
MexTest | feb92564b2 | 2 years ago |
@ -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=", ") |
@ -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 |
@ -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) |
Loading…
Reference in new issue