Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

moduleimporter-1.0.0.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

moduleimporter-1.0.0-py2.py3-none-any.whl (12.3 kB view details)

Uploaded Python 2Python 3

File details

Details for the file moduleimporter-1.0.0.tar.gz.

File metadata

  • Download URL: moduleimporter-1.0.0.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.8.10

File hashes

Hashes for moduleimporter-1.0.0.tar.gz
Algorithm Hash digest
SHA256 8b12e5169f7ac0add1f49c71ed76283419628638aa7abf9d414a4e5e3832d47e
MD5 df95e61938d397406e6720c50e10378b
BLAKE2b-256 c6fc3221bf18f120323115fbb1deab5d92fcd9546ab8a21a3a8e31bb9d4401a8

See more details on using hashes here.

File details

Details for the file moduleimporter-1.0.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for moduleimporter-1.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 e5a3cebed04298b98e8fae0c545a4f6eaf13fca8a1a3ad5d1737655eebbdb7ac
MD5 ee6bf11b841235508f991469646b5d66
BLAKE2b-256 b18ee9b08ec1b289e544c499b5856bd891b4c6bc3be192b5d60b089fd96b2bd6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page