Skip to main content

docstring-extractor

This library allows parsing docstrings of an entire Python file. It uses ast and docstring-parser.

The main difference between this library and docstring-parser is that this one is able to parse entire Python files.

Installation:

pip install docstring-extractor

Usage

>>> from canonicalwebteam.docstring_extractor import get_docstrings
>>>
>>> with open("example.py") as file:
...     docstrings = get_docstrings(file)

Imaging you have the following Python code:

"""
Example module.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua.
"""


def test_function():
    """
    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.

    Parameters:
        a (int): A decimal integer
        b (int): Another decimal integer

    Returns:
        str: A string containing "foo"
    """
    return "foo"

The output of the get_docstrings function will be the following dict object:

{'type': 'Module',
 'name': 'example',
 'line': 0,
 'docstring': <docstring_parser.common.Docstring at 0x7f06adee7a00>,
 'docstring_text': 'Example module.\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod\ntempor incididunt ut labore et dolore magna aliqua.',
 'content': [
    {'type': 'Function',
       'name': 'test_function',
       'line': 9,
       'docstring': <docstring_parser.common.Docstring at 0x7f06adef7490>,
       'docstring_text': 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod\ntempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim\nveniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea\ncommodo consequat. Duis aute irure dolor in reprehenderit in voluptate\nvelit esse cillum dolore eu fugiat nulla pariatur.\n\nParameters:\n    a (int): A decimal integer\n    b (int): Another decimal integer\n\nReturns:\n    str: A string containing "foo"',
    'content': []
    }
 ]
}

You can see the different properties of the Docstring object here, as an example if you are interested in obtaining the return type and return description of the first function:

>>> docstrings["content"][0]["docstring"].returns.type_name
'str'
>>> docstrings["content"][0]["docstring"].returns.description
'A string containing "foo"'

Metadata

Release files for canonicalwebteam-docstring-extractor 1.2.0

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

Source distribution (sdist)

Source distribution for canonicalwebteam-docstring-extractor 1.2.0
File Size Uploaded
canonicalwebteam_docstring_extractor-1.2.0.tar.gz 16.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for canonicalwebteam-docstring-extractor 1.2.0
File Interpreter ABI Platform
canonicalwebteam_docstring_extractor-1.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 32.7 kB

Release files / canonicalwebteam_docstring_extractor-1.2.0.tar.gz

Download URL canonicalwebteam_docstring_extractor-1.2.0.tar.gz
Size 16.2 kB
Tags Source
SHA-256 checksum
How to use checksums
8128e8750fbda3f81a8487b49a7ffdef6cecfdd857772bdd45a2b1f381766b04
BLAKE2b-256 checksum
How to use checksums
c44f803975a0a24cf8d29daf9651de78630e4e61df6b425122e103e7e7284e74
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.2

Release files / canonicalwebteam_docstring_extractor-1.2.0-py3-none-any.whl

Download URL canonicalwebteam_docstring_extractor-1.2.0-py3-none-any.whl
Size 16.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ecdef2f1a1bfb8e70187839ddecfa3bf08c1e361d3901a94d327a418721c6c6f
BLAKE2b-256 checksum
How to use checksums
31a9b3488a8979f439cbdc2b5453693d31dc9e9e11875233742fd843ac3a4f7e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.2

Release history Release notifications | RSS feed

This release

1.2.0 This release

2 release files

1.1.2

2 release files

1.1.1

3 release files

1.1.0

2 release files

1.0.0

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

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