Skip to main content

No project description provided

Project description

numpydoc_decorator

This package allows you to build numpy-style docstrings programmatically and apply them using a decorator. This can be useful because:

  • Parts of your documentation, such as parameter descriptions, can be shared between functions, avoiding the need to repeat yourself.

  • Type information for parameters and return values is automatically picked up from type annotations and added to the docstring, avoiding the need to maintain type information in two places.

Installation

pip install numpydoc_decorator

Usage

Here is an example of documenting a function:

from numpydoc_decorator import doc


@doc(
    summary="This function will say hello.",
    extended_summary="""
        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
        eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
        minim veniam, quis nostrud exercitation ullamco laboris nisi ut
        aliquip ex ea commodo consequat. Duis aute irure dolor in
        reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
        pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
        culpa qui officia deserunt mollit anim id est laborum.
    """,
    parameters=dict(
        name="The name of the person to greet.",
        language="The language in which to greet as an ISO 639-1 code."
    ),
    returns=dict(
        greeting="A pleasant greeting.",
    ),
    raises=dict(
        NotImplementedError="If the requested language has not been implemented yet.",
    ),
    see_also=dict(
        print="You could use this function to print your greeting.",
    ),
    notes="""
        This function is useful when greeting someone else. If you would
        like something to talk about next, you could try [1]_.
    """,
    references={
        "1": """
            O. McNoleg, "The integration of GIS, remote sensing, expert systems
            and adaptive co-kriging for environmental habitat modelling of the
            Highland Haggis using object-oriented, fuzzy-logic and neural-
            network techniques," Computers & Geosciences, vol. 22, pp. 585-588,
            1996.'
        """,
    },
    examples="""
        Here is how to greet a friend in English:

        >>> print(greet("Ford Prefect"))
        Hello Ford Prefect!

        Here is how to greet someone in another language:

        >>> print(greet("Tricia MacMillan", language="fr"))
        Salut Tricia MacMillan!

    """,
)
def greet(
    name: str,
    language: str = "en",
) -> str:
    if language == "en":
        return f"Hello {name}!"
    elif language == "fr":
        return f"Salut {name}!"
    else:
        raise NotImplementedError(f"language {language} not implemented")

Here is the docstring that will be created and attached to the decorated function:

>>> print(greet.__doc__)
This function will say hello.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.

Parameters
----------
name : str
    The name of the person to greet.
language : str, optional, default='en'
    The language in which to greet as an ISO 639-1 code.

Returns
-------
greeting : str
    A pleasant greeting.

Raises
------
NotImplementedError
    If the requested language has not been implemented yet.

See Also
--------
print : You could use this function to print your greeting.

Notes
-----
This function is useful when greeting someone else. If you would like
something to talk about next, you could try [1]_.

References
----------
.. [1] O. McNoleg, "The integration of GIS, remote sensing, expert systems
    and adaptive co-kriging for environmental habitat modelling of the
    Highland Haggis using object-oriented, fuzzy-logic and neural- network
    techniques," Computers & Geosciences, vol. 22, pp. 585-588, 1996.'

Examples
--------
Here is how to greet a friend in English:

>>> print(greet("Ford Prefect"))
Hello Ford Prefect!

Here is how to greet someone in another language:

>>> print(greet("Tricia MacMillan", language="fr"))
Salut Tricia MacMillan!

Caveats

There are probably lots of edge cases that this package has not covered yet. If you find something doesn't work as expected, or deviates from the numpydoc style guide in an unreasonable way, please feel free to submit a pull request.

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

numpydoc_decorator-1.0.0.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

numpydoc_decorator-1.0.0-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file numpydoc_decorator-1.0.0.tar.gz.

File metadata

  • Download URL: numpydoc_decorator-1.0.0.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.10.8 Linux/5.15.0-1033-azure

File hashes

Hashes for numpydoc_decorator-1.0.0.tar.gz
Algorithm Hash digest
SHA256 61590a49a56b6ff7f346f2a0140762f53f5b9b3ef4cec0eefeb571b3d321ea31
MD5 e2613525c38cbf5cad26e675e14a78fe
BLAKE2b-256 da6ec0bcf93943cc2afd4fd22ac465a35deb09670cc5b9c9a4f621ab325c1845

See more details on using hashes here.

Provenance

File details

Details for the file numpydoc_decorator-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for numpydoc_decorator-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7ff94b7cf956b901bdb79a33d199b06422d3bf652b0671b6a5c7393eb622f20d
MD5 14a5f29c96cdad4b87340dc40c55b05b
BLAKE2b-256 0c28e279bbb07419c0ff3f61f327f8540887f2e561d1d58aa309410523c330c0

See more details on using hashes here.

Provenance

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