Skip to content

Improve exports of __init__.py #111

@CodexVeritas

Description

@CodexVeritas

Make it so that all of forecasting-tools is not imported all at once (or at least make the imports styled like normal packages). Right now the init.py imports everything at once on package load.

See discussion here

On a side note, I have one particular implementation objection to using Forecasting Tools, and that is how the initialization works.
It seems like when you import Forecasting Tools, it initializes all of its modules. This means if you try and load one thing, you have to "dump out the whole box" as it were. It's pretty easily fixable: https://pypi.org/project/pep562/
in short: you can expose submodules at the package top level without importing them before their first access using an override of the initialization's __getattr__ funcitonality.
(in __init__.py )
import importlib

__all__ = ["mod1", "mod2"]  # for * imports and tooling

# Optional: map names you want to expose lazily
_lazy = {"mod1": ".mod1", "mod2": ".mod2"}

def __getattr__(name):
    if name in _lazy:
        module = importlib.import_module(_lazy[name], __name__)
        globals()[name] = module        # cache so we don't import again
        return module
    raise AttributeError(name)

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions