Collection of useful decorators for various python projects
Project description
FunnyDeco
Collection of useful decorators for python projects
Implements:
- Benchmarking util for methods
- Singleton pattern realisation for classes
- "Static" variables support in functions
Installation:
pip install funnydeco
Examples:
1. Benchmark decorator
from funnydeco.core import benchmark
# noinspection PyUnusedLocal
@benchmark
def long_loop() -> int:
"""
Demo func for example of benchmark
"""
result = 0
for i in range(int(1e7)):
result += i
print('foo')
return result
if __name__ == '__main__':
long_loop(print_benchmark=True, benchmark_name='Long loop procedure')
long_loop()
2. Singleton decorator
from funnydeco import singleton, SingletonReseter
from time import sleep
@singleton
class SlowInitClass(SingletonReseter):
"""
Demo class for example of singleton decorator
"""
def __init__(self):
self.variable = 1
print("I'm sleeping...")
sleep(3)
print("I'v woken up!")
if __name__ == '__main__':
print('First dumbass init:')
dumbass1 = SlowInitClass()
print(f'variable={dumbass1.variable}')
dumbass1.variable = 2
print()
print('Second dumbass init:')
dumbass2 = SlowInitClass()
print(f'variable={dumbass2.variable}')
print()
print('Third dumbass init:')
print('Temporary stopping singleton behaviour')
SlowInitClass().reset_singleton()
dumbass3 = SlowInitClass()
print(f'variable={dumbass3.variable}')
dumbass3.variable = 3
print()
print('Fourth dumbass init:')
dumbass4 = SlowInitClass()
print(f'variable={dumbass4.variable}')
print()
If you do not need to use reset_singleton functionality you may not to inherit your class from SingletonReseter
3. Static variables decorator
from funnydeco import static_vars
@static_vars(baz=False)
def foo(bar) -> None:
"""
Demo func for example of static_vars
"""
print(bar, foo.baz)
foo.baz = not foo.baz
if __name__ == '__main__':
foo(1)
foo(2)
foo(3)
foo(4)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
funnydeco-0.1.7.tar.gz
(15.0 kB
view hashes)
Built Distribution
funnydeco-0.1.7-py3-none-any.whl
(15.3 kB
view hashes)
Close
Hashes for funnydeco-0.1.7-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a2ce8221e5c760c9d5a6f23784a2944a8ab5e8edacdc2ddf0cfcbf800cc75f9b |
|
MD5 | 7f139333da78311a10b00518418da498 |
|
BLAKE2b-256 | d474eb341c985fc9218168fa280047003a253b1b3c4a48a6dc6f161e6bb607e0 |