From 7c48004aaa33c753ea7c239efa68a6fce99e2da7 Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Sat, 22 Aug 2020 05:16:25 +0200 Subject: [PATCH] Add decorator for needed packages --- decorators.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 decorators.py diff --git a/decorators.py b/decorators.py new file mode 100644 index 0000000..60b6b0a --- /dev/null +++ b/decorators.py @@ -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))