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 Distribution

File details

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

File metadata

  • Download URL: canonicalwebteam.docstring-extractor-1.0.0.tar.gz
  • Upload date:
  • Size: 16.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.9.6

File hashes

Hashes for canonicalwebteam.docstring-extractor-1.0.0.tar.gz
Algorithm Hash digest
SHA256 7956ae55227dad6c94fed504bfa48af6414faa9d3e3d70a3d2bf67296611c041
MD5 b4f48fde24e2d6c25dd5f1b86727d1a4
BLAKE2b-256 737ed23bdb00b1694be4a45dc1ef7e4ed7734355323a1a612bd3a63cfc59ead4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for canonicalwebteam.docstring_extractor-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 84583403e0801d9145fd0cb26584fa86ec67836cb89a278d13698a8d9ebcc516
MD5 3a3ae6f89d9f1ae68836fbd5c9d88cc2
BLAKE2b-256 1f73f124ecc9624fc9396578d7e197a11cfae77d0d008bce0f71d84e66e03817

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