Skip to main content

Get Python docstrings from files

Project description

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"'

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

Built Distributions

File details

Details for the file canonicalwebteam.docstring-extractor-1.1.1.tar.gz.

File metadata

File hashes

Hashes for canonicalwebteam.docstring-extractor-1.1.1.tar.gz
Algorithm Hash digest
SHA256 bcfe0ed306faeeec006e93a407530384eb4471bbf6f3c9e5a391e8707531a3a9
MD5 3f451e3bcb5a8673d49f4ad090b4b8b7
BLAKE2b-256 f96999dc2125bf841b8b6c074cb54c97741eac5cb75835e9e06f8683ad35c1e1

See more details on using hashes here.

File details

Details for the file canonicalwebteam_docstring_extractor-1.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for canonicalwebteam_docstring_extractor-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7d07ad2edb02383b4ed21c09ef75e4970c869714b35ccf5dbc269e5fcce132d2
MD5 bfa94466c69f7096e08026c8b7e934d7
BLAKE2b-256 3663b3d225f11099f07d95c21e54aab4ac8ec9599edeb7f61d29bc26023d8746

See more details on using hashes here.

File details

Details for the file canonicalwebteam.docstring_extractor-1.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for canonicalwebteam.docstring_extractor-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d3db1c19f0529ec241eb613da1f82283771515eec8fe22ed562d1796b341f4a6
MD5 6fd5b0e7c8b52b7a8dd85853e3a03066
BLAKE2b-256 d28adb1f454b2d4e488d820074b652b2095e3c21bd69c6b15c459e8db07a274b

See more details on using hashes here.

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