Saturday, 1 May 2021

Propagate module imports using __all__to top

I want to propagate some modules(utils) to top, but also controlling what is propagated using __all__

For example:

There is a package all and what is in __init__ are imported in alpha_utils, so from a user perspective the methods are like are coming from alpha_utils.

Also alpha_utils packages is imported by the module top_utils_module.

I want to do:

top_utils_module.beta_fn (even if beta_fn is in all package) top_utils_module.alpha_fn (even if beta is in alpha_utils package)

I can do using always import from <some_module_name> all but I want to have some control on what each module expose, by using "all".

Using a structure as below is not working, I can get what I want a level down but not two levels down.

-- top_2 utils_module

...
+-- alpha_utils
|   -- __init__
|   -- alpha_module_1
|   -- alpha_module_2
|   -- alpha_module_3

....
+-- all
|   -- __init__
|   -- all_module_1
|   -- all_module_2
|   -- all_module_3

in all folder __init__:

from .all_module_1 import fun1, fun12
from .all_module_2 import fun2
from .all_module_3 import fun3
__all__ = ["fun1", "fun2", "fun12", "fun2", "fun3"] 

in alpha_utils folder __init__:

from ..all import *
_all__ = ["alpha_module_1", "alpha_module_2", "alpha_module_3"] ## ? how to integrate ..all

in top_2 utils_module:

from some.tir import tir
from alpha_utils import *


from Propagate module imports using __all__to top

No comments:

Post a Comment