An open API service indexing awesome lists of open source software.

https://github.com/jorenham/exports

@export decorator that adds a function or class to __all__
https://github.com/jorenham/exports

dry-python python python3 zen-of-python

Last synced: 8 months ago
JSON representation

@export decorator that adds a function or class to __all__

Awesome Lists containing this project

README

          

python-exports


The DRY alternative to __all__



exports - PyPI


exports - Python Versions


exports - license




exports - CI


exports - basedmypy


exports - basedpyright


exports - ruff

-----

## Installation

To install the `exports` package, you can use
[`python-exports` on PyPI](https://pypi.org/project/python-exports/):

```bash
pip install python-exports
```

## Usage

```pycon
>>> from exports import export
```

Now you can use it to add to `__all__` as

- function decorator

```pycon
>>> @export
... def spam():
... ...
```

- class decorator:

```pycon
>>> @export
... class Ham:
... ...
```

- by name:

```pycon
>>> from functools import reduce as fold
>>> export('fold')
```

## Behaviour

If the module has no `__all__`, it is created.
Otherwise, `__all__` is converted to a list, and the export is appended.

## Caveats

Exporting a function or class directly relies on the `__name__` attribute,
so consider the following example:

```pycon
>>> def eggs():
... ...
>>> fake_eggs = eggs
```

If we want to export fake_eggs, then this **will not work**:

```pycon
>>> export(fake_eggs) # BAD: this will add `'eggs'` to `__all__`
```

In such cases, use the name instead:

```pycon
>>> export('fake_eggs') # GOOD
```

You'll be safe if you either

- decorate a function or a class directly with `@export`,
- pass the name string when using plain `export('...')` calls.