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
Installation:
pip install funnydeco
Examples:
1. Benchmark decorator
from funnydeco import benchmark
# noinspection PyUnusedLocal
@benchmark
def long_loop(print_benchmark=False, benchmark_name='') -> int:
"""
Demo class for example of benchmark
"""
result = 0
for i in range(int(1e7)):
result += i
return result
if __name__ == '__main__':
print('With benchmark:')
long_loop(print_benchmark=True, benchmark_name='Long loop procedure')
print('Without benchmark:')
long_loop()
2. Singleton decorator
from funnydeco.core 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
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.6.tar.gz
(14.8 kB
view hashes)
Built Distribution
funnydeco-0.1.6-py3-none-any.whl
(15.2 kB
view hashes)
Close
Hashes for funnydeco-0.1.6-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 939da43e30d077045b0646dcd84053d568da9ee2c34f24eba3a43f312ec7063a |
|
MD5 | c52cec9fc9ff689a7610caf6b5452a6f |
|
BLAKE2b-256 | 4bb7bf2ea321961c9d76a1e3dbc6fe214faab7494fea26934d3605d0fd08b4f7 |