-
Notifications
You must be signed in to change notification settings - Fork 18
Lazy-load top-level imports and defer setup #439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cvanelteren
wants to merge
22
commits into
Ultraplot:main
Choose a base branch
from
cvanelteren:lazy-imports-perf
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
f47fdc7
Lazy-load top-level imports
cvanelteren 5744830
Use warnings module in internals
cvanelteren eafd077
Add eager import option and tests
cvanelteren d785584
Cover eager setup and benchmark imports
cvanelteren 7ea91e6
Add tests for lazy import coverage
cvanelteren 9731ff1
update docs
cvanelteren 23cd51d
Merge branch 'main' into lazy-imports-perf
cvanelteren 7de24b3
refactor: Automate lazy loading and fix build error
cvanelteren b1a2155
update instructions
cvanelteren 05cf17a
fix issues
cvanelteren 94bcf34
attempt fix
cvanelteren b0a06d5
attempt fix
cvanelteren 134f4a1
Fix lazy import clobbering figure
cvanelteren 2abb008
Add regression test for figure lazy import
cvanelteren ebb37ab
fixed
cvanelteren 7fb309e
Refactor lazy loader into helper module
cvanelteren bd61a35
bump
cvanelteren d15e1a9
bump
cvanelteren a87dcf0
resolve namespace collision
cvanelteren 9c99e3d
resolve namespace collision
cvanelteren 6a0c775
Merge branch 'main' into lazy-imports-perf
cvanelteren 93dda9f
Merge branch 'main' into lazy-imports-perf
cvanelteren File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| .. _lazy_loading: | ||
|
|
||
| =================================== | ||
| Lazy Loading and Adding New Modules | ||
| =================================== | ||
|
|
||
| UltraPlot uses a lazy loading mechanism to improve import times. This means that | ||
| submodules are not imported until they are actually used. This is controlled by the | ||
| :py:func:`ultraplot.__getattr__` function in :py:mod:`ultraplot`. | ||
|
|
||
| The lazy loading system is mostly automated. It works by scanning the `ultraplot` | ||
| directory for modules and exposing them based on conventions. | ||
|
|
||
| **Convention-Based Loading** | ||
|
|
||
| The automated system follows these rules: | ||
|
|
||
| 1. **Single-Class Modules:** If a module `my_module.py` has an ``__all__`` | ||
| variable with a single class or function `MyCallable`, it will be exposed | ||
| at the top level as ``uplt.my_module``. For example, since | ||
| :py:mod:`ultraplot.figure` has ``__all__ = ['Figure']``, you can access the `Figure` | ||
| class with ``uplt.figure``. | ||
|
|
||
| 2. **Multi-Content Modules:** If a module has multiple items in ``__all__`` or no | ||
| ``__all__``, the module itself will be exposed. For example, you can access | ||
| the `utils` module with :py:mod:`ultraplot.utils`. | ||
|
|
||
| **Adding New Modules** | ||
|
|
||
| When adding a new submodule, you usually don't need to modify :py:mod:`ultraplot`. | ||
| Simply follow these conventions: | ||
|
|
||
| * If you want to expose a single class or function from your module as a | ||
| top-level attribute, set the ``__all__`` variable in your module to a list | ||
| containing just that callable's name. | ||
|
|
||
| * If you want to expose the entire module, you can either use an ``__all__`` with | ||
| multiple items, or no ``__all__`` at all. | ||
|
|
||
| **Handling Exceptions** | ||
|
|
||
| For cases that don't fit the conventions, there is an exception-based | ||
| configuration. The `_LAZY_LOADING_EXCEPTIONS` dictionary in | ||
| :py:mod:`ultraplot` is used to manually map top-level attributes to | ||
| modules and their contents. | ||
|
|
||
| You should only need to edit this dictionary if you are: | ||
|
|
||
| * Creating an alias for a module (e.g., `crs` for `proj`). | ||
| * Exposing an internal variable (e.g., `colormaps` for `_cmap_database`). | ||
| * Exposing a submodule that doesn't follow the file/directory structure. | ||
|
|
||
| By following these guidelines, your new module will be correctly integrated into | ||
| the lazy loading system. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This documentation appears to be wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure I follow -- could you clarify what is wrong?