Module importer with its own namespace and reflection feature for python.
Project description
moduleimporter_python
Module importer with its own namespace and reflection feature for python
Introduction
This importer works like Java URLClassLoader that you can import modules have same name or different versions without conflict. When you do not want to import a module to global namespace sys.modules, it also works.
Every importer of this library will create its own namespace when initializing, and all modules imported by this importer will be injected into importer's namespace, rather than sys.modules.
The package also provide wrapper for python modules, methods, classes and attributes to implement reflection.
Two high level importers are provided, including PathModuleImporter as default and NsModuleImporter as optional if you have another implementation of namespace importer (refer to nsimport) installed .
-
PathModuleImporter uses this package's util module which re-implement importlib with namespace specific so that it has its own namespace when instance is created.
-
NsModuleImporter uses importlib as implementation but creates new namespace and bind it to
_bootstrap and
_bootstrap_external
. It has its own namespace after_bootstrap
is inintialized.
APIs
Available module can be checked in __init__.py
, and the main functions to load modules can be checked in _PathModuleImporter
of Importer.py
and util.py
. The functions are short enough to read.
Functions and classes of reflection can be checked in _wrapper.py
.
Generally, you can use moduleimporter.PathModuleImporter(path)
to initialize a importer. Path is a list where importer to find modules, and it works like sys.path. Path will be initialized as path + sys.path to allow to import other package.
importer.load_module(name_of_module)
returns a wrapped module that supports reflection.
Access importer's attribute with importer.modules
, importer.path
and importer.meta_path
etc.
Access wrapped module with importer.unwrap(to_unwrap_object)
or wrapped.module
.
Example
>>> import sys
>>> import moduleimporter as MI
>>> importer = MI.PathModuleImporter(['./'])
>>> importer
<moduleimporter.Importer.PathModuleImporter object at 0x7f976241de50>
>>> importer.load_module('codeop')
<moduleimporter._wrapper.PyModule object at 0x7f976222fb50>
>>> 'codeop' in sys.modules
False
>>> 'codeop' in importer.modules
True
>>> random_module = importer.load_module('random')
>>> random_module
<moduleimporter._wrapper.PyModule object at 0x7f976222fb50>
>>> rand_int = random_module.get_method('randint')
>>> rand_int
<moduleimporter._wrapper.PyMethod object at 0x7f97620fb550>
>>> rand_int.invoke(1, 10)
10
>>> rand_int.invoke(1, 10)
4
>>> random_module.get_attr('NV_MAGICCONST')
<moduleimporter._wrapper.PyAttr object at 0x7f9762107640>
>>> random_module.get_class('Random')
<moduleimporter._wrapper.PyClass object at 0x7f97620847c0>
>>> rand_int.get_parameter_names()
['a', 'b']
Notice
Every importer has its own namespace, and all modules loaded by now loading module will be injected to importer's namespace rather than global sys.modules.
Only test on python3.8, and you can run scripts in tests to check if the library works.
Core implementation to load module to namespace is in util. Importer initialization uses functools
to bind a namespace to import function.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for moduleimporter-0.1.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d3b5e06ba36c074dbb04b808bfe83103d6642d94be846cb1077e34cdb28ca0ba |
|
MD5 | 1b6cd043a34b9b43e84cd8822d630a4c |
|
BLAKE2b-256 | ec4ab18e9aba3df300e81a4cb0032b025b7fc3bf713c19233ba751f80a71c34f |