apipkg: namespace control and lazy-import mechanism
Project description
Welcome to apipkg !
With apipkg you can control the exported namespace of a Python package and greatly reduce the number of imports for your users. It is a small pure Python module that works on CPython 3.7+, Jython and PyPy. It cooperates well with Python’s help() system, custom importers (PEP302) and common command-line completion tools.
Usage is very simple: you can require ‘apipkg’ as a dependency or you can copy paste the ~200 lines of code into your project.
Tutorial example
Here is a simple mypkg package that specifies one namespace and exports two objects imported from different modules:
# mypkg/__init__.py
import apipkg
apipkg.initpkg(__name__, {
'path': {
'Class1': "_mypkg.somemodule:Class1",
'clsattr': "_mypkg.othermodule:Class2.attr",
}
}
The package is initialized with a dictionary as namespace.
You need to create a _mypkg package with a somemodule.py and othermodule.py containing the respective classes. The _mypkg is not special - it’s a completely regular Python package.
Namespace dictionaries contain name: value mappings where the value may be another namespace dictionary or a string specifying an import location. On accessing an namespace attribute an import will be performed:
>>> import mypkg
>>> mypkg.path
<ApiModule 'mypkg.path'>
>>> mypkg.path.Class1 # '_mypkg.somemodule' gets imported now
<class _mypkg.somemodule.Class1 at 0xb7d428fc>
>>> mypkg.path.clsattr # '_mypkg.othermodule' gets imported now
4 # the value of _mypkg.othermodule.Class2.attr
The mypkg.path namespace and its two entries are loaded when they are accessed. This means:
lazy loading - only what is actually needed is ever loaded
only the root “mypkg” ever needs to be imported to get access to the complete functionality
the underlying modules are also accessible, for example:
from mypkg.sub import Class1
Including apipkg in your package
If you don’t want to add an apipkg dependency to your package you can copy the apipkg.py file somewhere to your own package, for example _mypkg/apipkg.py in the above example. You then import the initpkg function from that new place and are good to go.
Feedback?
If you have questions you are welcome to
join the #pytest channel on irc.libera.chat (using an IRC client, via webchat, or via Matrix).
create an issue on the bugtracker
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
File details
Details for the file apipkg-3.0.2.tar.gz
.
File metadata
- Download URL: apipkg-3.0.2.tar.gz
- Upload date:
- Size: 6.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c7aa61a4f82697fdaa667e70af1505acf1f7428b1c27b891d204ba7a8a3c5e0d |
|
MD5 | e07ca259f7bc1581b9e7f4a4f96b08ff |
|
BLAKE2b-256 | 2e2c717870bddbb588428093de4bfbce59c58d29b56bff76ecd422fd1724ae72 |
File details
Details for the file apipkg-3.0.2-py3-none-any.whl
.
File metadata
- Download URL: apipkg-3.0.2-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a16984c39de280701f3f6406ed3af658f2a1965011fe7bb5be34fbb48423b411 |
|
MD5 | eaca23728f0de54deb657aeba4e00372 |
|
BLAKE2b-256 | 716b11ee9d7d046d7012822986e42619eee7fe1522147cd3c6beab1ebcec9f33 |