Skip to content

Commit

Permalink
Merge branch 'uniquedotexporter_deterministic' of https://github.com/…
Browse files Browse the repository at this point in the history
…eckp/anytree into eckp-uniquedotexporter_deterministic
  • Loading branch information
c0fec0de committed Oct 11, 2023
2 parents 351aab5 + aab1dc0 commit fe240c6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 40 deletions.
67 changes: 36 additions & 31 deletions anytree/exporter/dotexporter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import codecs
import itertools
import logging
import re
from os import path, remove
Expand Down Expand Up @@ -361,26 +362,26 @@ def __init__(
>>> s1ca = Node("sub1Ca", parent=s1c)
>>> from anytree.exporter import UniqueDotExporter
>>> for line in UniqueDotExporter(root): # doctest: +SKIP
>>> for line in UniqueDotExporter(root):
... print(line)
digraph tree {
"0x7f1bf2c9c510" [label="root"];
"0x7f1bf2c9c5a0" [label="sub0"];
"0x7f1bf2c9c630" [label="s0"];
"0x7f1bf2c9c6c0" [label="s0"];
"0x7f1bf2c9c750" [label="sub1"];
"0x7f1bf2c9c7e0" [label="s1"];
"0x7f1bf2c9c870" [label="s1"];
"0x7f1bf2c9c900" [label="s1"];
"0x7f1bf2c9c990" [label="sub1Ca"];
"0x7f1bf2c9c510" -> "0x7f1bf2c9c5a0";
"0x7f1bf2c9c510" -> "0x7f1bf2c9c750";
"0x7f1bf2c9c5a0" -> "0x7f1bf2c9c630";
"0x7f1bf2c9c5a0" -> "0x7f1bf2c9c6c0";
"0x7f1bf2c9c750" -> "0x7f1bf2c9c7e0";
"0x7f1bf2c9c750" -> "0x7f1bf2c9c870";
"0x7f1bf2c9c750" -> "0x7f1bf2c9c900";
"0x7f1bf2c9c900" -> "0x7f1bf2c9c990";
"0" [label="root"];
"1" [label="sub0"];
"2" [label="s0"];
"3" [label="s0"];
"4" [label="sub1"];
"5" [label="s1"];
"6" [label="s1"];
"7" [label="s1"];
"8" [label="sub1Ca"];
"0" -> "1";
"0" -> "4";
"1" -> "2";
"1" -> "3";
"4" -> "5";
"4" -> "6";
"4" -> "7";
"7" -> "8";
}
The resulting graph:
Expand All @@ -396,16 +397,16 @@ def __init__(
>>> s0a = AnyNode(id="s0", parent=s0)
>>> from anytree.exporter import UniqueDotExporter
>>> for line in UniqueDotExporter(root, nodeattrfunc=lambda n: 'label="%s"' % (n.id)): # doctest: +SKIP
>>> for line in UniqueDotExporter(root, nodeattrfunc=lambda n: 'label="%s"' % (n.id)):
... print(line)
digraph tree {
"0x7f5c70449af8" [label="root"];
"0x7f5c70449bd0" [label="sub0"];
"0x7f5c70449c60" [label="s0"];
"0x7f5c70449cf0" [label="s0"];
"0x7f5c70449af8" -> "0x7f5c70449bd0";
"0x7f5c70449bd0" -> "0x7f5c70449c60";
"0x7f5c70449bd0" -> "0x7f5c70449cf0";
"0" [label="root"];
"1" [label="sub0"];
"2" [label="s0"];
"3" [label="s0"];
"0" -> "1";
"1" -> "2";
"1" -> "3";
}
"""
super(UniqueDotExporter, self).__init__(
Expand All @@ -418,12 +419,16 @@ def __init__(
nodeattrfunc=nodeattrfunc,
edgeattrfunc=edgeattrfunc,
edgetypefunc=edgetypefunc,
maxlevel=maxlevel,
)

@staticmethod
def _default_nodenamefunc(node):
return hex(id(node))
self.node_ids = {}
self.node_counter = itertools.count()

# pylint: disable=arguments-differ
def _default_nodenamefunc(self, node):
node_id = id(node)
if node_id not in self.node_ids:
self.node_ids[node_id] = next(self.node_counter)
return self.node_ids[id(node)]

@staticmethod
def _default_nodeattrfunc(node):
Expand Down
14 changes: 5 additions & 9 deletions tests/test_uniquedotexporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,16 @@ def test_tree1():
s0 = Node("sub0", parent=root)
s0b = Node("sub0B", parent=s0)

id_root = hex(id(root))
id_s0 = hex(id(s0))
id_s0b = hex(id(s0b))

lines = tuple(UniqueDotExporter(root))
eq_(
lines,
(
"digraph tree {",
' "{id_root}" [label="root"];'.format(id_root=id_root),
' "{id_s0}" [label="sub0"];'.format(id_s0=id_s0),
' "{id_s0b}" [label="sub0B"];'.format(id_s0b=id_s0b),
' "{id_root}" -> "{id_s0}";'.format(id_root=id_root, id_s0=id_s0),
' "{id_s0}" -> "{id_s0b}";'.format(id_s0=id_s0, id_s0b=id_s0b),
' "0" [label="root"];',
' "1" [label="sub0"];',
' "2" [label="sub0B"];',
' "0" -> "1";',
' "1" -> "2";',
"}",
),
)

0 comments on commit fe240c6

Please sign in to comment.