145 lines
3.7 KiB
Plaintext
145 lines
3.7 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import importlib\n",
|
|
"\n",
|
|
"import pathlib\n",
|
|
"from types import ModuleType\n",
|
|
"from typing import List\n",
|
|
"from pathlib import Path\n",
|
|
"\n",
|
|
"from IPython.lib import deepreload"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"=============== Importing my_pkg ===============\n",
|
|
"-------------- Importing motion.py ---------------\n",
|
|
"===================== Done =====================\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"import sys\n",
|
|
"\n",
|
|
"sys.path.insert(0, Path('apps/my_repo').resolve().as_posix())\n",
|
|
"\n",
|
|
"import my_pkg"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"['importlib._abc',\n",
|
|
" 'importlib._bootstrap',\n",
|
|
" 'importlib._bootstrap_external',\n",
|
|
" 'importlib._imp',\n",
|
|
" 'importlib.abc',\n",
|
|
" 'importlib.machinery',\n",
|
|
" 'importlib.metadata',\n",
|
|
" 'importlib.resources',\n",
|
|
" 'importlib.sys',\n",
|
|
" 'importlib.util',\n",
|
|
" 'importlib.warnings']"
|
|
]
|
|
},
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"def recursive_modules(module_obj: ModuleType) -> List[str]:\n",
|
|
" sub_modules = [\n",
|
|
" f'{module_obj.__name__}.{name}' for name in dir(module_obj)\n",
|
|
" if isinstance(getattr(module_obj, name), ModuleType)\n",
|
|
" ]\n",
|
|
" return sub_modules\n",
|
|
"\n",
|
|
"recursive_modules(importlib)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 10,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"[<module 'my_pkg' from '/home/john/conf/apps/my_repo/my_pkg/__init__.py'>, <module 'my_pkg.motion' from '/home/john/conf/apps/my_repo/my_pkg/motion.py'>, <module 'my_pkg.my_sub_pkg' from '/home/john/conf/apps/my_repo/my_pkg/my_sub_pkg/__init__.py'>, <module 'my_pkg.my_sub_pkg.hello' from '/home/john/conf/apps/my_repo/my_pkg/my_sub_pkg/hello.py'>]\n",
|
|
"=============== Importing my_pkg ===============\n",
|
|
"===================== Done =====================\n",
|
|
"-------------- Importing motion.py ---------------\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"def recursive_modules(module: ModuleType):\n",
|
|
" for name in dir(module):\n",
|
|
" sub_mod = getattr(module, name)\n",
|
|
" if isinstance(sub_mod, ModuleType) and sub_mod.__name__.startswith(module.__name__):\n",
|
|
" yield from recursive_modules(sub_mod)\n",
|
|
" yield module\n",
|
|
"\n",
|
|
"\n",
|
|
"def reload_package(pkg: ModuleType):\n",
|
|
" modules = sorted(recursive_modules(pkg), key=lambda m: m.__name__)\n",
|
|
" for mod in modules:\n",
|
|
" importlib.reload(mod)\n",
|
|
"\n",
|
|
"\n",
|
|
"reload_package(my_pkg)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"importlib.import_module(name)"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "appdaemon",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.11.8"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|