texgit.formatters package¶
Code formatters.
Submodules¶
texgit.formatters.python module¶
A formatter for python code.
- texgit.formatters.python.format_python(code, strip_docstrings=True, strip_comments=True, strip_hints=True)[source]¶
Format a python code fragment.
- texgit.formatters.python.preprocess_python(code, lines=None, labels=None, params=None)[source]¶
Preprocess Python code.
First, we select all lines of the code we want to keep. If labels are defined, then lines can be kept as ranges or as single lines. Otherwise, all lines are selected in this step.
Then, if line numbers are provided, we selected the lines based on the line numbers from the lines we have preserved.
Finally, the Python formatter is applied.
- Parameters:
- Return type:
- Returns:
the formatted code string
texgit.formatters.source_tools module¶
In this file, we put some shared tools for rendering source codes.
- texgit.formatters.source_tools.format_empty_lines(lines, empty_before=<function <lambda>>, no_empty_after=<function <lambda>>, force_no_empty_after=<function <lambda>>, max_consecutive_empty_lines=2)[source]¶
Obtain a generator that strips any consecutive empty lines.
- Parameters:
empty_before (
Callable, default:<function <lambda> at 0x7faf51e21620>) – a function checking whether an empty line is required before a certain stringno_empty_after (
Callable, default:<function <lambda> at 0x7faf51e216c0>) – a function checking whether an empty line is prohibited after a stringforce_no_empty_after (
Callable, default:<function <lambda> at 0x7faf51e21760>) – a function checking whether an empty line is prohibited after a stringmax_consecutive_empty_lines (
int, default:2) – the maximum number of permitted consecutive empty lines
- Return type:
- Returns:
the generation
>>> code = ["", "a", "", "b", "", "", "c", "", "", "", "d", "e", ""] >>> format_empty_lines(code, max_consecutive_empty_lines=3) ['a', '', 'b', '', '', 'c', '', '', '', 'd', 'e'] >>> format_empty_lines(code, max_consecutive_empty_lines=2) ['a', '', 'b', '', '', 'c', '', '', 'd', 'e'] >>> format_empty_lines(code, max_consecutive_empty_lines=1) ['a', '', 'b', '', 'c', '', 'd', 'e'] >>> format_empty_lines(code, max_consecutive_empty_lines=0) ['a', 'b', 'c', 'd', 'e'] >>> format_empty_lines(code, max_consecutive_empty_lines=2, ... no_empty_after=lambda s: s == "b") ['a', '', 'b', 'c', '', '', 'd', 'e'] >>> format_empty_lines(code, max_consecutive_empty_lines=2, ... no_empty_after=lambda s: s == "b", ... empty_before=lambda s: s == "e") ['a', '', 'b', 'c', '', '', 'd', '', 'e'] >>> format_empty_lines(code, max_consecutive_empty_lines=2, ... no_empty_after=lambda s: s == "b", ... empty_before=lambda s: s == "e", ... force_no_empty_after=lambda s: s == "d") ['a', '', 'b', 'c', '', '', 'd', 'e']
- texgit.formatters.source_tools.select_lines(code, lines=None, labels=None, line_comment_start='#', max_consecutive_empty_lines=2)[source]¶
Select lines of source code based on labels and line indices.
First, we select all lines of the code we want to keep. If labels are defined, then lines are kept as ranges or as single lines for all pre-defined labels. Ranges may overlap and/or intersect. Otherwise, all lines are selected in this step.
Then, if line numbers are provided, we selected the lines based on the line numbers from the lines we have preserved.
Finally, leading and trailing empty lines as well as superfluous empty lines are removed.
- Parameters:
lines (
Optional[Iterable[int]], default:None) – the lines to keep, or None if we keep alllabels (
Optional[Iterable[str]], default:None) – a list of labels marking start and end of code snippets to includeline_comment_start (
str, default:'#') – the string marking the line comment startmax_consecutive_empty_lines (
int, default:2) – the maximum number of permitted consecutive empty lines
- Return type:
- Returns:
the list of selected lines
>>> select_lines(["def a():", " b=c", " return x"]) ['def a():', ' b=c', ' return x'] >>> pc = ["# start x", "def a():", " b=c # -x", " return x", "# end x"] >>> select_lines(pc, labels={"x"}) ['def a():', ' return x'] >>> select_lines(["def a():", " b=c", " return x"], lines=[0, 2]) ['def a():', ' return x']
- texgit.formatters.source_tools.split_labels(labels)[source]¶
Get a sequence of labels from a string.
- Parameters:
- Return type:
- Returns:
the labels set
>>> print(split_labels(None)) set() >>> print(split_labels("")) set() >>> print(split_labels(",")) set() >>> sorted(split_labels("a;b;d;c")) ['a', 'b', 'c', 'd'] >>> sorted(split_labels("a,b c+a;a\td;c")) ['a', 'b', 'c', 'd']
- texgit.formatters.source_tools.split_line_choices(lines)[source]¶
Split line choices to iterables of int or None.
This function converts 1-based line selections into 0-based ones.
- Parameters:
- Return type:
- Returns:
the split line choices.
>>> print(split_line_choices(None)) None >>> print(split_line_choices("")) None >>> print(split_line_choices(",")) None >>> split_line_choices("1") [0] >>> split_line_choices("1,2") [0, 1] >>> split_line_choices("1-5") [0, 1, 2, 3, 4] >>> split_line_choices("3;4-5;7-7;22+12-15;") [2, 3, 4, 6, 21, 11, 12, 13, 14]
- texgit.formatters.source_tools.strip_common_whitespace_prefix(lines)[source]¶
Strip a common whitespace prefix from a list of strings and merge them.
- Parameters:
- Return type:
- Returns:
the code with the white space prefix stripped
>>> strip_common_whitespace_prefix([" a", " b"]) ['a', ' b'] >>> strip_common_whitespace_prefix([" a", " b"]) ['a', 'b'] >>> strip_common_whitespace_prefix([" a", " b"]) ['a', 'b'] >>> strip_common_whitespace_prefix([" a", " b", "c"]) [' a', ' b', 'c'] >>> strip_common_whitespace_prefix([" a", " b", "c"]) [' a', ' b', 'c'] >>> strip_common_whitespace_prefix([" a", " b", " c"]) ['a', 'b', ' c']