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 for classes
##Examples:
###1. Benchmark decorators
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 decorators
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 = 100
print("I'm sleeping...")
sleep(5)
print("I'v woken up")
if __name__ == '__main__':
print('First dumbass init:')
dumbass1 = SlowInitClass()
print(dumbass1.variable)
print()
print('Second dumbass init:')
dumbass2 = SlowInitClass()
print(dumbass2.variable)
print()
print('Third dumbass init:')
print('Temporary stopping singleton behaviour')
SlowInitClass().reset_singleton()
dumbass3 = SlowInitClass()
print(dumbass2.variable)
print()
print('Fourth dumbass init:')
dumbass4 = SlowInitClass()
print(dumbass4.variable)
print()
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.4.tar.gz
(14.6 kB
view hashes)
Built Distribution
funnydeco-0.1.4-py3-none-any.whl
(14.9 kB
view hashes)
Close
Hashes for funnydeco-0.1.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 41744845871d41ac469eabfb5a83d04e5ae0ab249a0d5f980ef2fb5e10393043 |
|
MD5 | a0853ca20dd2950e69578ccf493be990 |
|
BLAKE2b-256 | e973565eee35274f5dbcbb6c12c6bd7fc9fe3cfd5fdcf33a0a3e207e4cd206dc |