Skip to main content

Module Hygiene

Simple tools to help with Python namespace hygiene!

Description

This is an opinionated project! I prefer Python modules to only include what's necessary for users. If I'm in an IDE and type from module import <TAB>, I want only API-level elements to be available! Many Python projects use a leading underscore to specify private-like objects and modules. Still, it's sometimes nice to keep things simple without multiple _module definitions in a package.

This package is my solution! It currently provides a single function, cleanup, which returns code to delete all locals which are not in a provided "export list".

Usage

See the example usage below. Note that we need to import T to add a proper typed signature for the function square. Unfortunately, this means that T is also available to anyone who imports this module.

By default, cleanup assumes the name of the module's export list is '__export__'. If you want to choose a different export list name, pass that name as a str to cleanup!

"""module.py

A python module which exports a single function, `square`.
"""

from hygiene import cleanup
from typing  import T 

__export__ = [
    "square",
]

def square(x: T) -> T:
    """Returns the square of x!"""
    return x ** 2

if __name__ != "__main__":
    cleanup()

This cleanup approach will likely change how you write Python modules. If you need a Python package throughout your module, like numpy, you likely import numpy as np at the top of your module, and use np in various functions throughout the module. If you cleanup at the end of the module, your code will break on execution because np will no longer exist!

If you choose to use cleanup, then you will need to import modules at the function level. Personally, I like this better anyways! Every dependency is right next to where it's used. One major downside of this approach is you need to parse your source code for import statements to track all dependencies.

"""another.py

Another module which exports a single function, `abssqrt`!
"""

from hygiene import cleanup
from typing  import T

# Don't do this!
# from numpy import abs, sqrt

__export_list__ = [
    "abssqrt",
]

def abssqrt(x: T) -> T:
    """Returns the square root of the absolute value of `x`!"""
    from numpy import abs, sqrt
    return sqrt(abs(x))

if __name__ != "__main__":
    cleanup(export = __export_list__)

Metadata

Release files for module-hygiene 0.3.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for module-hygiene 0.3.4
File Size Uploaded Provenance Actions
module_hygiene-0.3.4.tar.gz 4.0 kB Without provenance Details for module_hygiene-0.3.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for module-hygiene 0.3.4
File Interpreter ABI Platform Actions
module_hygiene-0.3.4-py3-none-any.whl Python 3 none any Details for module_hygiene-0.3.4-py3-none-any.whl

Total release size: 8.5 kB

Release files / module_hygiene-0.3.4.tar.gz

Download URL module_hygiene-0.3.4.tar.gz
Size 4.0 kB
Tags Source
SHA-256 checksum
How to use checksums
cfd3582e26f5cfbb2deeffcb4f34e27d5ce34dea18fad785c0cc6b8734bc2cbe
BLAKE2b-256 checksum
How to use checksums
5e526db0c4f00ab450524cdfaf2f70a4efdf36078f01a1b9e5048d1e820eaf48
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.9

Release files / module_hygiene-0.3.4-py3-none-any.whl

Download URL module_hygiene-0.3.4-py3-none-any.whl
Size 4.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f655d775ffea74a06c00205c07bdff02dae903081e8192bf5c11288ae5ac7dc7
BLAKE2b-256 checksum
How to use checksums
270bba7b3491998a5a10068a8078e0703b1ff7f65b5465a0d8a3cf63343e23fc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.9

Security for module-hygiene 0.3.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page