-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from DavidCEllis/capturing_importer
Add a context manager that captures import statements to populate a LazyImporter.
- Loading branch information
Showing
22 changed files
with
828 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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 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,30 @@ | ||
# Capturing import statements # | ||
|
||
There is now an experimental method to capture import statements. This works by replacing the | ||
`__import__` function within the block and restoring it afterwards. Currently this is in a | ||
separate submodule. | ||
|
||
```python | ||
from ducktools.lazyimporter import LazyImporter, get_importer_state | ||
from ducktools.lazyimporter.capture import capture_imports | ||
|
||
laz = LazyImporter() | ||
|
||
with capture_imports(laz): | ||
# Inside this block, imports are captured and converted to lazy imports on laz | ||
import functools | ||
from collections import namedtuple as nt | ||
|
||
print(get_importer_state(laz)) | ||
|
||
# Note that the captured imports are *not* available in the module namespace | ||
try: | ||
functools | ||
except NameError: | ||
print("functools is not here") | ||
``` | ||
|
||
The replaced `__import__` function wraps the original `builtins.__import__` function outside | ||
of the module it is being executed in. If you call a functiion inside the capturing block | ||
that performs an import in another module that **should** work as expected. | ||
|
This file contains 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 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 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
Oops, something went wrong.