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__
- Host: GitHub
- URL: https://github.com/jorenham/exports
- Owner: jorenham
- License: mit
- Created: 2022-03-16T12:51:32.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2024-10-01T23:50:14.000Z (over 1 year ago)
- Last Synced: 2024-10-30T02:58:55.208Z (over 1 year ago)
- Topics: dry-python, python, python3, zen-of-python
- Language: Python
- Homepage:
- Size: 105 KB
- Stars: 5
- Watchers: 4
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
python-exports
The DRY alternative to __all__
-----
## 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.