Add solutions
This commit is contained in:
parent
0dbf9283f2
commit
59f4fd1b3e
54
CH_07_generators_and_coroutines/exercise_04/solution_01.py
Normal file
54
CH_07_generators_and_coroutines/exercise_04/solution_01.py
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import contextlib
|
||||||
|
import datetime
|
||||||
|
from solution_00 import generate_primes
|
||||||
|
|
||||||
|
|
||||||
|
@contextlib.contextmanager
|
||||||
|
def timer():
|
||||||
|
s = datetime.datetime.now()
|
||||||
|
yield
|
||||||
|
e = datetime.datetime.now()
|
||||||
|
print((e - s).total_seconds())
|
||||||
|
|
||||||
|
|
||||||
|
# copy from
|
||||||
|
# https://github.com/mastering-python/code_2/blob/master/CH_03_pythonic_syntax/T_04_simple_is_better_than_complex.rst
|
||||||
|
def primes_complicate():
|
||||||
|
sieve = dict()
|
||||||
|
for num in itertools.count(2):
|
||||||
|
if num not in sieve:
|
||||||
|
yield num
|
||||||
|
sieve[num * num] = [num]
|
||||||
|
else:
|
||||||
|
for p in sieve[num]:
|
||||||
|
sieve.setdefault(p + num, []).append(p)
|
||||||
|
del sieve[num]
|
||||||
|
|
||||||
|
|
||||||
|
# copy from
|
||||||
|
# https://github.com/mastering-python/code_2/blob/master/CH_03_pythonic_syntax/T_04_simple_is_better_than_complex.rst
|
||||||
|
def primes_complex():
|
||||||
|
sieve = itertools.count(2)
|
||||||
|
while True:
|
||||||
|
yield (prime := next(sieve))
|
||||||
|
sieve = filter(prime.__rmod__, sieve)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
print('complicate')
|
||||||
|
with timer():
|
||||||
|
list(itertools.islice(primes_complicate(), 10000))
|
||||||
|
|
||||||
|
print('complex filter')
|
||||||
|
with timer():
|
||||||
|
list(itertools.islice(primes_complex(), 10000))
|
||||||
|
|
||||||
|
print('solution_00, complex for')
|
||||||
|
with timer():
|
||||||
|
list(itertools.islice(generate_primes(), 10000))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
75
CH_13_async_io/exercise_01/solution_01.py
Normal file
75
CH_13_async_io/exercise_01/solution_01.py
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
import asyncio
|
||||||
|
|
||||||
|
|
||||||
|
class AsyncMeta(type):
|
||||||
|
|
||||||
|
def __new__(mcs, name, bases, namespace):
|
||||||
|
cls = type.__new__(mcs, name, bases, namespace)
|
||||||
|
cls._instances = []
|
||||||
|
origin_new = cls.__new__
|
||||||
|
|
||||||
|
def __new__(cls, *args, **kwargs):
|
||||||
|
ins = origin_new(cls, *args, **kwargs)
|
||||||
|
cls._instances.append(ins)
|
||||||
|
return ins
|
||||||
|
|
||||||
|
cls.__new__ = __new__
|
||||||
|
return cls
|
||||||
|
|
||||||
|
async def close_all(cls):
|
||||||
|
while cls._instances:
|
||||||
|
await cls._instances.pop().close()
|
||||||
|
|
||||||
|
async def __aenter__(cls):
|
||||||
|
return cls
|
||||||
|
|
||||||
|
async def __aexit__(cls, exc_type, exc_val, exc_tb):
|
||||||
|
await cls.close_all()
|
||||||
|
|
||||||
|
def __del__(cls):
|
||||||
|
asyncio.run(cls.close_all())
|
||||||
|
|
||||||
|
|
||||||
|
class AsyncClass(metaclass=AsyncMeta):
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.name = None
|
||||||
|
|
||||||
|
async def init(self, name):
|
||||||
|
self.name = name
|
||||||
|
print(f'init {name}')
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
async def create(cls, name):
|
||||||
|
ins = cls()
|
||||||
|
await ins.init(name)
|
||||||
|
return ins
|
||||||
|
|
||||||
|
async def close(self):
|
||||||
|
print(f'close {self.name}')
|
||||||
|
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
global AsyncClass
|
||||||
|
print('Using close method directly')
|
||||||
|
egg = await AsyncClass.create('egg')
|
||||||
|
spam = await AsyncClass.create('spam')
|
||||||
|
await AsyncClass.close_all()
|
||||||
|
|
||||||
|
print()
|
||||||
|
|
||||||
|
print('Using async with')
|
||||||
|
async with AsyncClass:
|
||||||
|
egg = await AsyncClass.create('egg')
|
||||||
|
spam = await AsyncClass.create('spam')
|
||||||
|
|
||||||
|
print()
|
||||||
|
|
||||||
|
print('Using __del__')
|
||||||
|
egg = await AsyncClass.create('egg')
|
||||||
|
spam = await AsyncClass.create('spam')
|
||||||
|
# del AsyncClass
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
asyncio.run(main())
|
||||||
Loading…
x
Reference in New Issue
Block a user