Last active
April 24, 2021 10:38
-
-
Save goltsevnet/a198f0b4f9d9818f7b6bb7cbdcc0c3f0 to your computer and use it in GitHub Desktop.
Сбощик функций
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from inspect import getmembers, isfunction | |
| import os | |
| import pkgutil | |
| # for example, let's take all the functions of the os module | |
| # для примера возьмем все функции модуля OS | |
| class CollectFunctions: | |
| def __init__(self): | |
| self.functions = dict() | |
| for _loader, _module_name, _ in pkgutil.walk_packages(os.__path__): | |
| _module = _loader.find_module(_module_name).load_module(_module_name) | |
| for attr_name in dir(_module): | |
| if attr_name.startswith("__"): | |
| continue | |
| attr_value = getattr(_module, attr_name) | |
| # we will also place all functions in the dict for call () | |
| # так же поместим все функции в дикт для call() | |
| self.functions.update({attr_name: attr_value}) | |
| if isfunction(attr_value) and attr_value.__module__ == _module.__name__: | |
| setattr(self, attr_name, attr_value) | |
| async def call(self, function_name): | |
| await self.functions[function_name](self) | |
| await CollectFunctions().call('function_name') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment