Skip to main content

Collection of utilities to aid working with python modules.

Project description

Repo Docs PyPI license PyPI version Conda (channel only) Code style: ruff uv

module-utilities

A Python package for creating python modules.

Overview

I was using the same code snippets over and over, so decided to put them in one place.

Features

  • cached: A module to cache class attributes and methods. Right now, this uses a standard python dictionary for storage. Future versions will hopefully integrate with something like cachetools.

  • docfiller: A module to share documentation. This is adapted from the pandas doc decorator. There are some convenience functions and classes for sharing documentation.

  • docinhert: An interface to docstring-inheritance module. This can be combined with docfiller to make creating related function/class documentation easy.

Status

This package is actively used by the author. Please feel free to create a pull request for wanted features and suggestions!

Example usage

Simple example of using cached module.

>>> from module_utilities import cached
>>>
>>> class Example:
...     @cached.prop
...     def aprop(self):
...         print("setting prop")
...         return ["aprop"]
...     @cached.meth
...     def ameth(self, x=1):
...         print("setting ameth")
...         return [x]
...     @cached.clear
...     def method_that_clears(self):
...         pass
...
>>> x = Example()
>>> x.aprop
setting prop
['aprop']
>>> x.aprop
['aprop']

>>> x.ameth(1)
setting ameth
[1]
>>> x.ameth(x=1)
[1]

>>> x.method_that_clears()

>>> x.aprop
setting prop
['aprop']
>>> x.ameth(1)
setting ameth
[1]

Simple example of using DocFiller.

>>> from module_utilities.docfiller import DocFiller, indent_docstring
>>> d = DocFiller.from_docstring(
...     """
...     Parameters
...     ----------
...     x : int
...         x param
...     y : int
...         y param
...     z0 | z : int
...         z int param
...     z1 | z : float
...         z float param
...
...     Returns
...     -------
...     output0 | output : int
...         Integer output.
...     output1 | output : float
...         Float output
...     """,
...     combine_keys="parameters",
... )
>>> @d.decorate
... def func(x, y, z):
...     """
...     Parameters
...     ----------
...     {x}
...     {y}
...     {z0}
...
...     Returns
...     --------
...     {returns.output0}
...     """
...     return x + y + z
...
>>> print(indent_docstring(func))
+  Parameters
+  ----------
+  x : int
+      x param
+  y : int
+      y param
+  z : int
+      z int param
<BLANKLINE>
+  Returns
+  --------
+  output : int
+      Integer output.

# Note that for python version <= 3.8 that method chaining
# in decorators doesn't work, so have to do the following.
# For newer python, you can inline this.
>>> dd = d.assign_keys(z="z0", out="returns.output0")
>>> @dd.decorate
... def func1(x, y, z):
...     """
...     Parameters
...     ----------
...     {x}
...     {y}
...     {z}
...     Returns
...     -------
...     {out}
...     """
...     pass
...
>>> print(indent_docstring(func1))
+  Parameters
+  ----------
+  x : int
+      x param
+  y : int
+      y param
+  z : int
+      z int param
+  Returns
+  -------
+  output : int
+      Integer output.

>>> dd = d.assign_keys(z="z1", out="returns.output1")
>>> @dd(func1)
... def func2(x, y, z):
...     pass
...

>>> print(indent_docstring(func2))
+  Parameters
+  ----------
+  x : int
+      x param
+  y : int
+      y param
+  z : float
+      z float param
+  Returns
+  -------
+  output : float
+      Float output

Installation

Use one of the following

pip install module-utilities

or

conda install -c conda-forge module-utilities

Optionally, you can install docstring-inheritance to use the docinherit module:

pip install docstring-inheritance
# or
conda install -c conda-forge docstring-inheritance

Documentation

See the documentation for a look at module-utilities in action.

What's new?

See changelog.

License

This is free software. See LICENSE.

Related work

module-utilities is used in the following packages

Contact

The author can be reached at wpk@nist.gov.

Credits

This package was created using Cookiecutter with the usnistgov/cookiecutter-nist-python template.

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

module_utilities-0.11.1.dev0.tar.gz (37.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

module_utilities-0.11.1.dev0-py3-none-any.whl (30.9 kB view details)

Uploaded Python 3

File details

Details for the file module_utilities-0.11.1.dev0.tar.gz.

File metadata

  • Download URL: module_utilities-0.11.1.dev0.tar.gz
  • Upload date:
  • Size: 37.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for module_utilities-0.11.1.dev0.tar.gz
Algorithm Hash digest
SHA256 10dcd924e0a77049faa8a7be8aaf7d60b552fde24124057b2449006b8dd69a31
MD5 4a320e0d0301f9aa90cb8a5af3dbe91f
BLAKE2b-256 1758aef8bb8fce258278da0d1572cb5ff1ca16b748c69808aaa074a701975f49

See more details on using hashes here.

Provenance

The following attestation bundles were made for module_utilities-0.11.1.dev0.tar.gz:

Publisher: cd.yml on usnistgov/module-utilities

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file module_utilities-0.11.1.dev0-py3-none-any.whl.

File metadata

File hashes

Hashes for module_utilities-0.11.1.dev0-py3-none-any.whl
Algorithm Hash digest
SHA256 b800a2882d57c69a9de85defa8c4b3a0ff3f9b3b92de3b7ed56ad016ce4ac3e1
MD5 48e1563c041a9775a9e85d3a47472619
BLAKE2b-256 fd0367f2804029e0b60a73adb0f57c39cb1e931b73193368e784cc2d83aee1f3

See more details on using hashes here.

Provenance

The following attestation bundles were made for module_utilities-0.11.1.dev0-py3-none-any.whl:

Publisher: cd.yml on usnistgov/module-utilities

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page