Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docstring_parser/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class DocstringMeta:
"""

def __init__(
self, args: T.List[str], description: T.Optional[str]
self, args: T.List[str], description: T.Optional[str],
) -> None:
"""Initialize self.

Expand Down Expand Up @@ -140,11 +140,13 @@ def __init__(
args: T.List[str],
snippet: T.Optional[str],
description: T.Optional[str],
post_snippet: T.Optional[str] = None,
) -> None:
"""Initialize self."""
super().__init__(args, description)
self.snippet = snippet
self.description = description
self.post_snippet = post_snippet


class Docstring:
Expand Down
38 changes: 31 additions & 7 deletions docstring_parser/numpydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,28 +238,52 @@ class ExamplesSection(Section):
[ 6586976, 22740995]])
"""

def parse(self, text: str) -> T.Iterable[DocstringMeta]:
def parse(self, text: str) -> T.Iterable[DocstringExample]:
"""Parse ``DocstringExample`` objects from the body of this section.

:param text: section body text. Should be cleaned with
``inspect.cleandoc`` before parsing.
"""
lines = dedent(text).strip().splitlines()
lines = [x.rstrip() for x in dedent(text).strip().splitlines()]
while lines:
snippet_lines = []
description_lines = []
post_snippet_lines = []

# Parse description of snippet
while lines:
if lines[0].startswith(">>>"):
break
description_lines.append(lines.pop(0))

# Parse code of snippet
while lines:
if not lines[0].startswith(">>>"):
if not lines[0].startswith(">>>") and not lines[0].startswith('...'):
break
snippet_lines.append(lines.pop(0))

# Parse output of snippet
while lines:
if lines[0].startswith(">>>"):
# Bail out at blank lines
if not lines[0]:
lines.pop(0)
break
description_lines.append(lines.pop(0))
# Bail out if a new snippet is started
elif lines[0].startswith(">>>"):
break
else:
snippet_lines.append(lines.pop(0))

# if there is following text, but no more snippets, make this a post description.
if not [x for x in lines if '>>>' in x]:
post_snippet_lines.extend(lines)
lines = []

yield DocstringExample(
[self.key],
snippet="\n".join(snippet_lines) if snippet_lines else None,
description="\n".join(description_lines),
snippet="\n".join(snippet_lines).strip() if snippet_lines else None,
description="\n".join(description_lines).strip(),
post_snippet="\n".join(post_snippet_lines).strip(),
)


Expand Down
Loading