Add decorator for needed packages
This commit is contained in:
parent
4b384d27a9
commit
7c48004aaa
43
decorators.py
Normal file
43
decorators.py
Normal file
@ -0,0 +1,43 @@
|
||||
from functools import wraps
|
||||
|
||||
installed_packages = []
|
||||
|
||||
|
||||
def install_package(pkg):
|
||||
if pkg not in installed_packages:
|
||||
installed_packages.append(pkg)
|
||||
print(f"Installed: {pkg}")
|
||||
else:
|
||||
print(f"Already installed: {pkg}")
|
||||
|
||||
|
||||
def needs_packages(packages):
|
||||
def inner_decorator(function):
|
||||
@wraps(function)
|
||||
def execution_wrapper(*args, **kwargs):
|
||||
[install_package(package) for package in packages]
|
||||
return function(*args, **kwargs)
|
||||
|
||||
return execution_wrapper
|
||||
|
||||
return inner_decorator
|
||||
|
||||
|
||||
@needs_packages(['vim', 'calc'])
|
||||
def add(z1, z2):
|
||||
return z1 + z2
|
||||
|
||||
|
||||
@needs_packages(['calc', 'sed'])
|
||||
def sub(z1, z2):
|
||||
return z1 - z2
|
||||
|
||||
|
||||
@needs_packages(['calc'])
|
||||
def bla(z1, z2):
|
||||
return z1 / z2
|
||||
|
||||
|
||||
print(add(1, 2))
|
||||
print(sub(1, 2))
|
||||
print(bla(1, 2))
|
Loading…
Reference in New Issue
Block a user