Skip to main content

@export decorator that adds a function or class to __all__

Project description

python-exports

The DRY alternative to __all__

exports - PyPI exports - Python Versions exports - license

exports - CI exports - pre-commit exports - basedmypy exports - basedpyright exports - ruff


Installation

To install the exports package, you can use python-exports on PyPI:

pip install python-exports

Usage

>>> from exports import export

Now you can use it to add to __all__ as

  • function decorator

    >>> @export
    ... def spam():
    ...     ...
    
  • class decorator:

    >>> @export
    ... class Ham:
    ...     ...
    
  • by name:

    >>> 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:

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

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

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

In such cases, use the name instead:

>>> 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.

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

python_exports-1.2.0.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

python_exports-1.2.0-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file python_exports-1.2.0.tar.gz.

File metadata

  • Download URL: python_exports-1.2.0.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.5 Linux/6.9.3-76060903-generic

File hashes

Hashes for python_exports-1.2.0.tar.gz
Algorithm Hash digest
SHA256 f5ef300c20f4dfcaf5b324f1449550d4ea0d1c4048fd390e6f00f6bd69b977ad
MD5 8413f7d6f83065d08a791d6a02415904
BLAKE2b-256 f68358acae682edb28a9a97d2c67f46e9650608df044c5d7fd8e05c71da58ced

See more details on using hashes here.

File details

Details for the file python_exports-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: python_exports-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.5 Linux/6.9.3-76060903-generic

File hashes

Hashes for python_exports-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cb599fb29fbe667914ba354f6692afb3f0bedfbcf0cf3ac75e55343a86247c43
MD5 971bc38dae5ac9167eab8a9a038b35bc
BLAKE2b-256 bec9e3f578b6c4994829f71c2aa910d981c57a553b309b5fa5fc2c1ad7df5733

See more details on using hashes here.

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