Skip to main content

Import Python modules from JSON documents

Project description

Paker

Build Version Version

Paker is module for importing Python packages/modules from JSON documents. It was inspired by httpimporter.

Installation

From PyPI

pip install paker

From source

git clone https://github.com/desty2k/paker.git
cd paker
pip install .

Usage

In Python script

You can import Python module from dict.

import paker
import logging

MODULE = {"somemodule": {"type": "module", "code": "fun = lambda x: x**2"}}
logging.basicConfig(level=logging.NOTSET)

if __name__ == '__main__':
    with paker.loads(MODULE) as loader:
        # somemodule will be available only in this context
        from somemodule import fun
        assert fun(2), 4
        assert fun(5), 25
        print("6**2 is {}".format(fun(6)))
        print("It works!")

It is also possible to import modules from .json files. In this example we will use paker to serialize mss package.

import paker
import logging

file = "mss.json"
logging.basicConfig(level=logging.NOTSET)

# install mss using `pip install mss`
# serialize module
with open(file, "w+") as f:
    paker.dump("mss", f, indent=4)

# now you can uninstall mss using `pip uninstall mss -y`
# load package back from dump file
with open(file, "r") as f:
    loader = paker.load(f)

import mss
with mss.mss() as sct:
    sct.shot()

# remove loader and clean the cache
loader.unload()

# this will throw error
import mss

CLI

Paker can also work as a standalone script. To dump module to JSON dict use dump command:

paker dump mss

To recreate module from JSON dict use load:

paker load mss.json

Show all modules and packages in .json file

paker list mss.json

How it works

Paker implements its own importer called jsonimporter. When importing modules or packages Python iterates over importers in sys.meta_path and calls find_module method on each object. If the importer returns self, it means that the module can be imported and None means that importer did not find searched package. If any importer has confirmed the ability to import module, Python executes another method on it - load_module.

To dump module or package to JSON document, Paker recursively iterates over modules and creates dict with code and type of each module and submodules if object is package.

Bugs

Loading modules from .pyd files does not work.

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

paker-0.3.4.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

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

paker-0.3.4-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file paker-0.3.4.tar.gz.

File metadata

  • Download URL: paker-0.3.4.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

File hashes

Hashes for paker-0.3.4.tar.gz
Algorithm Hash digest
SHA256 93b5af6468c72131e5278cce98d7b4d01a6b525d6a961262ec5f0ce82e08a0ec
MD5 afcad3a69e8b5c1c5018a8ee7c105808
BLAKE2b-256 0cb7304c4487a26019ce276cd8dcfbdf7024a1b224e91ad07f6f8d1a74d373ce

See more details on using hashes here.

File details

Details for the file paker-0.3.4-py3-none-any.whl.

File metadata

  • Download URL: paker-0.3.4-py3-none-any.whl
  • Upload date:
  • Size: 10.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

File hashes

Hashes for paker-0.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 415866f05f2a65d24519835c11bce6f1948ade00d6dc3086b69b436c47f09d1e
MD5 1415b9c61e6ca98be2ecf38605d48b3b
BLAKE2b-256 ce6e052122e0ee691fecafcc2d7153b3ff0d4c5bcba3046831ba4c4452c1b1c6

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