Giter VIP home page Giter VIP logo

modlib's Introduction

modlib

Latest Version Build Status Coverage Status Code Quality MIT Licensed

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 }

The module's object is returned by .get(), so my_module = modlib.Modstack.get('foobar') is equivalent to import foobar as my_module: if foobar.py has a "say_something" function, you'd call it with my_module.say_something()

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 }

When specifying a target object, .get() returns that object. So if you have foobar.py that defines say_something(), loading the module with my_method = modlib.Modstack(target='say_something').get('foobar') would let you run say_something as my_method()

Installation

pip install modlib

License

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

modlib's People

Contributors

akerl avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

ryanwohara

modlib's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.