Skip to content

frostming/modul

Repository files navigation

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.

About

Python modules with public exports

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages