Download lam2/lib/python3.8/site-packages/networkx/readwrite/tests/test_text.py from anusfoil/LLaQo-ckpts: direct link, hf CLI and curl.
- Browser
- Download file 7.93 kB
-
https://huggingface.co/anusfoil/LLaQo-ckpts/resolve/main/lam2/lib/python3.8/site-packages/networkx/readwrite/tests/test_text.py
- Command line
-
hf download hf://anusfoil/LLaQo-ckpts/lam2/lib/python3.8/site-packages/networkx/readwrite/tests/test_text.py
-
curl -L -o test_text.py https://huggingface.co/anusfoil/LLaQo-ckpts/resolve/main/lam2/lib/python3.8/site-packages/networkx/readwrite/tests/test_text.py
7.93 kB
| from textwrap import dedent | |
| import pytest | |
| import networkx as nx | |
| def test_directed_tree_str(): | |
| # Create a directed forest with labels | |
| graph = nx.balanced_tree(r=2, h=2, create_using=nx.DiGraph) | |
| for node in graph.nodes: | |
| graph.nodes[node]["label"] = "node_" + chr(ord("a") + node) | |
| node_target = dedent( | |
| """ | |
| βββ 0 | |
| βββΌ 1 | |
| βΒ Β βββΌ 3 | |
| βΒ Β βββΌ 4 | |
| βββΌ 2 | |
| βββΌ 5 | |
| βββΌ 6 | |
| """ | |
| ).strip() | |
| label_target = dedent( | |
| """ | |
| βββ node_a | |
| βββΌ node_b | |
| βΒ Β βββΌ node_d | |
| βΒ Β βββΌ node_e | |
| βββΌ node_c | |
| βββΌ node_f | |
| βββΌ node_g | |
| """ | |
| ).strip() | |
| # Basic node case | |
| ret = nx.forest_str(graph, with_labels=False) | |
| print(ret) | |
| assert ret == node_target | |
| # Basic label case | |
| ret = nx.forest_str(graph, with_labels=True) | |
| print(ret) | |
| assert ret == label_target | |
| # Custom write function case | |
| lines = [] | |
| ret = nx.forest_str(graph, write=lines.append, with_labels=False) | |
| assert ret is None | |
| assert lines == node_target.split("\n") | |
| # Smoke test to ensure passing the print function works. To properly test | |
| # this case we would need to capture stdout. (for potential reference | |
| # implementation see :class:`ubelt.util_stream.CaptureStdout`) | |
| ret = nx.forest_str(graph, write=print) | |
| assert ret is None | |
| def test_empty_graph(): | |
| assert nx.forest_str(nx.DiGraph()) == "β" | |
| assert nx.forest_str(nx.Graph()) == "β" | |
| def test_directed_multi_tree_forest(): | |
| tree1 = nx.balanced_tree(r=2, h=2, create_using=nx.DiGraph) | |
| tree2 = nx.balanced_tree(r=2, h=2, create_using=nx.DiGraph) | |
| forest = nx.disjoint_union_all([tree1, tree2]) | |
| ret = nx.forest_str(forest) | |
| print(ret) | |
| target = dedent( | |
| """ | |
| βββ 0 | |
| βΒ Β βββΌ 1 | |
| βΒ Β βΒ Β βββΌ 3 | |
| βΒ Β βΒ Β βββΌ 4 | |
| βΒ Β βββΌ 2 | |
| βΒ Β βββΌ 5 | |
| βΒ Β βββΌ 6 | |
| βββ 7 | |
| βββΌ 8 | |
| βΒ Β βββΌ 10 | |
| βΒ Β βββΌ 11 | |
| βββΌ 9 | |
| βββΌ 12 | |
| βββΌ 13 | |
| """ | |
| ).strip() | |
| assert ret == target | |
| tree3 = nx.balanced_tree(r=2, h=2, create_using=nx.DiGraph) | |
| forest = nx.disjoint_union_all([tree1, tree2, tree3]) | |
| ret = nx.forest_str(forest, sources=[0, 14, 7]) | |
| print(ret) | |
| target = dedent( | |
| """ | |
| βββ 0 | |
| βΒ Β βββΌ 1 | |
| βΒ Β βΒ Β βββΌ 3 | |
| βΒ Β βΒ Β βββΌ 4 | |
| βΒ Β βββΌ 2 | |
| βΒ Β βββΌ 5 | |
| βΒ Β βββΌ 6 | |
| βββ 14 | |
| βΒ Β βββΌ 15 | |
| βΒ Β βΒ Β βββΌ 17 | |
| βΒ Β βΒ Β βββΌ 18 | |
| βΒ Β βββΌ 16 | |
| βΒ Β βββΌ 19 | |
| βΒ Β βββΌ 20 | |
| βββ 7 | |
| βββΌ 8 | |
| βΒ Β βββΌ 10 | |
| βΒ Β βββΌ 11 | |
| βββΌ 9 | |
| βββΌ 12 | |
| βββΌ 13 | |
| """ | |
| ).strip() | |
| assert ret == target | |
| ret = nx.forest_str(forest, sources=[0, 14, 7], ascii_only=True) | |
| print(ret) | |
| target = dedent( | |
| """ | |
| +-- 0 | |
| :Β Β |-> 1 | |
| :Β Β |Β Β |-> 3 | |
| :Β Β |Β Β L-> 4 | |
| :Β Β L-> 2 | |
| :Β Β |-> 5 | |
| :Β Β L-> 6 | |
| +-- 14 | |
| :Β Β |-> 15 | |
| :Β Β |Β Β |-> 17 | |
| :Β Β |Β Β L-> 18 | |
| :Β Β L-> 16 | |
| :Β Β |-> 19 | |
| :Β Β L-> 20 | |
| +-- 7 | |
| |-> 8 | |
| |Β Β |-> 10 | |
| |Β Β L-> 11 | |
| L-> 9 | |
| |-> 12 | |
| L-> 13 | |
| """ | |
| ).strip() | |
| assert ret == target | |
| def test_undirected_multi_tree_forest(): | |
| tree1 = nx.balanced_tree(r=2, h=2, create_using=nx.Graph) | |
| tree2 = nx.balanced_tree(r=2, h=2, create_using=nx.Graph) | |
| tree2 = nx.relabel_nodes(tree2, {n: n + len(tree1) for n in tree2.nodes}) | |
| forest = nx.union(tree1, tree2) | |
| ret = nx.forest_str(forest, sources=[0, 7]) | |
| print(ret) | |
| target = dedent( | |
| """ | |
| βββ 0 | |
| βΒ Β βββ 1 | |
| βΒ Β βΒ Β βββ 3 | |
| βΒ Β βΒ Β βββ 4 | |
| βΒ Β βββ 2 | |
| βΒ Β βββ 5 | |
| βΒ Β βββ 6 | |
| βββ 7 | |
| βββ 8 | |
| βΒ Β βββ 10 | |
| βΒ Β βββ 11 | |
| βββ 9 | |
| βββ 12 | |
| βββ 13 | |
| """ | |
| ).strip() | |
| assert ret == target | |
| ret = nx.forest_str(forest, sources=[0, 7], ascii_only=True) | |
| print(ret) | |
| target = dedent( | |
| """ | |
| +-- 0 | |
| :Β Β |-- 1 | |
| :Β Β |Β Β |-- 3 | |
| :Β Β |Β Β L-- 4 | |
| :Β Β L-- 2 | |
| :Β Β |-- 5 | |
| :Β Β L-- 6 | |
| +-- 7 | |
| |-- 8 | |
| |Β Β |-- 10 | |
| |Β Β L-- 11 | |
| L-- 9 | |
| |-- 12 | |
| L-- 13 | |
| """ | |
| ).strip() | |
| assert ret == target | |
| def test_undirected_tree_str(): | |
| # Create a directed forest with labels | |
| graph = nx.balanced_tree(r=2, h=2, create_using=nx.Graph) | |
| # arbitrary starting point | |
| nx.forest_str(graph) | |
| node_target0 = dedent( | |
| """ | |
| βββ 0 | |
| βββ 1 | |
| βΒ Β βββ 3 | |
| βΒ Β βββ 4 | |
| βββ 2 | |
| βββ 5 | |
| βββ 6 | |
| """ | |
| ).strip() | |
| # defined starting point | |
| ret = nx.forest_str(graph, sources=[0]) | |
| print(ret) | |
| assert ret == node_target0 | |
| # defined starting point | |
| node_target2 = dedent( | |
| """ | |
| βββ 2 | |
| βββ 0 | |
| βΒ Β βββ 1 | |
| βΒ Β βββ 3 | |
| βΒ Β βββ 4 | |
| βββ 5 | |
| βββ 6 | |
| """ | |
| ).strip() | |
| ret = nx.forest_str(graph, sources=[2]) | |
| print(ret) | |
| assert ret == node_target2 | |
| def test_forest_str_errors(): | |
| ugraph = nx.complete_graph(3, create_using=nx.Graph) | |
| with pytest.raises(nx.NetworkXNotImplemented): | |
| nx.forest_str(ugraph) | |
| dgraph = nx.complete_graph(3, create_using=nx.DiGraph) | |
| with pytest.raises(nx.NetworkXNotImplemented): | |
| nx.forest_str(dgraph) | |
| def test_overspecified_sources(): | |
| """ | |
| When sources are directly specified, we wont be able to determine when we | |
| are in the last component, so there will always be a trailing, leftmost | |
| pipe. | |
| """ | |
| graph = nx.disjoint_union_all( | |
| [ | |
| nx.balanced_tree(r=2, h=1, create_using=nx.DiGraph), | |
| nx.balanced_tree(r=1, h=2, create_using=nx.DiGraph), | |
| nx.balanced_tree(r=2, h=1, create_using=nx.DiGraph), | |
| ] | |
| ) | |
| # defined starting point | |
| target1 = dedent( | |
| """ | |
| βββ 0 | |
| βΒ Β βββΌ 1 | |
| βΒ Β βββΌ 2 | |
| βββ 3 | |
| βΒ Β βββΌ 4 | |
| βΒ Β βββΌ 5 | |
| βββ 6 | |
| βΒ Β βββΌ 7 | |
| βΒ Β βββΌ 8 | |
| """ | |
| ).strip() | |
| target2 = dedent( | |
| """ | |
| βββ 0 | |
| βΒ Β βββΌ 1 | |
| βΒ Β βββΌ 2 | |
| βββ 3 | |
| βΒ Β βββΌ 4 | |
| βΒ Β βββΌ 5 | |
| βββ 6 | |
| βββΌ 7 | |
| βββΌ 8 | |
| """ | |
| ).strip() | |
| lines = [] | |
| nx.forest_str(graph, write=lines.append, sources=graph.nodes) | |
| got1 = chr(10).join(lines) | |
| print("got1: ") | |
| print(got1) | |
| lines = [] | |
| nx.forest_str(graph, write=lines.append) | |
| got2 = chr(10).join(lines) | |
| print("got2: ") | |
| print(got2) | |
| assert got1 == target1 | |
| assert got2 == target2 | |