latexgit.formatters package¶
Code formatters. A formatter for python code. Format a python code fragment. 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. the formatted code string In this file, we put some shared tools for rendering source codes. Obtain a generator that strips any consecutive empty lines. empty_before ( no_empty_after ( force_no_empty_after ( max_consecutive_empty_lines ( the generation 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. lines ( labels ( line_comment_start ( max_consecutive_empty_lines ( the list of selected lines Get a sequence of labels from a string. the labels set Split line choices to iterables of int or None. This function converts 1-based line selections into 0-based ones. the split line choices. Strip a common whitespace prefix from a list of strings and merge them. the code with the white space prefix strippedSubmodules¶
latexgit.formatters.python module¶
latexgit.formatters.source_tools module¶
Callable
, default: <function <lambda> at 0x7f58b06837e0>
) – a function checking whether an empty line is required before a certain stringCallable
, default: <function <lambda> at 0x7f58b0683880>
) – a function checking whether an empty line is prohibited after a stringCallable
, default: <function <lambda> at 0x7f58b0683920>
) – a function checking whether an empty line is prohibited after a stringint
, default: 2
) – the maximum number of permitted consecutive empty lines>>> 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']
Optional
[Iterable
[int
]], default: None
) – the lines to keep, or None if we keep allOptional
[Iterable
[str
]], default: None
) – a list of labels marking start and end of code snippets to includestr
, default: '#'
) – the string marking the line comment startint
, default: 2
) – the maximum number of permitted consecutive empty 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']
>>> 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']
>>> 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]
>>> 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']