writing the documentation
This commit is contained in:
parent
bcab8b2427
commit
82cb7cf320
Binary file not shown.
Binary file not shown.
|
@ -43,7 +43,6 @@
|
|||
<span class="sd"> Should be subclassed only</span>
|
||||
<span class="sd"> """</span>
|
||||
<span class="c1"># Class Var for statistics</span>
|
||||
<span class="n">total_nodes_number</span><span class="p">:</span> <span class="n">ClassVar</span><span class="p">[</span><span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>
|
||||
<span class="n">deepest_level</span><span class="p">:</span> <span class="n">ClassVar</span><span class="p">[</span><span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>
|
||||
<span class="n">largest_sibling_number</span><span class="p">:</span> <span class="n">ClassVar</span><span class="p">[</span><span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>
|
||||
<span class="n">all_nodes</span><span class="p">:</span> <span class="n">ClassVar</span><span class="p">[</span><span class="nb">list</span><span class="p">[</span><span class="n">Node</span><span class="p">]]</span> <span class="o">=</span> <span class="p">[]</span>
|
||||
|
@ -52,7 +51,6 @@
|
|||
<span class="w"> </span><span class="sd">""" each subclass must define its own ClassVar """</span>
|
||||
<span class="c1"># TODO to be renamed for clarity</span>
|
||||
<span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">__init_subclass__</span><span class="p">()</span>
|
||||
<span class="bp">cls</span><span class="o">.</span><span class="n">total_nodes_number</span><span class="p">:</span> <span class="n">ClassVar</span><span class="p">[</span><span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>
|
||||
<span class="bp">cls</span><span class="o">.</span><span class="n">deepest_level</span><span class="p">:</span> <span class="n">ClassVar</span><span class="p">[</span><span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>
|
||||
<span class="bp">cls</span><span class="o">.</span><span class="n">largest_sibling_number</span><span class="p">:</span> <span class="n">ClassVar</span><span class="p">[</span><span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>
|
||||
<span class="bp">cls</span><span class="o">.</span><span class="n">all_nodes</span><span class="p">:</span> <span class="n">ClassVar</span><span class="p">[</span><span class="nb">list</span><span class="p">[</span><span class="n">Node</span><span class="p">]]</span> <span class="o">=</span> <span class="p">[]</span>
|
||||
|
@ -61,7 +59,7 @@
|
|||
<span class="k">def</span> <span class="fm">__new__</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">name</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">parent</span><span class="p">:</span> <span class="n">Node</span> <span class="o">|</span> <span class="kc">None</span> <span class="o">=</span> <span class="kc">None</span><span class="p">)</span> <span class="o">-></span> <span class="kc">None</span><span class="p">:</span>
|
||||
<span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="bp">cls</span><span class="o">.</span><span class="n">all_nodes</span><span class="p">:</span>
|
||||
<span class="k">if</span> <span class="n">name</span> <span class="o">==</span> <span class="n">n</span><span class="o">.</span><span class="n">name</span><span class="p">:</span>
|
||||
<span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">'Node with this name already exist'</span><span class="p">)</span>
|
||||
<span class="k">raise</span> <span class="ne">AttributeError</span><span class="p">(</span><span class="s1">'Node with this name already exists'</span><span class="p">)</span>
|
||||
<span class="k">else</span><span class="p">:</span>
|
||||
<span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__new__</span><span class="p">(</span><span class="bp">cls</span><span class="p">)</span>
|
||||
|
||||
|
@ -69,9 +67,24 @@
|
|||
<span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">name</span>
|
||||
<span class="bp">self</span><span class="o">.</span><span class="n">parent</span> <span class="o">=</span> <span class="n">parent</span> <span class="c1"># is set with add_child</span>
|
||||
<span class="bp">self</span><span class="o">.</span><span class="n">children</span><span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="n">Node</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span>
|
||||
<span class="nb">type</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">total_nodes_number</span> <span class="o">+=</span> <span class="mi">1</span>
|
||||
<span class="nb">type</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">all_nodes</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
|
||||
|
||||
<div class="viewcode-block" id="Node.add_child">
|
||||
<a class="viewcode-back" href="../index.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">""" add new child node to current instance """</span>
|
||||
<span class="n">child</span><span class="o">.</span><span class="n">parent</span> <span class="o">=</span> <span class="bp">self</span>
|
||||
<span class="k">if</span> <span class="n">child</span><span class="o">.</span><span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="p">[</span><span class="n">c</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">c</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="bp">self</span><span class="o">.</span><span class="n">children</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">child</span><span class="p">)</span>
|
||||
<span class="k">if</span> <span class="n">child</span><span class="o">.</span><span class="n">level</span> <span class="o">></span> <span class="nb">type</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">deepest_level</span><span class="p">:</span>
|
||||
<span class="nb">type</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">deepest_level</span> <span class="o">=</span> <span class="n">child</span><span class="o">.</span><span class="n">level</span>
|
||||
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">children</span><span class="p">)</span> <span class="o">></span> <span class="nb">type</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">largest_sibling_number</span><span class="p">:</span>
|
||||
<span class="nb">type</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">largest_sibling_number</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">children</span><span class="p">)</span>
|
||||
<span class="k">else</span><span class="p">:</span>
|
||||
<span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">'Child with same name already exists'</span><span class="p">)</span></div>
|
||||
|
||||
|
||||
|
||||
<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>
|
||||
|
@ -109,17 +122,25 @@
|
|||
<span class="n">path</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span>
|
||||
<span class="k">return</span> <span class="n">path</span>
|
||||
|
||||
<div class="viewcode-block" id="Node.is_sibling">
|
||||
<a class="viewcode-back" href="../index.html#pynodes.Node.is_sibling">[docs]</a>
|
||||
<span class="k">def</span> <span class="nf">is_sibling</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 sibling of the other Node object """</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">siblings</span><span class="p">]:</span>
|
||||
<span class="k">return</span> <span class="kc">True</span>
|
||||
<span class="k">else</span><span class="p">:</span>
|
||||
<span class="k">return</span> <span class="kc">False</span>
|
||||
<span class="k">return</span> <span class="kc">False</span></div>
|
||||
|
||||
|
||||
<div class="viewcode-block" id="Node.is_child">
|
||||
<a class="viewcode-back" href="../index.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="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>
|
||||
<span class="k">return</span> <span class="kc">False</span>
|
||||
<span class="k">return</span> <span class="kc">False</span></div>
|
||||
|
||||
|
||||
<div class="viewcode-block" id="Node.pretty_print">
|
||||
<a class="viewcode-back" href="../index.html#pynodes.Node.pretty_print">[docs]</a>
|
||||
|
@ -131,48 +152,54 @@
|
|||
<span class="n">c</span><span class="o">.</span><span class="n">pretty_print</span><span class="p">()</span></div>
|
||||
|
||||
|
||||
<div class="viewcode-block" id="Node.add_child">
|
||||
<a class="viewcode-back" href="../index.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">""" add new child node to current instance """</span>
|
||||
<span class="n">child</span><span class="o">.</span><span class="n">parent</span> <span class="o">=</span> <span class="bp">self</span>
|
||||
<span class="k">if</span> <span class="n">child</span><span class="o">.</span><span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="p">[</span><span class="n">c</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">c</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="bp">self</span><span class="o">.</span><span class="n">children</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">child</span><span class="p">)</span>
|
||||
<span class="k">if</span> <span class="n">child</span><span class="o">.</span><span class="n">level</span> <span class="o">></span> <span class="nb">type</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">deepest_level</span><span class="p">:</span>
|
||||
<span class="nb">type</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">deepest_level</span> <span class="o">=</span> <span class="n">child</span><span class="o">.</span><span class="n">level</span>
|
||||
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">children</span><span class="p">)</span> <span class="o">></span> <span class="nb">type</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">largest_sibling_number</span><span class="p">:</span>
|
||||
<span class="nb">type</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">largest_sibling_number</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">children</span><span class="p">)</span>
|
||||
<span class="k">else</span><span class="p">:</span>
|
||||
<span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">'Child with same name already exists'</span><span class="p">)</span></div>
|
||||
|
||||
|
||||
<div class="viewcode-block" id="Node.has_parent">
|
||||
<a class="viewcode-back" href="../index.html#pynodes.Node.has_parent">[docs]</a>
|
||||
<span class="k">def</span> <span class="nf">has_parent</span><span class="p">(</span><span class="bp">self</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 has a parent or not """</span>
|
||||
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">parent</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
|
||||
<span class="k">return</span> <span class="kc">True</span>
|
||||
<span class="k">return</span> <span class="kc">False</span>
|
||||
<span class="k">return</span> <span class="kc">False</span></div>
|
||||
|
||||
|
||||
<div class="viewcode-block" id="Node.has_children">
|
||||
<a class="viewcode-back" href="../index.html#pynodes.Node.has_children">[docs]</a>
|
||||
<span class="k">def</span> <span class="nf">has_children</span><span class="p">(</span><span class="bp">self</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 has one child at least """</span>
|
||||
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">children</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
|
||||
<span class="k">return</span> <span class="kc">True</span>
|
||||
<span class="k">return</span> <span class="kc">False</span>
|
||||
<span class="k">return</span> <span class="kc">False</span></div>
|
||||
|
||||
|
||||
<div class="viewcode-block" id="Node.has_siblings">
|
||||
<a class="viewcode-back" href="../index.html#pynodes.Node.has_siblings">[docs]</a>
|
||||
<span class="k">def</span> <span class="nf">has_siblings</span><span class="p">(</span><span class="bp">self</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 has one sibling at least """</span>
|
||||
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_parent</span><span class="p">()</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">has_children</span><span class="p">()</span> \
|
||||
<span class="ow">and</span> <span class="nb">len</span><span class="p">(</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="p">)</span> <span class="o">></span> <span class="mi">0</span><span class="p">:</span>
|
||||
<span class="k">return</span> <span class="kc">True</span>
|
||||
<span class="k">return</span> <span class="kc">False</span>
|
||||
<span class="k">return</span> <span class="kc">False</span></div>
|
||||
|
||||
|
||||
<div class="viewcode-block" id="Node.get_child">
|
||||
<a class="viewcode-back" href="../index.html#pynodes.Node.get_child">[docs]</a>
|
||||
<span class="k">def</span> <span class="nf">get_child</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-></span> <span class="n">Node</span> <span class="o">|</span> <span class="kc">None</span><span class="p">:</span>
|
||||
<span class="w"> </span><span class="sd">""" find and returns a child with specified name. None if nothing found """</span>
|
||||
<span class="k">for</span> <span class="n">c</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">if</span> <span class="n">c</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="n">name</span><span class="p">:</span>
|
||||
<span class="k">return</span> <span class="n">c</span>
|
||||
<span class="k">return</span> <span class="kc">None</span>
|
||||
<span class="k">return</span> <span class="kc">None</span></div>
|
||||
|
||||
|
||||
<div class="viewcode-block" id="Node.get_sibling">
|
||||
<a class="viewcode-back" href="../index.html#pynodes.Node.get_sibling">[docs]</a>
|
||||
<span class="k">def</span> <span class="nf">get_sibling</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-></span> <span class="n">Node</span> <span class="o">|</span> <span class="kc">None</span><span class="p">:</span>
|
||||
<span class="w"> </span><span class="sd">""" find and returns a sibling with specified name. None if nothing</span>
|
||||
<span class="sd"> found """</span>
|
||||
<span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">siblings</span><span class="p">:</span>
|
||||
<span class="k">if</span> <span class="n">c</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="n">name</span><span class="p">:</span>
|
||||
<span class="k">return</span> <span class="n">c</span>
|
||||
<span class="k">return</span> <span class="kc">None</span>
|
||||
<span class="k">return</span> <span class="kc">None</span></div>
|
||||
|
||||
|
||||
<span class="k">def</span> <span class="nf">get_children</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">:</span> <span class="nb">str</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="c1"># refactoring, recursion is not good</span>
|
||||
|
@ -207,7 +234,6 @@
|
|||
<span class="w"> </span><span class="sd">"""</span>
|
||||
<span class="sd"> reset all the ClassVar members</span>
|
||||
<span class="sd"> """</span>
|
||||
<span class="bp">cls</span><span class="o">.</span><span class="n">total_nodes_number</span> <span class="o">=</span> <span class="mi">0</span>
|
||||
<span class="bp">cls</span><span class="o">.</span><span class="n">deepest_level</span> <span class="o">=</span> <span class="mi">0</span>
|
||||
<span class="bp">cls</span><span class="o">.</span><span class="n">largest_sibling_number</span> <span class="o">=</span> <span class="mi">0</span>
|
||||
<span class="bp">cls</span><span class="o">.</span><span class="n">all_nodes</span> <span class="o">=</span> <span class="p">[]</span></div>
|
||||
|
@ -221,7 +247,7 @@
|
|||
<span class="sd"> Creates random tree of nodes for testing purpose</span>
|
||||
<span class="sd"> """</span>
|
||||
<span class="k">def</span> <span class="nf">create_node</span><span class="p">(</span><span class="n">level</span><span class="p">,</span> <span class="n">i</span><span class="p">):</span>
|
||||
<span class="n">id_</span> <span class="o">=</span> <span class="bp">cls</span><span class="o">.</span><span class="n">total_nodes_number</span> <span class="o">+</span> <span class="mi">1</span>
|
||||
<span class="n">id_</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="n">all_nodes</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span>
|
||||
<span class="k">return</span> <span class="bp">cls</span><span class="p">(</span><span class="sa">f</span><span class="s1">'</span><span class="si">{</span><span class="n">type_</span><span class="si">}</span><span class="s1">_'</span><span class="o">+</span><span class="nb">str</span><span class="p">(</span><span class="n">id_</span><span class="p">))</span>
|
||||
|
||||
<span class="k">def</span> <span class="nf">create_node_list</span><span class="p">(</span><span class="n">level</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">width</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">5</span><span class="p">):</span>
|
||||
|
|
|
@ -13,6 +13,8 @@ A node is an object which is related to other nodes given a tree chart.
|
|||
|
||||
(venv) $ pip install pynodes
|
||||
|
||||
Source code is hosted on my own instance of `gitea <https://gitea.lutix.org/nodes>`_.
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 2
|
||||
:caption: Contents:
|
||||
|
|
|
@ -35,6 +35,9 @@
|
|||
<div class="genindex-jumpbox">
|
||||
<a href="#A"><strong>A</strong></a>
|
||||
| <a href="#C"><strong>C</strong></a>
|
||||
| <a href="#G"><strong>G</strong></a>
|
||||
| <a href="#H"><strong>H</strong></a>
|
||||
| <a href="#I"><strong>I</strong></a>
|
||||
| <a href="#L"><strong>L</strong></a>
|
||||
| <a href="#N"><strong>N</strong></a>
|
||||
| <a href="#P"><strong>P</strong></a>
|
||||
|
@ -62,6 +65,44 @@
|
|||
</ul></td>
|
||||
</tr></table>
|
||||
|
||||
<h2 id="G">G</h2>
|
||||
<table style="width: 100%" class="indextable genindextable"><tr>
|
||||
<td style="width: 33%; vertical-align: top;"><ul>
|
||||
<li><a href="index.html#pynodes.Node.get_child">get_child() (pynodes.Node method)</a>
|
||||
</li>
|
||||
</ul></td>
|
||||
<td style="width: 33%; vertical-align: top;"><ul>
|
||||
<li><a href="index.html#pynodes.Node.get_sibling">get_sibling() (pynodes.Node method)</a>
|
||||
</li>
|
||||
</ul></td>
|
||||
</tr></table>
|
||||
|
||||
<h2 id="H">H</h2>
|
||||
<table style="width: 100%" class="indextable genindextable"><tr>
|
||||
<td style="width: 33%; vertical-align: top;"><ul>
|
||||
<li><a href="index.html#pynodes.Node.has_children">has_children() (pynodes.Node method)</a>
|
||||
</li>
|
||||
</ul></td>
|
||||
<td style="width: 33%; vertical-align: top;"><ul>
|
||||
<li><a href="index.html#pynodes.Node.has_parent">has_parent() (pynodes.Node method)</a>
|
||||
</li>
|
||||
<li><a href="index.html#pynodes.Node.has_siblings">has_siblings() (pynodes.Node method)</a>
|
||||
</li>
|
||||
</ul></td>
|
||||
</tr></table>
|
||||
|
||||
<h2 id="I">I</h2>
|
||||
<table style="width: 100%" class="indextable genindextable"><tr>
|
||||
<td style="width: 33%; vertical-align: top;"><ul>
|
||||
<li><a href="index.html#pynodes.Node.is_child">is_child() (pynodes.Node method)</a>
|
||||
</li>
|
||||
</ul></td>
|
||||
<td style="width: 33%; vertical-align: top;"><ul>
|
||||
<li><a href="index.html#pynodes.Node.is_sibling">is_sibling() (pynodes.Node method)</a>
|
||||
</li>
|
||||
</ul></td>
|
||||
</tr></table>
|
||||
|
||||
<h2 id="L">L</h2>
|
||||
<table style="width: 100%" class="indextable genindextable"><tr>
|
||||
<td style="width: 33%; vertical-align: top;"><ul>
|
||||
|
|
|
@ -37,6 +37,7 @@ A node is an object which is related to other nodes given a tree chart.</p>
|
|||
<div class="highlight-console notranslate"><div class="highlight"><pre><span></span><span class="gp gp-VirtualEnv">(venv)</span> <span class="gp">$ </span>pip<span class="w"> </span>install<span class="w"> </span>pynodes
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>Source code is hosted on my own instance of <a class="reference external" href="https://gitea.lutix.org/nodes">gitea</a>.</p>
|
||||
<div class="toctree-wrapper compound">
|
||||
</div>
|
||||
</section>
|
||||
|
@ -72,6 +73,49 @@ children)</p>
|
|||
<dd><p>Creates random tree of nodes for testing purpose</p>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="py method">
|
||||
<dt class="sig sig-object py" id="pynodes.Node.get_child">
|
||||
<span class="sig-name descname"><span class="pre">get_child</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</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"><a class="reference internal" href="#pynodes.Node" title="pynodes.Node"><span class="pre">Node</span></a><span class="w"> </span><span class="p"><span class="pre">|</span></span><span class="w"> </span><span class="pre">None</span></span></span><a class="reference internal" href="_modules/pynodes.html#Node.get_child"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#pynodes.Node.get_child" title="Link to this definition">¶</a></dt>
|
||||
<dd><p>find and returns a child with specified name. None if nothing found</p>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="py method">
|
||||
<dt class="sig sig-object py" id="pynodes.Node.get_sibling">
|
||||
<span class="sig-name descname"><span class="pre">get_sibling</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</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"><a class="reference internal" href="#pynodes.Node" title="pynodes.Node"><span class="pre">Node</span></a><span class="w"> </span><span class="p"><span class="pre">|</span></span><span class="w"> </span><span class="pre">None</span></span></span><a class="reference internal" href="_modules/pynodes.html#Node.get_sibling"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#pynodes.Node.get_sibling" title="Link to this definition">¶</a></dt>
|
||||
<dd><p>find and returns a sibling with specified name. None if nothing
|
||||
found</p>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="py method">
|
||||
<dt class="sig sig-object py" id="pynodes.Node.has_children">
|
||||
<span class="sig-name descname"><span class="pre">has_children</span></span><span class="sig-paren">(</span><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.has_children"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#pynodes.Node.has_children" title="Link to this definition">¶</a></dt>
|
||||
<dd><p>check if Node object has one child at least</p>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="py method">
|
||||
<dt class="sig sig-object py" id="pynodes.Node.has_parent">
|
||||
<span class="sig-name descname"><span class="pre">has_parent</span></span><span class="sig-paren">(</span><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.has_parent"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#pynodes.Node.has_parent" title="Link to this definition">¶</a></dt>
|
||||
<dd><p>check if Node object has a parent or not</p>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="py method">
|
||||
<dt class="sig sig-object py" id="pynodes.Node.has_siblings">
|
||||
<span class="sig-name descname"><span class="pre">has_siblings</span></span><span class="sig-paren">(</span><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.has_siblings"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#pynodes.Node.has_siblings" title="Link to this definition">¶</a></dt>
|
||||
<dd><p>check if Node object has one sibling at least</p>
|
||||
</dd></dl>
|
||||
|
||||
<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></dl>
|
||||
|
||||
<dl class="py method">
|
||||
<dt class="sig sig-object py" id="pynodes.Node.is_sibling">
|
||||
<span class="sig-name descname"><span class="pre">is_sibling</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_sibling"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#pynodes.Node.is_sibling" title="Link to this definition">¶</a></dt>
|
||||
<dd><p>check if Node object is a sibling of the other Node object</p>
|
||||
</dd></dl>
|
||||
|
||||
<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>
|
||||
|
|
Binary file not shown.
|
@ -1 +1 @@
|
|||
Search.setIndex({"docnames": ["index"], "filenames": ["index.rst"], "titles": ["Welcome to pynodes\u2019s documentation!"], "terms": {"i": 0, "python": 0, "librari": 0, "manag": 0, "node": 0, "class": 0, "A": 0, "an": 0, "object": 0, "which": 0, "relat": 0, "other": 0, "given": 0, "tree": 0, "chart": 0, "venv": 0, "pip": 0, "instal": 0, "import": 0, "name": 0, "str": 0, "parent": 0, "none": 0, "sourc": 0, "should": 0, "subclass": 0, "onli": 0, "add_child": 0, "child": 0, "add": 0, "new": 0, "current": 0, "instanc": 0, "static": 0, "check_lineag": 0, "list": 0, "bool": 0, "check": 0, "straight": 0, "lineag": 0, "1": 0, "ancestor": 0, "2": 0, "3": 0, "n": 0, "grand": 0, "children": 0, "classmethod": 0, "create_random_nod": 0, "type_": 0, "cmd": 0, "depth": 0, "int": 0, "0": 0, "creat": 0, "random": 0, "test": 0, "purpos": 0, "properti": 0, "path": 0, "return": 0, "represent": 0, "self": 0, "pretty_print": 0, "print": 0, "from": 0, "reset_stat": 0, "reset": 0, "all": 0, "classvar": 0, "member": 0, "index": 0, "modul": 0, "search": 0, "page": 0, "level": 0, "sibl": 0}, "objects": {"pynodes": [[0, 0, 1, "", "Node"]], "pynodes.Node": [[0, 1, 1, "", "add_child"], [0, 1, 1, "", "check_lineage"], [0, 1, 1, "", "create_random_nodes"], [0, 2, 1, "", "level"], [0, 2, 1, "", "parents"], [0, 2, 1, "", "path"], [0, 1, 1, "", "pretty_print"], [0, 1, 1, "", "reset_stats"], [0, 2, 1, "", "siblings"]]}, "objtypes": {"0": "py:class", "1": "py:method", "2": "py:property"}, "objnames": {"0": ["py", "class", "Python class"], "1": ["py", "method", "Python method"], "2": ["py", "property", "Python property"]}, "titleterms": {"welcom": 0, "pynod": 0, "": 0, "document": 0, "basic": 0, "usag": 0, "indic": 0, "tabl": 0}, "envversion": {"sphinx.domains.c": 3, "sphinx.domains.changeset": 1, "sphinx.domains.citation": 1, "sphinx.domains.cpp": 9, "sphinx.domains.index": 1, "sphinx.domains.javascript": 3, "sphinx.domains.math": 2, "sphinx.domains.python": 4, "sphinx.domains.rst": 2, "sphinx.domains.std": 2, "sphinx.ext.viewcode": 1, "sphinx": 60}, "alltitles": {"Welcome to pynodes\u2019s documentation!": [[0, "welcome-to-pynodes-s-documentation"]], "Basic Usage": [[0, "basic-usage"]], "Indices and tables": [[0, "indices-and-tables"]]}, "indexentries": {"node (class in pynodes)": [[0, "pynodes.Node"]], "add_child() (pynodes.node method)": [[0, "pynodes.Node.add_child"]], "check_lineage() (pynodes.node static method)": [[0, "pynodes.Node.check_lineage"]], "create_random_nodes() (pynodes.node class method)": [[0, "pynodes.Node.create_random_nodes"]], "level (pynodes.node property)": [[0, "pynodes.Node.level"]], "parents (pynodes.node property)": [[0, "pynodes.Node.parents"]], "path (pynodes.node property)": [[0, "pynodes.Node.path"]], "pretty_print() (pynodes.node method)": [[0, "pynodes.Node.pretty_print"]], "reset_stats() (pynodes.node class method)": [[0, "pynodes.Node.reset_stats"]], "siblings (pynodes.node property)": [[0, "pynodes.Node.siblings"]]}})
|
||||
Search.setIndex({"docnames": ["index"], "filenames": ["index.rst"], "titles": ["Welcome to pynodes\u2019s documentation!"], "terms": {"i": 0, "python": 0, "librari": 0, "manag": 0, "node": 0, "class": 0, "A": 0, "an": 0, "object": 0, "which": 0, "relat": 0, "other": 0, "given": 0, "tree": 0, "chart": 0, "venv": 0, "pip": 0, "instal": 0, "import": 0, "name": 0, "str": 0, "parent": 0, "none": 0, "sourc": 0, "should": 0, "subclass": 0, "onli": 0, "add_child": 0, "child": 0, "add": 0, "new": 0, "current": 0, "instanc": 0, "static": 0, "check_lineag": 0, "list": 0, "bool": 0, "check": 0, "straight": 0, "lineag": 0, "1": 0, "ancestor": 0, "2": 0, "3": 0, "n": 0, "grand": 0, "children": 0, "classmethod": 0, "create_random_nod": 0, "type_": 0, "cmd": 0, "depth": 0, "int": 0, "0": 0, "creat": 0, "random": 0, "test": 0, "purpos": 0, "properti": 0, "path": 0, "return": 0, "represent": 0, "self": 0, "pretty_print": 0, "print": 0, "from": 0, "reset_stat": 0, "reset": 0, "all": 0, "classvar": 0, "member": 0, "index": 0, "modul": 0, "search": 0, "page": 0, "level": 0, "sibl": 0, "get_child": 0, "find": 0, "specifi": 0, "noth": 0, "found": 0, "get_sibl": 0, "has_children": 0, "ha": 0, "one": 0, "least": 0, "has_par": 0, "has_sibl": 0, "is_child": 0, "is_sibl": 0, "code": 0, "host": 0, "my": 0, "own": 0, "gitea": 0, "link": [], "text": []}, "objects": {"pynodes": [[0, 0, 1, "", "Node"]], "pynodes.Node": [[0, 1, 1, "", "add_child"], [0, 1, 1, "", "check_lineage"], [0, 1, 1, "", "create_random_nodes"], [0, 1, 1, "", "get_child"], [0, 1, 1, "", "get_sibling"], [0, 1, 1, "", "has_children"], [0, 1, 1, "", "has_parent"], [0, 1, 1, "", "has_siblings"], [0, 1, 1, "", "is_child"], [0, 1, 1, "", "is_sibling"], [0, 2, 1, "", "level"], [0, 2, 1, "", "parents"], [0, 2, 1, "", "path"], [0, 1, 1, "", "pretty_print"], [0, 1, 1, "", "reset_stats"], [0, 2, 1, "", "siblings"]]}, "objtypes": {"0": "py:class", "1": "py:method", "2": "py:property"}, "objnames": {"0": ["py", "class", "Python class"], "1": ["py", "method", "Python method"], "2": ["py", "property", "Python property"]}, "titleterms": {"welcom": 0, "pynod": 0, "": 0, "document": 0, "basic": 0, "usag": 0, "indic": 0, "tabl": 0}, "envversion": {"sphinx.domains.c": 3, "sphinx.domains.changeset": 1, "sphinx.domains.citation": 1, "sphinx.domains.cpp": 9, "sphinx.domains.index": 1, "sphinx.domains.javascript": 3, "sphinx.domains.math": 2, "sphinx.domains.python": 4, "sphinx.domains.rst": 2, "sphinx.domains.std": 2, "sphinx.ext.viewcode": 1, "sphinx": 60}, "alltitles": {"Welcome to pynodes\u2019s documentation!": [[0, "welcome-to-pynodes-s-documentation"]], "Basic Usage": [[0, "basic-usage"]], "Indices and tables": [[0, "indices-and-tables"]]}, "indexentries": {"node (class in pynodes)": [[0, "pynodes.Node"]], "add_child() (pynodes.node method)": [[0, "pynodes.Node.add_child"]], "check_lineage() (pynodes.node static method)": [[0, "pynodes.Node.check_lineage"]], "create_random_nodes() (pynodes.node class method)": [[0, "pynodes.Node.create_random_nodes"]], "get_child() (pynodes.node method)": [[0, "pynodes.Node.get_child"]], "get_sibling() (pynodes.node method)": [[0, "pynodes.Node.get_sibling"]], "has_children() (pynodes.node method)": [[0, "pynodes.Node.has_children"]], "has_parent() (pynodes.node method)": [[0, "pynodes.Node.has_parent"]], "has_siblings() (pynodes.node method)": [[0, "pynodes.Node.has_siblings"]], "is_child() (pynodes.node method)": [[0, "pynodes.Node.is_child"]], "is_sibling() (pynodes.node method)": [[0, "pynodes.Node.is_sibling"]], "level (pynodes.node property)": [[0, "pynodes.Node.level"]], "parents (pynodes.node property)": [[0, "pynodes.Node.parents"]], "path (pynodes.node property)": [[0, "pynodes.Node.path"]], "pretty_print() (pynodes.node method)": [[0, "pynodes.Node.pretty_print"]], "reset_stats() (pynodes.node class method)": [[0, "pynodes.Node.reset_stats"]], "siblings (pynodes.node property)": [[0, "pynodes.Node.siblings"]]}})
|
|
@ -13,6 +13,8 @@ A node is an object which is related to other nodes given a tree chart.
|
|||
|
||||
(venv) $ pip install pynodes
|
||||
|
||||
Source code is hosted on my own instance of `gitea <https://gitea.lutix.org/nodes>`_.
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 2
|
||||
:caption: Contents:
|
||||
|
|
42
pynodes.py
42
pynodes.py
|
@ -9,7 +9,6 @@ class Node:
|
|||
Should be subclassed only
|
||||
"""
|
||||
# Class Var for statistics
|
||||
total_nodes_number: ClassVar[int] = 0
|
||||
deepest_level: ClassVar[int] = 0
|
||||
largest_sibling_number: ClassVar[int] = 0
|
||||
all_nodes: ClassVar[list[Node]] = []
|
||||
|
@ -18,7 +17,6 @@ class Node:
|
|||
""" each subclass must define its own ClassVar """
|
||||
# TODO to be renamed for clarity
|
||||
super().__init_subclass__()
|
||||
cls.total_nodes_number: ClassVar[int] = 0
|
||||
cls.deepest_level: ClassVar[int] = 0
|
||||
cls.largest_sibling_number: ClassVar[int] = 0
|
||||
cls.all_nodes: ClassVar[list[Node]] = []
|
||||
|
@ -27,7 +25,7 @@ class Node:
|
|||
def __new__(cls, name: str, parent: Node | None = None) -> None:
|
||||
for n in cls.all_nodes:
|
||||
if name == n.name:
|
||||
raise Exception('Node with this name already exist')
|
||||
raise AttributeError('Node with this name already exists')
|
||||
else:
|
||||
return super().__new__(cls)
|
||||
|
||||
|
@ -35,9 +33,21 @@ class Node:
|
|||
self.name = name
|
||||
self.parent = parent # is set with add_child
|
||||
self.children: list[Node] = []
|
||||
type(self).total_nodes_number += 1
|
||||
type(self).all_nodes.append(self)
|
||||
|
||||
def add_child(self, child: Node) -> None:
|
||||
""" add new child node to current instance """
|
||||
child.parent = self
|
||||
if child.name not in [c.name for c in self.children]:
|
||||
self.children.append(child)
|
||||
if child.level > type(self).deepest_level:
|
||||
type(self).deepest_level = child.level
|
||||
if len(self.children) > type(self).largest_sibling_number:
|
||||
type(self).largest_sibling_number = len(self.children)
|
||||
else:
|
||||
raise Exception('Child with same name already exists')
|
||||
|
||||
|
||||
@property
|
||||
def siblings(self) -> list[Node]:
|
||||
""" returns all the siblings of the Node object """
|
||||
|
@ -76,13 +86,14 @@ class Node:
|
|||
return path
|
||||
|
||||
def is_sibling(self, other: str) -> bool:
|
||||
""" test if Node object is sibling with other Node object """
|
||||
""" check if Node object is a sibling of the other Node object """
|
||||
if other in [s.name for s in self.siblings]:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def is_child(self, other: str) -> bool:
|
||||
""" check if Node object is a child of the other Node object """
|
||||
if other in [s.name for s in self.children]:
|
||||
return True
|
||||
else:
|
||||
|
@ -95,41 +106,35 @@ class Node:
|
|||
for c in self.children:
|
||||
c.pretty_print()
|
||||
|
||||
def add_child(self, child: Node) -> None:
|
||||
""" add new child node to current instance """
|
||||
child.parent = self
|
||||
if child.name not in [c.name for c in self.children]:
|
||||
self.children.append(child)
|
||||
if child.level > type(self).deepest_level:
|
||||
type(self).deepest_level = child.level
|
||||
if len(self.children) > type(self).largest_sibling_number:
|
||||
type(self).largest_sibling_number = len(self.children)
|
||||
else:
|
||||
raise Exception('Child with same name already exists')
|
||||
|
||||
def has_parent(self) -> bool:
|
||||
""" check if Node object has a parent or not """
|
||||
if self.parent is not None:
|
||||
return True
|
||||
return False
|
||||
|
||||
def has_children(self) -> bool:
|
||||
""" check if Node object has one child at least """
|
||||
if self.children is not None:
|
||||
return True
|
||||
return False
|
||||
|
||||
def has_siblings(self) -> bool:
|
||||
""" check if Node object has one sibling at least """
|
||||
if self.has_parent() and self.parent.has_children() \
|
||||
and len(self.parent.children) > 0:
|
||||
return True
|
||||
return False
|
||||
|
||||
def get_child(self, name: str) -> Node | None:
|
||||
""" find and returns a child with specified name. None if nothing found """
|
||||
for c in self.children:
|
||||
if c.name == name:
|
||||
return c
|
||||
return None
|
||||
|
||||
def get_sibling(self, name: str) -> Node | None:
|
||||
""" find and returns a sibling with specified name. None if nothing
|
||||
found """
|
||||
for c in self.siblings:
|
||||
if c.name == name:
|
||||
return c
|
||||
|
@ -163,7 +168,6 @@ class Node:
|
|||
"""
|
||||
reset all the ClassVar members
|
||||
"""
|
||||
cls.total_nodes_number = 0
|
||||
cls.deepest_level = 0
|
||||
cls.largest_sibling_number = 0
|
||||
cls.all_nodes = []
|
||||
|
@ -174,7 +178,7 @@ class Node:
|
|||
Creates random tree of nodes for testing purpose
|
||||
"""
|
||||
def create_node(level, i):
|
||||
id_ = cls.total_nodes_number + 1
|
||||
id_ = len(cls.all_nodes) + 1
|
||||
return cls(f'{type_}_'+str(id_))
|
||||
|
||||
def create_node_list(level: int, width: int = 5):
|
||||
|
|
Loading…
Reference in New Issue