Skip to main content

Dynamic module loader

Project description

modlib
=========

[![Latest Version](https://img.shields.io/pypi/v/modlib.svg?style=flat)](https://pypi.python.org/pypi/modlib/)
[![Coverage Status](https://img.shields.io/coveralls/akerl/modlib.svg?style=flat)](https://coveralls.io/r/akerl/modlib)
[![Build Status](https://img.shields.io/travis/akerl/modlib.svg?style=flat)](https://travis-ci.org/akerl/modlib)
[![MIT Licensed](https://img.shields.io/badge/license-MIT-green.svg?style=flat)](https://tldrlegal.com/license/mit-license)

Simplifies importing dynamic modules. It exposes the Modstack class, which collects modules as you import them. If you need the same module again, it will return the existing module from the stack rather than reimporting it. It also supports getting a specific item from the modules rather than the full module

## Usage

The basic usage is pretty straightforward:

```
import modlib

stack = modlib.Modstack
names = ['foo.bar', 'alpha', 'hello.world']
modules = { x: stack.get(x) for x in names }
```

It's possible to use a custom formula for your module path. The default is `'{0}'`, which takes the name you pass to `.get()` directly. This is useful if your modules follow a common pattern. For instance, if your modules are stored in `./modules/{kind}/{name}`, you can pass that parameter to Modstack:

```
import modlib

stack = modlib.Modstack(formula='modules.{kind}.{name}')
fruits = ['apple', 'orange', 'tomato']
fruit_modules = { x: stack.get(kind='fruit', name=x) for x in fruits }
```

You can also specify a target object, and only that object will be pulled from the module. For instance, if you want to pull the run() method from each of your modules:

```
import modlib

stack = modlib.Modstack(target='run')
actions = ['jump', 'swim', 'bike']
action_methods = { x: stack.get(x) for x in names }
```

## Installation

pip install modlib

## License

modlib is released under the MIT License. See the bundled LICENSE file for details.

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

modlib-1.0.0.tar.gz (2.8 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: modlib-1.0.0.tar.gz
  • Upload date:
  • Size: 2.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for modlib-1.0.0.tar.gz
Algorithm Hash digest
SHA256 3b2c178943e5e48721a8d343d5ee0a27cc0e062bdee76d0e8e47d3291eb0c2bb
MD5 f84bc73168157e2ee51b25b8119695d7
BLAKE2b-256 b4e41ca472fe0116e5e07114a13d6d35c529999ea0a3d698e29fd983e39065a0

See more details on using hashes here.

Supported by

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