Skip to main content

Yet another docstring parser for python

Project description

pardoc

pypi tag travis codacy quality codacy quality pyver

Yet another docstring parser for python, using lark-parser

Installation

pip install pardoc

A quick look

>>> from pardoc import google_parser, pretty

>>> docstring = """This is an example of a module level function.

Function parameters should be documented in the ``Args`` section. The name
of each parameter is required. The type and description of each parameter
is optional, but should be included if not obvious.

If \*args or \*\*kwargs are accepted,
they should be listed as ``*args`` and ``**kwargs``.

The format for a parameter is::

    name (type): description
        The description may span multiple lines. Following
        lines should be indented. The "(type)" is optional.

        Multiple paragraphs are supported in parameter
        descriptions.

Args:
    param1 (int): The first parameter.
    param2 (:obj:`str`, optional): The second parameter. Defaults to None.
        Second line of description should be indented.
    *args: Variable length argument list.
    **kwargs: Arbitrary keyword arguments.

Returns:
    bool: True if successful, False otherwise.

    The return type is optional and may be specified at the beginning of
    the ``Returns`` section followed by a colon.

    The ``Returns`` section may span multiple lines and paragraphs.
    Following lines should be indented to match the first line.

    The ``Returns`` section supports any reStructuredText formatting,
    including literal blocks::

        {
            'param1': param1,
            'param2': param2
        }

Raises:
    AttributeError: The ``Raises`` section is a list of all exceptions
        that are relevant to the interface.
    ValueError: If `param2` is equal to `param1`.

"""

>>> parsed = google_parser.parse(docstring)
>>> pretty(parsed, print_=True)

ParsedSection(title=SUMMARY)
   ParsedPara(lines=1)
      This is an example of a module level function.
   ParsedPara(lines=3)
      Function parameters should be documented in the ``Args`` section. The name
      of each parameter is required. The type and description of each parameter
      is optional, but should be included if not obvious.
   ParsedPara(lines=2)
      If \*args or \*\*kwargs are accepted,
      they should be listed as ``*args`` and ``**kwargs``.
   ParsedPara(lines=1)
      The format for a parameter is::
   ParsedPara(lines=2)
      ParsedPara(lines=1)
         name (type): description
      ParsedPara(lines=2)
         ParsedPara(lines=2)
            The description may span multiple lines. Following
            lines should be indented. The "(type)" is optional.
         ParsedPara(lines=2)
            Multiple paragraphs are supported in parameter
            descriptions.

ParsedSection(title=Args)
   ParsedItem(name=param1, type=int, desc=The first parameter.)
   ParsedItem(name=param2, type=:obj:`str`, optional, desc=The second parameter. Defaults to None.)
      ParsedPara(lines=1)
         Second line of description should be indented.
   ParsedItem(name=*args, type=None, desc=Variable length argument list.)
   ParsedItem(name=**kwargs, type=None, desc=Arbitrary keyword arguments.)

ParsedSection(title=Returns)
   ParsedItem(name=bool, type=None, desc=True if successful, False otherwise.)
   ParsedPara(lines=2)
      The return type is optional and may be specified at the beginning of
      the ``Returns`` section followed by a colon.
   ParsedPara(lines=2)
      The ``Returns`` section may span multiple lines and paragraphs.
      Following lines should be indented to match the first line.
   ParsedPara(lines=2)
      The ``Returns`` section supports any reStructuredText formatting,
      including literal blocks::
   ParsedPara(lines=2)
      ParsedPara(lines=1)
         {
      ParsedPara(lines=1)
         ParsedPara(lines=2)
            'param1': param1,
            'param2': param2
   ParsedPara(lines=1)
      ParsedPara(lines=1)
         }

ParsedSection(title=Raises)
   ParsedItem(name=AttributeError, type=None, desc=The ``Raises`` section is a list of all exceptions)
      ParsedPara(lines=1)
         that are relevant to the interface.
   ParsedItem(name=ValueError, type=None, desc=If `param2` is equal to `param1`.)

Usage

Parsing a known style docstring

from pardoc import google_parser, numpy_parser
parsed = google_parser(docstring)
# or
parsed = numpy_parser(docstring)

Parsing an unknown style docstring

from pardoc import auto_parser

parser = auto_parser(docstring)
# parsing results from auto_parser is cached and reused.
parsed = parser.parse(docstring)

Parsed object

There are 6 types of parsed objects, include the final Parsed object that attaches all sections

The first 5 are all namedtuples:

ParsedItem = namedtuple('ParsedItem',
                        ['name', 'type', 'desc', 'more'])
ParsedTodo = namedtuple('ParsedTodo', ['todo', 'more'])
ParsedSection = namedtuple('ParsedSection', ['title', 'section'])
ParsedPara = namedtuple('ParsedPara', ['lines'])
ParsedCode = namedtuple('ParsedCode', ['lang', 'codes'])

The Parsed is an ordered dictionary (OrderedDiot) from diot, which allows dot access to keys:

from diot import OrderedDiot

class Parsed(OrderedDiot):
    """Parsed object"""

Formatting a parsed object to the original style

>>> from pardoc import google_parser
>>> docstring = """Example function with types documented in the docstring.


Args:


    param0: No type

    param1 (int): The first parameter.
    param2 (str): The second parameter.

Returns:


    bool: The return value. True for success, False otherwise.

"""
>>> # note the arbitrary empty lines
>>> reformatted = google_parser.format(docstring)
>>> # or
>>> reformatted = google_parser.format(google_parser.parse(docstring))
>>> print(reformatted)
Example function with types documented in the docstring.

Args:
    param0: No type
    param1 (int): The first parameter.
    param2 (str): The second parameter.

Returns:
    bool: The return value. True for success, False otherwise.

Pretty printing the parsed objects

See A quick look

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

pardoc-0.0.2.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

pardoc-0.0.2-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file pardoc-0.0.2.tar.gz.

File metadata

  • Download URL: pardoc-0.0.2.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.9.0 Linux/5.4.0-1031-azure

File hashes

Hashes for pardoc-0.0.2.tar.gz
Algorithm Hash digest
SHA256 2ec000d39c0c9d1340efb6b787eda717d87e9a0d29364050a5374d969af7cbfd
MD5 5520df98d8c595eba1bc4bc9ce743cac
BLAKE2b-256 caa341a62777236713638a2b80fdbffa1c23ef30a98fcd42ceefbe1d782d7d6d

See more details on using hashes here.

File details

Details for the file pardoc-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: pardoc-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 13.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.9.0 Linux/5.4.0-1031-azure

File hashes

Hashes for pardoc-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 11b816efe45a7e6b1c0cc11dd720474b1ada8bdebf742c2b1659ab781fd6d109
MD5 eeb4d489dc409ac4a7f613f3628b5f9a
BLAKE2b-256 b35174931333777cad516268cd4f1b69410391b051ec9e40bed2e32017f0ea16

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