Skip to main content

Control the exported members for your modules

Project description

Modul

/moˈduːl/

Tests pypi version Code style: black pdm-managed

Control the exported members for your modules

Requirements

Modul requires Python >=3.7

Installation

$ python -m pip install modul

Modul is a single-file module with less than 200 lines of code and no dependencies. It can be easily copied into your project.

Quick start

Write a module exporting limited members:

# mymodule.py
from modul import exports


@exports
def foo():
    return 42


baz = "unexported"
bar = "hello"

exports.bar = bar

In another module or REPL:

>>> import mymodule
>>> mymodule.foo()
42
>>> mymodule.bar
"hello"
>>> mymodule.baz
AttributeError: Module test has no attribute baz
>>> mymodule.__all__
['foo', 'bar']

Usage

  1. Export a function with decorator:

    @exports
    def foo():
        return 42
    
  2. Export a variable with attribute set:

    exports.bar = 42
    

    Note that to use the variable inside the module, you still need to declare a variable for it:

    bar = 42
    exports.bar = bar
    
  3. Export a variable with item set:

    exports["bar"] = 42
    

    Besides, the exports object supports all APIs of dict:

    exports.update({"bar": 42})
    
  4. Export a map of (name, value) pairs:

    exports({
        "bar": 42,
        "baz": "hello"
    })
    
  5. You can even have conditional exports and exports from function call:

    flag = True
    if flag:
        exports.foo = 42
    
    def export_bar():
        exports.bar = 42
    export_bar()
    
  6. Alternatively, you can assign members to the exports attribute of the module:

    import modul
    
    modul.exports = {
        "bar": 42,
        "baz": "hello"
    }
    

    Note that you can't use exports = <variable> in this case, because it will lose the reference to the API. And each assignment will overwrite the previous one so there can be only one assignment in your module.

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

modul-0.2.0.post0.tar.gz (5.1 kB view hashes)

Uploaded Source

Built Distribution

modul-0.2.0.post0-py3-none-any.whl (4.4 kB view hashes)

Uploaded Python 3

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