Helpers

Helpers functions that implement boiler plate code used together with Asserter. It provides support for easier Input/Output tests.

class capture_output

This context manager allows you to capture the output printed to stdout in a io.StringIO buffer.

>>> from cg_feedback_helpers import helpers
>>> with helpers.capture_output() as buffer:
...     print("Hi")
>>> print(f"Output was: {buffer.getvalue()}")
Output was: Hi
class as_stdin

This context manager allows you to provide input to a program by passing in the input lines. It temporarily replaces the standard stdin buffer with one containing the specified input lines.

>>> from cg_feedback_helpers import helpers
>>> with helpers.as_stdin("Hi"):
...     print(input())
Hi
get_lines_from_buffer(buffer, *, extra_splits=None)

Boiler plate to make the use of capture_output easier. By providing the buffer from capture_output you will get the output lines, split on the new line, and all lines with just white spaces filtered out.

Parameters:
  • buffer (StringIO) – A io.StringIO buffer providing the lines to parse and split.

  • extra_splits (Union[List[str], Pattern[str], None]) – A list of regexes that provide extra split points other than \n. It is your responsibility to guarantee these are correct regexes.

Returns:

A list containing the output lines, with the empty lines filtered out.

Return type:

List[str]

>>> from cg_feedback_helpers import helpers
>>> with helpers.capture_output() as buffer:
...     print("something.goes\nout")
>>> print(helpers.get_lines_from_buffer(buffer, extra_splits=[r"\."]))
['something', 'goes', 'out']