-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
150 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
""" | ||
Node Classes. | ||
* :any:`Node`: a simple tree node with at least a name attribute and any number of additional attributes. | ||
* :any:`AnyNode`: a generic tree node with any number of attributes. | ||
* :any:`NodeMixin`: extends any python class to a tree node. | ||
""" | ||
|
||
from .anynode import AnyNode # noqa | ||
from .exceptions import LoopError # noqa | ||
from .exceptions import TreeError # noqa | ||
from .node import Node # noqa | ||
from .nodemixin import NodeMixin # noqa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from .nodemixin import NodeMixin | ||
from .util import _repr | ||
|
||
|
||
class AnyNode(NodeMixin, object): | ||
|
||
def __init__(self, parent=None, **kwargs): | ||
u""" | ||
A generic tree node with any `kwargs`. | ||
>>> from anytree import AnyNode, RenderTree | ||
>>> root = AnyNode(id="root") | ||
>>> s0 = AnyNode(id="sub0", parent=root) | ||
>>> s0b = AnyNode(id="sub0B", parent=s0, foo=4, bar=109) | ||
>>> s0a = AnyNode(id="sub0A", parent=s0) | ||
>>> s1 = AnyNode(id="sub1", parent=root) | ||
>>> s1a = AnyNode(id="sub1A", parent=s1) | ||
>>> s1b = AnyNode(id="sub1B", parent=s1, bar=8) | ||
>>> s1c = AnyNode(id="sub1C", parent=s1) | ||
>>> s1ca = AnyNode(id="sub1Ca", parent=s1c) | ||
>>> root | ||
AnyNode(id='root') | ||
>>> s0 | ||
AnyNode(id='sub0') | ||
>>> print(RenderTree(root)) | ||
AnyNode(id='root') | ||
├── AnyNode(id='sub0') | ||
│ ├── AnyNode(bar=109, foo=4, id='sub0B') | ||
│ └── AnyNode(id='sub0A') | ||
└── AnyNode(id='sub1') | ||
├── AnyNode(id='sub1A') | ||
├── AnyNode(bar=8, id='sub1B') | ||
└── AnyNode(id='sub1C') | ||
└── AnyNode(id='sub1Ca') | ||
""" | ||
self.__dict__.update(kwargs) | ||
self.parent = parent | ||
|
||
def __repr__(self): | ||
return _repr(self) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class TreeError(RuntimeError): | ||
|
||
"""Tree Error.""" | ||
|
||
pass | ||
|
||
|
||
class LoopError(TreeError): | ||
|
||
"""Tree contains infinite loop.""" | ||
|
||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from .nodemixin import NodeMixin | ||
from .util import _repr | ||
|
||
|
||
class Node(NodeMixin, object): | ||
|
||
def __init__(self, name, parent=None, **kwargs): | ||
u""" | ||
A simple tree node with a `name` and any `kwargs`. | ||
>>> from anytree import Node, RenderTree | ||
>>> root = Node("root") | ||
>>> s0 = Node("sub0", parent=root) | ||
>>> s0b = Node("sub0B", parent=s0, foo=4, bar=109) | ||
>>> s0a = Node("sub0A", parent=s0) | ||
>>> s1 = Node("sub1", parent=root) | ||
>>> s1a = Node("sub1A", parent=s1) | ||
>>> s1b = Node("sub1B", parent=s1, bar=8) | ||
>>> s1c = Node("sub1C", parent=s1) | ||
>>> s1ca = Node("sub1Ca", parent=s1c) | ||
>>> print(RenderTree(root)) | ||
Node('/root') | ||
├── Node('/root/sub0') | ||
│ ├── Node('/root/sub0/sub0B', bar=109, foo=4) | ||
│ └── Node('/root/sub0/sub0A') | ||
└── Node('/root/sub1') | ||
├── Node('/root/sub1/sub1A') | ||
├── Node('/root/sub1/sub1B', bar=8) | ||
└── Node('/root/sub1/sub1C') | ||
└── Node('/root/sub1/sub1C/sub1Ca') | ||
""" | ||
self.__dict__.update(kwargs) | ||
self.name = name | ||
self.parent = parent | ||
|
||
@property | ||
def name(self): | ||
"""Name.""" | ||
return self._name | ||
|
||
@name.setter | ||
def name(self, value): | ||
self._name = value | ||
|
||
def __repr__(self): | ||
args = ["%r" % self.separator.join([""] + [str(node.name) for node in self.path])] | ||
return _repr(self, args=args) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
def _repr(node, args=None): | ||
classname = node.__class__.__name__ | ||
args = args or [] | ||
for key, value in filter(lambda item: not item[0].startswith("_"), | ||
sorted(node.__dict__.items(), | ||
key=lambda item: item[0])): | ||
args.append("%s=%r" % (key, value)) | ||
return "%s(%s)" % (classname, ", ".join(args)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters