361 lines
36 KiB
HTML
361 lines
36 KiB
HTML
<!DOCTYPE html>
|
|
|
|
<html lang="en" data-content_root="../">
|
|
<head>
|
|
<meta charset="utf-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
<title>pynodes — pynodes 0.1 documentation</title>
|
|
<link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=d1102ebc" />
|
|
<link rel="stylesheet" type="text/css" href="../_static/alabaster.css?v=12dfc556" />
|
|
<script src="../_static/documentation_options.js?v=2709fde1"></script>
|
|
<script src="../_static/doctools.js?v=888ff710"></script>
|
|
<script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
|
|
<link rel="index" title="Index" href="../genindex.html" />
|
|
<link rel="search" title="Search" href="../search.html" />
|
|
|
|
<link rel="stylesheet" href="../_static/custom.css" type="text/css" />
|
|
|
|
|
|
|
|
|
|
|
|
</head><body>
|
|
|
|
|
|
<div class="document">
|
|
<div class="documentwrapper">
|
|
<div class="bodywrapper">
|
|
|
|
|
|
<div class="body" role="main">
|
|
|
|
<h1>Source code for pynodes</h1><div class="highlight"><pre>
|
|
<span></span><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">annotations</span> <span class="c1"># for Class forward reference</span>
|
|
<span class="kn">import</span> <span class="nn">random</span>
|
|
|
|
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">ClassVar</span>
|
|
|
|
|
|
<div class="viewcode-block" id="Node">
|
|
<a class="viewcode-back" href="../index.html#pynodes.Node">[docs]</a>
|
|
<span class="k">class</span> <span class="nc">Node</span><span class="p">:</span>
|
|
<span class="w"> </span><span class="sd">"""</span>
|
|
<span class="sd"> Should be subclassed only</span>
|
|
<span class="sd"> """</span>
|
|
<span class="c1"># Class Var for statistics</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>
|
|
|
|
<span class="k">def</span> <span class="nf">__init_subclass__</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
|
|
<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">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>
|
|
<span class="k">return</span> <span class="bp">cls</span>
|
|
|
|
<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">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>
|
|
|
|
<span class="k">def</span> <span class="fm">__init__</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="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="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">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>
|
|
<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="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>
|
|
<span class="n">p</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">parent</span>
|
|
<span class="n">parents</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
|
|
<span class="k">return</span> <span class="n">parents</span>
|
|
|
|
<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="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>
|
|
<span class="n">level</span> <span class="o">+=</span> <span class="mi">1</span>
|
|
<span class="n">p</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">parent</span>
|
|
<span class="k">return</span> <span class="n">level</span>
|
|
|
|
<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"> """</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>
|
|
<span class="n">path</span> <span class="o">+=</span> <span class="n">p</span><span class="o">.</span><span class="n">name</span><span class="o">+</span><span class="s1">'.'</span>
|
|
<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></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></div>
|
|
|
|
|
|
<div class="viewcode-block" id="Node.pretty_print">
|
|
<a class="viewcode-back" href="../index.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="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="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="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s1">'</span><span class="si">{</span><span class="n">dashes</span><span class="si">}{</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="si">}</span><span class="s1">'</span><span class="p">)</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="n">c</span><span class="o">.</span><span class="n">pretty_print</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></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></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></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></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></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>
|
|
<span class="n">results</span> <span class="o">=</span> <span class="p">[]</span>
|
|
<span class="k">if</span> <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="p">:</span>
|
|
<span class="n">results</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="p">)</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="n">results</span> <span class="o">+=</span> <span class="n">c</span><span class="o">.</span><span class="n">get_children</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
|
|
<span class="k">return</span> <span class="n">results</span>
|
|
|
|
<div class="viewcode-block" id="Node.check_lineage">
|
|
<a class="viewcode-back" href="../index.html#pynodes.Node.check_lineage">[docs]</a>
|
|
<span class="nd">@staticmethod</span>
|
|
<span class="k">def</span> <span class="nf">check_lineage</span><span class="p">(</span><span class="n">nodes</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="nb">bool</span><span class="p">:</span>
|
|
<span class="w"> </span><span class="sd">"""</span>
|
|
<span class="sd"> check if the list of nodes is a straight lineage:</span>
|
|
<span class="sd"> node 1 (ancestor) -> node 2 -> node 3 -> ... -> node n (grand</span>
|
|
<span class="sd"> children)</span>
|
|
|
|
<span class="sd"> """</span>
|
|
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">nodes</span><span class="p">)):</span>
|
|
<span class="k">if</span> <span class="n">nodes</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">parent</span> <span class="o">!=</span> <span class="n">nodes</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">]:</span>
|
|
<span class="k">return</span> <span class="kc">False</span>
|
|
|
|
<span class="k">return</span> <span class="kc">True</span></div>
|
|
|
|
|
|
<div class="viewcode-block" id="Node.reset_stats">
|
|
<a class="viewcode-back" href="../index.html#pynodes.Node.reset_stats">[docs]</a>
|
|
<span class="nd">@classmethod</span>
|
|
<span class="k">def</span> <span class="nf">reset_stats</span><span class="p">(</span><span class="bp">cls</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"> reset all the ClassVar members</span>
|
|
<span class="sd"> """</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>
|
|
|
|
|
|
<div class="viewcode-block" id="Node.create_random_nodes">
|
|
<a class="viewcode-back" href="../index.html#pynodes.Node.create_random_nodes">[docs]</a>
|
|
<span class="nd">@classmethod</span>
|
|
<span class="k">def</span> <span class="nf">create_random_nodes</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">type_</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s1">'cmd'</span><span class="p">,</span> <span class="n">depth</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">0</span><span class="p">)</span> <span class="o">-></span> <span class="n">Node</span><span class="p">:</span>
|
|
<span class="w"> </span><span class="sd">"""</span>
|
|
<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="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>
|
|
<span class="k">return</span> <span class="p">[</span><span class="n">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="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">width</span><span class="p">))]</span>
|
|
|
|
<span class="k">def</span> <span class="nf">create_arg_tree</span><span class="p">(</span><span class="n">arg</span><span class="p">:</span> <span class="bp">cls</span><span class="p">):</span>
|
|
<span class="k">if</span> <span class="n">arg</span><span class="o">.</span><span class="n">level</span> <span class="o"><</span> <span class="n">depth</span><span class="p">:</span>
|
|
<span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">create_node_list</span><span class="p">(</span><span class="n">arg</span><span class="o">.</span><span class="n">level</span><span class="p">):</span>
|
|
<span class="n">arg</span><span class="o">.</span><span class="n">add_child</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
|
|
<span class="n">create_arg_tree</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
|
|
<span class="k">return</span> <span class="n">arg</span>
|
|
|
|
<span class="n">arg</span> <span class="o">=</span> <span class="bp">cls</span><span class="p">(</span><span class="s1">'parser'</span><span class="p">)</span>
|
|
|
|
<span class="k">return</span> <span class="n">create_arg_tree</span><span class="p">(</span><span class="n">arg</span><span class="p">)</span></div>
|
|
|
|
|
|
<span class="k">def</span> <span class="fm">__lt__</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="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">level</span> <span class="o"><</span> <span class="n">other</span><span class="o">.</span><span class="n">level</span>
|
|
|
|
<span class="k">def</span> <span class="fm">__gt__</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="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">level</span> <span class="o">></span> <span class="n">other</span><span class="o">.</span><span class="n">level</span>
|
|
|
|
<span class="k">def</span> <span class="fm">__le__</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="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">level</span> <span class="o"><=</span> <span class="n">other</span><span class="o">.</span><span class="n">level</span>
|
|
|
|
<span class="k">def</span> <span class="fm">__ge__</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="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">level</span> <span class="o">>=</span> <span class="n">other</span><span class="o">.</span><span class="n">level</span>
|
|
|
|
<span class="k">def</span> <span class="fm">__str__</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="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span></div>
|
|
|
|
|
|
|
|
<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s2">"__main__"</span><span class="p">:</span>
|
|
|
|
<span class="c1"># while True:</span>
|
|
<span class="c1"># master_node = Node.create_random_nodes(depth=3, type_='cmd')</span>
|
|
<span class="c1"># node_list = [master_node, master_node.children[0],</span>
|
|
<span class="c1"># master_node.children[0].children[0]]</span>
|
|
<span class="c1"># random.shuffle(node_list)</span>
|
|
<span class="c1"># print([n.name for n in node_list])</span>
|
|
<span class="c1"># print(Node.check_lineage(node_list))</span>
|
|
<span class="c1"># input('pause')</span>
|
|
<span class="n">n0</span> <span class="o">=</span> <span class="n">Node</span><span class="p">(</span><span class="s1">'name0'</span><span class="p">)</span>
|
|
<span class="n">n1</span> <span class="o">=</span> <span class="n">Node</span><span class="p">(</span><span class="s1">'name0'</span><span class="p">)</span>
|
|
</pre></div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
|
|
<div class="sphinxsidebarwrapper">
|
|
<h1 class="logo"><a href="../index.html">pynodes</a></h1>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<h3>Navigation</h3>
|
|
|
|
<div class="relations">
|
|
<h3>Related Topics</h3>
|
|
<ul>
|
|
<li><a href="../index.html">Documentation overview</a><ul>
|
|
<li><a href="index.html">Module code</a><ul>
|
|
</ul></li>
|
|
</ul></li>
|
|
</ul>
|
|
</div>
|
|
<div id="searchbox" style="display: none" role="search">
|
|
<h3 id="searchlabel">Quick search</h3>
|
|
<div class="searchformwrapper">
|
|
<form class="search" action="../search.html" method="get">
|
|
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
|
|
<input type="submit" value="Go" />
|
|
</form>
|
|
</div>
|
|
</div>
|
|
<script>document.getElementById('searchbox').style.display = "block"</script>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</div>
|
|
</div>
|
|
<div class="clearer"></div>
|
|
</div>
|
|
<div class="footer">
|
|
©2024, fabthegreat.
|
|
|
|
|
|
|
Powered by <a href="https://www.sphinx-doc.org/">Sphinx 7.2.6</a>
|
|
& <a href="https://alabaster.readthedocs.io">Alabaster 0.7.16</a>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
</body>
|
|
</html> |