Docstring Expander
This project addresses a common problem with the development of python packages in which there several higher level functions that have are many keyword options that are processed by low level functions. Typically, the higher level functions use **kwargs to pass keywords transparently to the lower level functions. This is an excellent use of python's capabilities.
But it creates a problem when used with intellisense in that the keyword options are only exposed at the lower level.
To illustrate, consider the following low level plotting function that is called by two plotting functions.
def lowlevelPlot(data, num_col:int=2, num_row:int=3, bins=100):
"""
General plot function.
Parameters
----------
num_col: Number of columns of plots
num_row: Number of rows of plots
bins: Number of bins
"""
...
def plotHist(data:np.ndarray, num_col:int=2, num_row:int=3, bins=100):
"""
Plot a histogram.
Parameters
----------
num_col: Number of columns of plots
default: 2
num_row: Number of rows of plots
default: 3
bins: Number of bins
default: 100
"""
...
lowlevelPlot(num_col=num_col, num_row=num_row, bins=bins)
def plotTimeseries(data:np.ndarray, num_col:int=2, num_row:int=3):
"""
Plot a histogram.
Parameters
----------
num_col: Number of columns of plots
default: 2
num_row: Number of rows of plots
"""
...
lowlevelPlot(num_col=num_col, num_row=num_row)
`
Since there is an almost unlimited number of options for plotting, we expect that the keyword options for genPlot will grow over time. For example, we may want to add options for a title, the position of the title, and its font. These should be transparently available to hist and timeseries, without changing their signatures or docstrings. So, a more maintainable version of these functions is:
def plotHist(data, **kwargs):
"""
Plot a histogram.
Parameters
----------
See lowlevelPlot.
"""
...
lowlevelPlot(**kwargs)
def plotTimeseries(data:np.ndarray, **kwargs):
"""
Plot a histogram.
Parameters
----------
See lowlevelPlot.
"""
...
lowlevelPlot(**kwargs)
But now intellisense doesn't work since the options exposed for a function by intellisese are what's in the docstring of function.
docstring_expander provides another solution. Suppose we have the following dictionary that describes all keyword arguments for genPlot:
import docstring_expander as de
kwargs = [
de.Kwarg('num_col', dtype=int, default=2, doc='Number of columns of plots'),
de.Kwarg('num_row', dtype=int, default=3, doc='Number of rows of plots'),
de.Kwarg(name='bins' default=100, doc='Number of bins'),
]
We define a few keyword arguments as common to most plotting functions.
base = ['num_col', 'num_row']
Then we can write:
@de.Expander(kwargs, base, includes=['bins'])
def plotHist(data:np.ndarray, **kwargs):
"""
Plot a histogram.
Parameters
----------
#@expand
"""
...
lowlevelPlot(**kwargs)
@de.Expander(kwargs, base)
def plotTimeseries(data:np.ndarray, **kwargs):
"""
Plot a histogram.
Parameters
----------
#@exapnd
"""
...
lowlevelPlot(**kwargs)
For plotHist, the decorator replaces #@expand with:
num_col: int
Number of columns of plots
default: 2
num_row: int
Number of rows of plots
default: 3
bins:
Number of bins
default: 100
The keyword arguments in the expansion are those in base plus those in includes minus those in excludes.
Metadata
Release files for docstring-expander 0.23
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| docstring_expander-0.23.tar.gz | 4.0 kB | Without provenance | Details for docstring_expander-0.23.tar.gz |
Release files / docstring_expander-0.23.tar.gz
| Download URL | docstring_expander-0.23.tar.gz |
|---|---|
| Size | 4.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
01d881bd22c8f4ebbf62db846e3c0a80b9dc9888907fdce9a82c52a6495014ef
|
|
BLAKE2b-256 checksum How to use checksums |
a40e18b0b2947c017704cbff62ff7ef24118a6ecfc7913fc194186a72aedff1a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.6.9
|
Security for docstring-expander 0.23
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.