robertwb commented on a change in pull request #12819:
URL: https://github.com/apache/beam/pull/12819#discussion_r497179044
##########
File path: sdks/python/apache_beam/dataframe/doctests.py
##########
@@ -428,7 +482,106 @@ def print_partition(indent, desc, n, total):
print()
-def teststring(text, report=True, **runner_kwargs):
+def parse_rst_ipython_tests(rst, name, extraglobs=None, optionflags=None):
+ """Extracts examples from an rst file and produce a test suite by running
+ them through pandas to get the expected outputs.
+ """
+
+ # Optional dependency.
+ import IPython
+ from traitlets.config import Config
+
+ def get_indent(line):
+ return len(line) - len(line.lstrip())
+
+ def is_example_line(line):
+ line = line.strip()
+ return line and not line.startswith('#') and not line[0] == line[-1] == ':'
+
+ IMPORT_PANDAS = 'import pandas as pd'
+
+ example_srcs = []
+ lines = iter(
+ [line.rstrip()
+ for line in rst.split('\n') if is_example_line(line)] + ['END'])
+
+ # https://ipython.readthedocs.io/en/stable/sphinxext.html
+ for line in lines:
+ if line.startswith('.. ipython::'):
+ line = next(lines)
+ indent = get_indent(line)
+ example = []
+ example_srcs.append(example)
+ while get_indent(line) >= indent:
+ if '@verbatim' in line or '@savefig' in line:
+ example_srcs.pop()
+ break
+ example.append(line[indent:])
+ line = next(lines)
+ if get_indent(line) == indent:
+ example = []
+ example_srcs.append(example)
Review comment:
Actually, this may happen because I'm eliding empty lines. Fixed.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]