def test_simple(): y = list(sliding_window.window(range(1000, 1005), 1, 2)) assert y == [ # prev this next next2 (None, 1000, 1001, 1002), (1000, 1001, 1002, 1003), (1001, 1002, 1003, 1004), (1002, 1003, 1004, None), (1003, 1004, None, None) ]
def test_is_lazy(): done = False def gen(): nonlocal done done = True yield 42 x = sliding_window.window(gen(), 1, 1) assert not done assert list(x) assert done
def format_xml(tokens: Iterable[Token]) -> str: out = io.StringIO() context = ElementStack() for prev2, prev1, token, next1, next2 in sliding_window.window( tokens, 2, 2): if isinstance(token, Tag): if token.is_opening: out.write(indent_text(token.data, context.indent)) if not is_inline(prev2, prev1, token, next1, next2): out.write("\n") context.push_tag(token.tag) elif token.is_closing: context.pop_tag(token.tag) if is_inline(prev2, prev1, token, next1, next2): out.write(token.data) else: out.write(indent_text(token.data, context.indent)) out.write("\n") else: # self-closing out.write(indent_text(token.data, context.indent)) out.write("\n") elif isinstance(token, Text): if is_inline(prev2, prev1, token, next1, next2): out.write(token.text) else: out.write(indent_text(token.data, context.indent)) out.write("\n") else: # pragma: no cover raise RuntimeError() return out.getvalue()
def format_xml(tokens: Iterable[Token]) -> str: out = io.StringIO() context = ElementStack() for prev2, prev1, token, next1, next2 in sliding_window.window(tokens, 2, 2): if isinstance(token, Tag): if token.is_opening: out.write(indent_text(token.data, context.indent)) if not is_inline(prev2, prev1, token, next1, next2): out.write("\n") context.push_tag(token.tag) elif token.is_closing: context.pop_tag(token.tag) if is_inline(prev2, prev1, token, next1, next2): out.write(token.data) else: out.write(indent_text(token.data, context.indent)) out.write("\n") else: # self-closing out.write(indent_text(token.data, context.indent)) out.write("\n") elif isinstance(token, Text): if is_inline(prev2, prev1, token, next1, next2): out.write(token.text) else: out.write(indent_text(token.data, context.indent)) out.write("\n") else: # pragma: no cover raise RuntimeError() return out.getvalue()