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.

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.

Changelog

Changelog for module-utilities

Unreleased

See the fragment files in changelog.d

v0.11.0 — 2025-07-21

Changed

  • Changes HasCache protocol to support frozen attrs and dataclass classes.

Fixed

  • Errors when applying property to inherit decorator fixed.

v0.9.0 — 2023-08-22

Changed

  • Revert to TypeVar S being invariant. This leads to some issues with cached.prop decorator. However, the use of covariant TypeVar was a hack. Instead, it is better in these cases to decorate with @property on top of @cached.meth. mypy/pyright deal with property in a special way.
  • To better work with the above, single parameter methods are caached using only the method name, and no parameters.

v0.8.0 — 2023-08-21

Changed

  • Moved submodule _typing to typing (i.e., publicly accessible).
  • Made TypeVar S covariant. This fixes issues with subclassing overrides.

v0.7.0 — 2023-08-15

Changed

  • Simplified cached.prop by using (new) CachedProperty class.

v0.6.0 — 2023-08-01

Added

  • Now include module docinhert to interface with docstring-inheritance
  • Fully support mypy and pyright type checking.

v0.5.0 — 2023-07-10

Added

See the fragment files in changelog.d

  • Add _prepend option to docfiller. Default behavior is now to append current docstring to templates.

v0.4.0 — 2023-06-14

Added

  • Package now available on conda-forge

Changed

  • Properly vendor numpydocs and include pointer to license

v0.3.0 — 2023-05-03

Added

  • Added DocFiller.assign_param to more easily add a new parameter.

v0.2.0 — 2023-05-02

Added

  • Added method assign_keys to DocFiller.

v0.1.0 — 2023-05-01

Added

  • Add typing support. Passing mypy, pyright, pytype. This software was developed by employees of the National Institute of Standards and Technology (NIST), an agency of the Federal Government. Pursuant to title 17 United States Code Section 105, works of NIST employees are not subject to copyright protection in the United States and are considered to be in the public domain. Permission to freely use, copy, modify, and distribute this software and its documentation without fee is hereby granted, provided that this notice and disclaimer of warranty appears in all copies.

THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.

Distributions of NIST software should also include copyright and licensing statements of any third-party software that are legally bundled with the code in compliance with the conditions of those licenses.


module-utilities vendors a copy of docscrape.py from numpydoc. The license is BSD and include at "module_utilities/vendored/LICENSE.txt".

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.0.tar.gz (220.4 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.0-py3-none-any.whl (31.9 kB view details)

Uploaded Python 3

File details

Details for the file module_utilities-0.11.0.tar.gz.

File metadata

  • Download URL: module_utilities-0.11.0.tar.gz
  • Upload date:
  • Size: 220.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.0

File hashes

Hashes for module_utilities-0.11.0.tar.gz
Algorithm Hash digest
SHA256 72ad7e0802fadcf8cce1affd431ff749046c2ea4f14a3eaeab2b1173c4e4ee78
MD5 87dc12f77cbe15ca40671ebf69164b47
BLAKE2b-256 a4d0d05603f10b18fcd9a734676b98352f669055994d660baa63b2e84807c654

See more details on using hashes here.

File details

Details for the file module_utilities-0.11.0-py3-none-any.whl.

File metadata

File hashes

Hashes for module_utilities-0.11.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4b6c999bddcdaf03229af8ee35fa0650b6b449c6f2c46a15bd4d0288dbfd8d90
MD5 8709e7d7f2414dcdb56c08c232367ab0
BLAKE2b-256 0beb7ad99515e443423225d65f0f42ece8db78161afa68f7888c5fd72ff30047

See more details on using hashes here.

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