add tests directory
This commit is contained in:
parent
f19ab0e8c0
commit
b0b07c9425
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -115,8 +115,8 @@
|
|||
<div class="viewcode-block" id="Node.add_child">
|
||||
<a class="viewcode-back" href="../api.html#pynodes.Node.add_child">[docs]</a>
|
||||
<span class="k">def</span> <span class="nf">add_child</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">child</span><span class="p">:</span> <span class="n">Node</span><span class="p">)</span> <span class="o">-></span> <span class="kc">None</span><span class="p">:</span>
|
||||
<span class="w"> </span><span class="sd">""" </span>
|
||||
<span class="sd"> Add new child node to current instance </span>
|
||||
<span class="w"> </span><span class="sd">"""</span>
|
||||
<span class="sd"> Add new child node to current instance</span>
|
||||
|
||||
<span class="sd"> :param child: Node object</span>
|
||||
<span class="sd"> """</span>
|
||||
|
@ -133,13 +133,17 @@
|
|||
|
||||
<span class="nd">@property</span>
|
||||
<span class="k">def</span> <span class="nf">siblings</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-></span> <span class="nb">list</span><span class="p">[</span><span class="n">Node</span><span class="p">]:</span>
|
||||
<span class="w"> </span><span class="sd">""" returns all the siblings of the Node object """</span>
|
||||
<span class="w"> </span><span class="sd">"""</span>
|
||||
<span class="sd"> Returns all the siblings of the Node object</span>
|
||||
<span class="sd"> """</span>
|
||||
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_siblings</span><span class="p">():</span>
|
||||
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">children</span>
|
||||
|
||||
<span class="nd">@property</span>
|
||||
<span class="k">def</span> <span class="nf">parents</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-></span> <span class="nb">list</span><span class="p">[</span><span class="n">Node</span><span class="p">]:</span>
|
||||
<span class="w"> </span><span class="sd">""" returns all the ancestors of the Node object """</span>
|
||||
<span class="w"> </span><span class="sd">"""</span>
|
||||
<span class="sd"> Returns all the ancestors of the Node object</span>
|
||||
<span class="sd"> """</span>
|
||||
<span class="n">parents</span> <span class="o">=</span> <span class="p">[]</span>
|
||||
<span class="n">p</span> <span class="o">=</span> <span class="bp">self</span>
|
||||
<span class="k">while</span> <span class="n">p</span><span class="o">.</span><span class="n">has_parent</span><span class="p">():</span>
|
||||
|
@ -149,7 +153,9 @@
|
|||
|
||||
<span class="nd">@property</span>
|
||||
<span class="k">def</span> <span class="nf">level</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-></span> <span class="nb">int</span><span class="p">:</span>
|
||||
<span class="w"> </span><span class="sd">""" returns the level of the Node object """</span>
|
||||
<span class="w"> </span><span class="sd">"""</span>
|
||||
<span class="sd"> Returns the level of the Node object</span>
|
||||
<span class="sd"> """</span>
|
||||
<span class="n">level</span> <span class="o">=</span> <span class="mi">0</span>
|
||||
<span class="n">p</span> <span class="o">=</span> <span class="bp">self</span>
|
||||
<span class="k">while</span> <span class="n">p</span><span class="o">.</span><span class="n">has_parent</span><span class="p">():</span>
|
||||
|
@ -160,7 +166,7 @@
|
|||
<span class="nd">@property</span>
|
||||
<span class="k">def</span> <span class="nf">path</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-></span> <span class="nb">str</span><span class="p">:</span>
|
||||
<span class="w"> </span><span class="sd">"""</span>
|
||||
<span class="sd"> returns a representation of the ancestor lineage of self</span>
|
||||
<span class="sd"> Returns a representation of the ancestor lineage of self</span>
|
||||
<span class="sd"> """</span>
|
||||
<span class="n">path</span> <span class="o">=</span> <span class="s1">''</span>
|
||||
<span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="nb">reversed</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">parents</span><span class="p">):</span>
|
||||
|
@ -185,7 +191,9 @@
|
|||
<div class="viewcode-block" id="Node.is_child">
|
||||
<a class="viewcode-back" href="../api.html#pynodes.Node.is_child">[docs]</a>
|
||||
<span class="k">def</span> <span class="nf">is_child</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-></span> <span class="nb">bool</span><span class="p">:</span>
|
||||
<span class="w"> </span><span class="sd">""" check if Node object is a child of the other Node object """</span>
|
||||
<span class="w"> </span><span class="sd">"""</span>
|
||||
<span class="sd"> Check if Node object is a child of the other Node object</span>
|
||||
<span class="sd"> """</span>
|
||||
<span class="k">if</span> <span class="n">other</span> <span class="ow">in</span> <span class="p">[</span><span class="n">s</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">children</span><span class="p">]:</span>
|
||||
<span class="k">return</span> <span class="kc">True</span>
|
||||
<span class="k">else</span><span class="p">:</span>
|
||||
|
@ -195,7 +203,9 @@
|
|||
<div class="viewcode-block" id="Node.pretty_print">
|
||||
<a class="viewcode-back" href="../api.html#pynodes.Node.pretty_print">[docs]</a>
|
||||
<span class="k">def</span> <span class="nf">pretty_print</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">option</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s1">'default'</span><span class="p">)</span> <span class="o">-></span> <span class="kc">None</span><span class="p">:</span>
|
||||
<span class="w"> </span><span class="sd">""" print children tree from current instance """</span>
|
||||
<span class="w"> </span><span class="sd">"""</span>
|
||||
<span class="sd"> Print children tree from current instance</span>
|
||||
<span class="sd"> """</span>
|
||||
<span class="n">dashes</span> <span class="o">=</span> <span class="s1">' '</span><span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">level</span><span class="o">+</span><span class="s1">'|'</span><span class="o">+</span><span class="s1">'--'</span><span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">level</span><span class="o">+</span><span class="s1">' '</span>
|
||||
<span class="k">if</span> <span class="n">option</span> <span class="o">==</span> <span class="s1">'id'</span><span class="p">:</span>
|
||||
<span class="n">dashes</span> <span class="o">+=</span> <span class="sa">f</span><span class="s1">'[</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">id</span><span class="si">}</span><span class="s1">] '</span>
|
||||
|
|
|
@ -48,9 +48,6 @@ Basic Usage:
|
|||
|
||||
alice.pretty_print()
|
||||
|
||||
.. code-block::
|
||||
:caption: Pretty print of the tree
|
||||
|
||||
Alice
|
||||
-- Bob
|
||||
-- Eve
|
||||
|
@ -59,4 +56,3 @@ Basic Usage:
|
|||
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -159,7 +159,7 @@ found</p>
|
|||
<dl class="py method">
|
||||
<dt class="sig sig-object py" id="pynodes.Node.is_child">
|
||||
<span class="sig-name descname"><span class="pre">is_child</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">other</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">str</span></span></em><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">→</span> <span class="sig-return-typehint"><span class="pre">bool</span></span></span><a class="reference internal" href="_modules/pynodes.html#Node.is_child"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#pynodes.Node.is_child" title="Link to this definition">¶</a></dt>
|
||||
<dd><p>check if Node object is a child of the other Node object</p>
|
||||
<dd><p>Check if Node object is a child of the other Node object</p>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="py method">
|
||||
|
@ -176,25 +176,25 @@ found</p>
|
|||
<dl class="py property">
|
||||
<dt class="sig sig-object py" id="pynodes.Node.level">
|
||||
<em class="property"><span class="pre">property</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">level</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="pre">int</span></em><a class="headerlink" href="#pynodes.Node.level" title="Link to this definition">¶</a></dt>
|
||||
<dd><p>returns the level of the Node object</p>
|
||||
<dd><p>Returns the level of the Node object</p>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="py property">
|
||||
<dt class="sig sig-object py" id="pynodes.Node.parents">
|
||||
<em class="property"><span class="pre">property</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">parents</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="pre">list</span><span class="p"><span class="pre">[</span></span><a class="reference internal" href="#pynodes.Node" title="pynodes.Node"><span class="pre">Node</span></a><span class="p"><span class="pre">]</span></span></em><a class="headerlink" href="#pynodes.Node.parents" title="Link to this definition">¶</a></dt>
|
||||
<dd><p>returns all the ancestors of the Node object</p>
|
||||
<dd><p>Returns all the ancestors of the Node object</p>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="py property">
|
||||
<dt class="sig sig-object py" id="pynodes.Node.path">
|
||||
<em class="property"><span class="pre">property</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">path</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="pre">str</span></em><a class="headerlink" href="#pynodes.Node.path" title="Link to this definition">¶</a></dt>
|
||||
<dd><p>returns a representation of the ancestor lineage of self</p>
|
||||
<dd><p>Returns a representation of the ancestor lineage of self</p>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="py method">
|
||||
<dt class="sig sig-object py" id="pynodes.Node.pretty_print">
|
||||
<span class="sig-name descname"><span class="pre">pretty_print</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">option</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">str</span></span><span class="w"> </span><span class="o"><span class="pre">=</span></span><span class="w"> </span><span class="default_value"><span class="pre">'default'</span></span></em><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">→</span> <span class="sig-return-typehint"><span class="pre">None</span></span></span><a class="reference internal" href="_modules/pynodes.html#Node.pretty_print"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#pynodes.Node.pretty_print" title="Link to this definition">¶</a></dt>
|
||||
<dd><p>print children tree from current instance</p>
|
||||
<dd><p>Print children tree from current instance</p>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="py method">
|
||||
|
@ -206,7 +206,7 @@ found</p>
|
|||
<dl class="py property">
|
||||
<dt class="sig sig-object py" id="pynodes.Node.siblings">
|
||||
<em class="property"><span class="pre">property</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">siblings</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="pre">list</span><span class="p"><span class="pre">[</span></span><a class="reference internal" href="#pynodes.Node" title="pynodes.Node"><span class="pre">Node</span></a><span class="p"><span class="pre">]</span></span></em><a class="headerlink" href="#pynodes.Node.siblings" title="Link to this definition">¶</a></dt>
|
||||
<dd><p>returns all the siblings of the Node object</p>
|
||||
<dd><p>Returns all the siblings of the Node object</p>
|
||||
</dd></dl>
|
||||
|
||||
</dd></dl>
|
||||
|
|
|
@ -111,16 +111,12 @@
|
|||
<span class="n">alice</span><span class="o">.</span><span class="n">add_child</span><span class="p">(</span><span class="n">eve</span><span class="p">)</span>
|
||||
|
||||
<span class="n">alice</span><span class="o">.</span><span class="n">pretty_print</span><span class="p">()</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<div class="literal-block-wrapper docutils container" id="id1">
|
||||
<div class="code-block-caption"><span class="caption-text">Pretty print of the tree</span><a class="headerlink" href="#id1" title="Link to this code">¶</a></div>
|
||||
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span> <span class="n">Alice</span>
|
||||
|
||||
<span class="n">Alice</span>
|
||||
<span class="o">--</span> <span class="n">Bob</span>
|
||||
<span class="o">--</span> <span class="n">Eve</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
</section>
|
||||
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -48,9 +48,6 @@ Basic Usage:
|
|||
|
||||
alice.pretty_print()
|
||||
|
||||
.. code-block::
|
||||
:caption: Pretty print of the tree
|
||||
|
||||
Alice
|
||||
-- Bob
|
||||
-- Eve
|
||||
|
@ -59,4 +56,3 @@ Basic Usage:
|
|||
|
||||
|
||||
|
||||
|
||||
|
|
22
pynodes.py
22
pynodes.py
|
@ -232,26 +232,4 @@ class Node:
|
|||
return self.name
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
# while True:
|
||||
# master_node = Node.create_random_nodes(depth=3, type_='cmd')
|
||||
# node_list = [master_node, master_node.children[0],
|
||||
# master_node.children[0].children[0]]
|
||||
# random.shuffle(node_list)
|
||||
# print([n.name for n in node_list])
|
||||
# print(Node.check_lineage(node_list))
|
||||
# input('pause')
|
||||
class Person(Node):
|
||||
pass
|
||||
|
||||
bob = Person('Bob')
|
||||
eve = Person('Eve')
|
||||
alice = Person('Alice')
|
||||
|
||||
# Alice is parent of bob and eve
|
||||
alice.add_child(bob)
|
||||
alice.add_child(eve)
|
||||
|
||||
alice.pretty_print(option = 'id')
|
||||
|
||||
print(alice.id)
|
||||
|
|
Binary file not shown.
|
@ -0,0 +1,21 @@
|
|||
from pathlib import Path
|
||||
import sys
|
||||
# to include the main lib of the tests
|
||||
sys.path.append(str(Path(__file__).parent.parent))
|
||||
|
||||
from pynodes import Node
|
||||
|
||||
if __name__ == '__main__':
|
||||
class Person(Node):
|
||||
pass
|
||||
|
||||
bob = Person('Bob')
|
||||
eve = Person('Eve')
|
||||
alice = Person('Alice')
|
||||
|
||||
# Alice is parent of bob and eve
|
||||
alice.add_child(bob)
|
||||
alice.add_child(eve)
|
||||
|
||||
alice.pretty_print(option = 'id')
|
||||
|
|
@ -1,20 +1,9 @@
|
|||
import inspect
|
||||
from pathlib import Path
|
||||
import sys
|
||||
# to include the main lib of the tests
|
||||
sys.path.append(str(Path(__file__).parent.parent))
|
||||
|
||||
def f(*args, **kwargs):
|
||||
print(kwargs)
|
||||
|
||||
class SquareNumber(int):
|
||||
def __new__(cls):
|
||||
return super().__new__(cls)
|
||||
|
||||
|
||||
class Test:
|
||||
def __new__(cls, *args):
|
||||
print(args[0])
|
||||
return super().__new__(cls)
|
||||
|
||||
def __init__(self, first_arg: str = 'first', second_arg: str = 'second' ):
|
||||
self.name = first_arg
|
||||
import pynodes
|
||||
|
||||
if __name__ == '__main__':
|
||||
t = Test(first_arg = 'hello', second_arg = 'world')
|
||||
pass
|
||||
|
|
Loading…
Reference in New Issue