adding all files done so far
This commit is contained in:
0
learning/decorators/__init__.py
Normal file
0
learning/decorators/__init__.py
Normal file
BIN
learning/decorators/__pycache__/decorator.cpython-37.pyc
Normal file
BIN
learning/decorators/__pycache__/decorator.cpython-37.pyc
Normal file
Binary file not shown.
BIN
learning/decorators/__pycache__/decorators.cpython-37.pyc
Normal file
BIN
learning/decorators/__pycache__/decorators.cpython-37.pyc
Normal file
Binary file not shown.
BIN
learning/decorators/__pycache__/pathfix.cpython-37.pyc
Normal file
BIN
learning/decorators/__pycache__/pathfix.cpython-37.pyc
Normal file
Binary file not shown.
BIN
learning/decorators/__pycache__/script1.cpython-37.pyc
Normal file
BIN
learning/decorators/__pycache__/script1.cpython-37.pyc
Normal file
Binary file not shown.
61
learning/decorators/classes_decorater.py
Normal file
61
learning/decorators/classes_decorater.py
Normal file
@@ -0,0 +1,61 @@
|
||||
import sys
|
||||
import os
|
||||
sys.path.append(os.getcwd()) # noqa E402
|
||||
from decorator import timer, debug, repeatN, repeat_partial, count_calls, \
|
||||
Counter, Slow, slowDown
|
||||
from dataclasses import dataclass
|
||||
|
||||
|
||||
class TimeWaster(object):
|
||||
@debug
|
||||
def __init__(self, max_num: int):
|
||||
super(TimeWaster, self).__init__()
|
||||
self.max_num = max_num
|
||||
|
||||
@timer
|
||||
def waste_time(self, num_times: int):
|
||||
for _ in range(num_times):
|
||||
sum([i ** 2 for i in range(self.max_num)])
|
||||
|
||||
|
||||
@timer
|
||||
@dataclass
|
||||
class PlayingCard(object):
|
||||
rank: str
|
||||
suit: str
|
||||
|
||||
|
||||
@repeatN
|
||||
def say_hello(name: str):
|
||||
print(f'Hello, {name}.')
|
||||
|
||||
|
||||
@repeatN(num=5)
|
||||
def say_hi(name: str):
|
||||
print(f'Hi, {name}.')
|
||||
|
||||
|
||||
@repeat_partial(num=10)
|
||||
def say_yo(name: str):
|
||||
print(f'Yo, {name}!')
|
||||
|
||||
|
||||
@count_calls
|
||||
def say_whee():
|
||||
print('Whee!')
|
||||
|
||||
|
||||
@Counter
|
||||
def say_howdy():
|
||||
print('Howdy!')
|
||||
|
||||
|
||||
@slowDown()
|
||||
def count_down(num: int):
|
||||
if not isinstance(num, int):
|
||||
raise TypeError("Must input an integer.")
|
||||
if num >= 1:
|
||||
print(num)
|
||||
count_down(num - 1)
|
||||
else:
|
||||
print('Liftoff!')
|
||||
72
learning/decorators/classes_with_args.py
Normal file
72
learning/decorators/classes_with_args.py
Normal file
@@ -0,0 +1,72 @@
|
||||
import sys
|
||||
import os
|
||||
sys.path.append(os.getcwd()) # noqa E402
|
||||
import decorator
|
||||
from itertools import repeat
|
||||
|
||||
# @slowDown(1)
|
||||
# def count_down(num: int):
|
||||
# if not isinstance(num, int):
|
||||
# raise TypeError("Must input an integer.")
|
||||
# if num >= 1:
|
||||
# print(num)
|
||||
# count_down(num - 1)
|
||||
# else:
|
||||
# print('Liftoff!')
|
||||
|
||||
|
||||
# @Counter
|
||||
# def say_howdy():
|
||||
# print('Howdy!')
|
||||
|
||||
|
||||
class tester(object):
|
||||
@decorator.slowDown()
|
||||
def count(self, num):
|
||||
if not isinstance(num, int):
|
||||
raise TypeError("Must input an integer.")
|
||||
if num >= 1:
|
||||
print(num)
|
||||
self.count(num - 1)
|
||||
else:
|
||||
print('Liftoff!')
|
||||
|
||||
# print()
|
||||
# var = tester()
|
||||
# var.count(1)
|
||||
|
||||
|
||||
# @decorator.slowDown()
|
||||
# def count(num):
|
||||
# if not isinstance(num, int):
|
||||
# raise TypeError("Must input an integer.")
|
||||
# if num >= 1:
|
||||
# print(num)
|
||||
# count(num - 1)
|
||||
# else:
|
||||
# print('Liftoff!')
|
||||
|
||||
|
||||
# print()
|
||||
# count(3)
|
||||
|
||||
# @decorator.slowDown
|
||||
# def count(num):
|
||||
# if not isinstance(num, int):
|
||||
# raise TypeError("Must input an integer.")
|
||||
# if num >= 1:
|
||||
# print(num)
|
||||
# count(num - 1)
|
||||
# else:
|
||||
# print('Liftoff!')
|
||||
|
||||
|
||||
# @slowDown
|
||||
# def countme(num):
|
||||
# if not isinstance(num, int):
|
||||
# raise TypeError("Must input an integer.")
|
||||
# if num >= 1:
|
||||
# print(num)
|
||||
# countme(num - 1)
|
||||
# else:
|
||||
# print('Liftoff!')
|
||||
204
learning/decorators/decorator.py
Executable file
204
learning/decorators/decorator.py
Executable file
@@ -0,0 +1,204 @@
|
||||
import itertools
|
||||
import functools
|
||||
from time import perf_counter, sleep
|
||||
import sys
|
||||
|
||||
|
||||
def do_twice(func):
|
||||
@functools.wraps(func)
|
||||
def wrapper(*args, **kwargs):
|
||||
for _ in itertools.repeat(None, 2):
|
||||
func(*args, **kwargs)
|
||||
return func(*args, **kwargs)
|
||||
return wrapper
|
||||
|
||||
|
||||
def timer(func):
|
||||
@functools.wraps(func)
|
||||
def wrapper_timer(*args, **kwargs):
|
||||
start_time = perf_counter()
|
||||
value = func(*args, **kwargs)
|
||||
end_time = perf_counter()
|
||||
run_time = end_time - start_time
|
||||
print(f'Finished {func.__name__!r} in {run_time:.4f} secs')
|
||||
return value
|
||||
return wrapper_timer
|
||||
|
||||
|
||||
def debug(func):
|
||||
""" Print the function signature and return the value """
|
||||
@functools.wraps(func)
|
||||
def wrapper_debug(*args, **kwargs):
|
||||
args_repr = [repr(a) for a in args]
|
||||
kwargs_repr = [f'{k}={v!r}' for k, v in kwargs.items()]
|
||||
signature = ', '.join(args_repr + kwargs_repr)
|
||||
print(f'Calling {func.__name__}({signature})')
|
||||
value = func(*args, **kwargs)
|
||||
print(f'{func.__name__!r} returned {value!r}')
|
||||
return value
|
||||
return wrapper_debug
|
||||
|
||||
|
||||
def slow_down(func):
|
||||
@functools.wraps(func)
|
||||
def wrapper_slow_down(*args, **kwargs):
|
||||
sleep(1)
|
||||
return func(*args, **kwargs)
|
||||
return wrapper_slow_down
|
||||
|
||||
|
||||
def repeat(num: int):
|
||||
def decorator_repeat(func: callable):
|
||||
@functools.wraps(func)
|
||||
def wrapper_repeat(*args: list, **kwargs: dict):
|
||||
for _ in range(num):
|
||||
value = func(*args, **kwargs)
|
||||
return value
|
||||
return wrapper_repeat
|
||||
return decorator_repeat
|
||||
|
||||
|
||||
def repeatN(_func=None, *, num=2):
|
||||
def decorator_repeatN(func):
|
||||
@functools.wraps(func)
|
||||
def wrapper_repeatN(*args, **kwargs):
|
||||
for _ in range(num):
|
||||
value = func(*args, **kwargs)
|
||||
return value
|
||||
return wrapper_repeatN
|
||||
|
||||
if _func is None:
|
||||
return decorator_repeatN
|
||||
else:
|
||||
return decorator_repeatN(_func)
|
||||
|
||||
|
||||
def repeat_partial(func=None, *, num=2):
|
||||
if func is None:
|
||||
return functools.partial(repeat_partial, num=num)
|
||||
|
||||
@functools.wraps(func)
|
||||
def repeat_partial_wrapper(*args, **kwargs):
|
||||
for _ in range(num):
|
||||
value = func(*args, **kwargs)
|
||||
return value
|
||||
return repeat_partial_wrapper
|
||||
|
||||
|
||||
# def count_calls(func: callable):
|
||||
# @functools.wraps(func)
|
||||
# def wrapper_count_calls(*args, **kwargs):
|
||||
# wrapper_count_calls.num_calls += 1
|
||||
# print(f'Call {wrapper_count_calls.num_calls} of {func.__name__!r}')
|
||||
# return func(*args, **kwargs)
|
||||
# wrapper_count_calls.num_calls = 0
|
||||
# print(wrapper_count_calls.num_calls)
|
||||
# return wrapper_count_calls
|
||||
|
||||
|
||||
# def count_calls(func: callable):
|
||||
# num_calls = 0
|
||||
# print(num_calls)
|
||||
# @functools.wraps(func)
|
||||
# def wrapper_count_calls(*args, **kwargs):
|
||||
# nonlocal num_calls
|
||||
# num_calls += 1
|
||||
# print(f'Call {num_calls} of {func.__name__}')
|
||||
# return func(*args, **kwargs)
|
||||
# return wrapper_count_calls
|
||||
|
||||
|
||||
def count_calls(func=None, *, num=0):
|
||||
if func is None:
|
||||
return functools.partial(count_calls, num=num)
|
||||
|
||||
@functools.wraps(func)
|
||||
def wrapper_count_calls(*args, **kwargs):
|
||||
wrapper_count_calls.num += 1
|
||||
print(f'Call {wrapper_count_calls.num} of {func.__name__}')
|
||||
return func(*args, **kwargs)
|
||||
wrapper_count_calls.num = num
|
||||
return wrapper_count_calls
|
||||
|
||||
|
||||
class Counter(object):
|
||||
"""docstring for Counter"""
|
||||
def __init__(self, func):
|
||||
print('start init')
|
||||
super(Counter, self).__init__()
|
||||
functools.update_wrapper(self, func)
|
||||
self.func = func
|
||||
self.num_calls = 0
|
||||
print('finished init')
|
||||
|
||||
def __call__(self, *args, **kwargs):
|
||||
print('start call')
|
||||
self.num_calls += 1
|
||||
print(f'Call {self.num_calls} of {self.func.__name__!r}')
|
||||
print('finished call')
|
||||
return self.func(*args, **kwargs)
|
||||
|
||||
|
||||
# class slowDown(object):
|
||||
# """docstring for slowDown"""
|
||||
# def __init__(self, rate):
|
||||
# if callable(rate):
|
||||
# self.func = rate
|
||||
# self.rate = 1
|
||||
# else:
|
||||
# self.rate = rate
|
||||
|
||||
# def __get__(self, obj, type=None):
|
||||
# return functools.partial(self, obj)
|
||||
|
||||
# def __call__(self, *args, **kwargs):
|
||||
# if not hasattr(self, 'func'):
|
||||
# self.func = args[0]
|
||||
# return self
|
||||
# sleep(self.rate)
|
||||
# self.func(*args, **kwargs)
|
||||
|
||||
|
||||
class slowDown(object):
|
||||
"""docstring for Slow_Down"""
|
||||
def __init__(self, rate=1):
|
||||
print('init')
|
||||
self.rate = rate
|
||||
|
||||
def __call__(self, func):
|
||||
@functools.wraps(func)
|
||||
def wrapper(*args, **kwargs):
|
||||
print('wrapper')
|
||||
sleep(self.rate)
|
||||
func(*args, **kwargs)
|
||||
print('call finished')
|
||||
print(self, func)
|
||||
return wrapper
|
||||
|
||||
|
||||
# class slowDown(object):
|
||||
# """docstring for slowDown"""
|
||||
# def __init__(self, rate):
|
||||
# if callable(rate):
|
||||
# self.func = rate
|
||||
# self.rate = 1
|
||||
# print(f'no args, {locals()}, func={self.func}, rate={self.rate},\
|
||||
# self={self}')
|
||||
# else:
|
||||
# self.rate = rate
|
||||
# print(f'args set, rate={self.rate}')
|
||||
|
||||
# def __get__(self, obj, type=None):
|
||||
# print(f'get called, self={self}, obj={obj}, type={type}')
|
||||
# return functools.partial(self, obj)
|
||||
|
||||
# def __call__(self, *args, **kwargs):
|
||||
# print(f'call called, rate={self.rate}, args={args}'
|
||||
# f', kwargs={kwargs} ,self={self}')
|
||||
# print(f'locals = {locals()}')
|
||||
# if not hasattr(self, 'func'):
|
||||
# self.func = args[0]
|
||||
# print(f'args set, setting self.func to {self.func}')
|
||||
# return self
|
||||
# sleep(self.rate)
|
||||
# self.func(*args, **kwargs)
|
||||
58
learning/decorators/function_decorators.py
Normal file
58
learning/decorators/function_decorators.py
Normal file
@@ -0,0 +1,58 @@
|
||||
import sys
|
||||
import os
|
||||
sys.path.append(os.getcwd()) # noqa E402
|
||||
from decorator import timer, debug, slow_down
|
||||
import math
|
||||
import random
|
||||
|
||||
|
||||
PLUGINS = dict()
|
||||
|
||||
|
||||
@timer
|
||||
def waste_time(num):
|
||||
for _ in range(num):
|
||||
sum([i ** 2 for i in range(10000)])
|
||||
|
||||
|
||||
@debug
|
||||
def make_greeting(first_name, age: int):
|
||||
return f'Hello {first_name}, you are {age}!'
|
||||
|
||||
|
||||
math.factorial = debug(math.factorial)
|
||||
|
||||
|
||||
def approximate_e(terms):
|
||||
return sum(1 / math.factorial(n) for n in range(terms))
|
||||
|
||||
|
||||
@slow_down
|
||||
def countdown(num: int):
|
||||
if num < 1:
|
||||
print('Liftoff!')
|
||||
else:
|
||||
print(num)
|
||||
countdown(num - 1)
|
||||
|
||||
|
||||
def register(func):
|
||||
"""Register a function as a plug-in"""
|
||||
PLUGINS[func.__name__] = func
|
||||
return func
|
||||
|
||||
|
||||
@register
|
||||
def say_hello(name: str) -> str:
|
||||
return f'Hello {name}'
|
||||
|
||||
|
||||
@register
|
||||
def be_awesome(name: str) -> str:
|
||||
return f'Yo {name}, together we are awesome!'
|
||||
|
||||
|
||||
def randomly_greet(name):
|
||||
greeter, greeter_func = random.choice(list(PLUGINS.items()))
|
||||
print(f'Using {greeter!r}')
|
||||
return greeter_func(name)
|
||||
13
learning/decorators/test.py
Normal file
13
learning/decorators/test.py
Normal file
@@ -0,0 +1,13 @@
|
||||
import sys
|
||||
import os
|
||||
sys.path.append(os.getcwd()) # noqa E402
|
||||
import decorator
|
||||
import functools
|
||||
|
||||
|
||||
@functools.lru_cache(maxsize=4)
|
||||
@decorator.count_calls
|
||||
def fib(num):
|
||||
if num < 2:
|
||||
return num
|
||||
return fib(num - 1) + fib(num - 2)
|
||||
Reference in New Issue
Block a user