Skip to content

Commit

Permalink
Deployed 01fadef with MkDocs version: 1.5.3
Browse files Browse the repository at this point in the history
  • Loading branch information
bitplane committed Sep 24, 2023
1 parent 569e43a commit b4bf983
Show file tree
Hide file tree
Showing 6 changed files with 230 additions and 280 deletions.
162 changes: 79 additions & 83 deletions construction/index.html

Large diffs are not rendered by default.

64 changes: 31 additions & 33 deletions iteration/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -463,48 +463,46 @@

<h1 id="iteration">Iteration</h1>
<p><code>Ranges</code> objects are sequences of integers that you can iterate over.</p>
<div class="codehilite"><pre><span></span><code><span class="kn">from</span> <span class="nn">arranges</span> <span class="kn">import</span> <span class="n">Ranges</span>

<span class="k">assert</span> <span class="nb">list</span><span class="p">(</span><span class="n">Ranges</span><span class="p">(</span><span class="s2">&quot;start:3, 10&quot;</span><span class="p">))</span> <span class="o">==</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">10</span><span class="p">]</span>
</code></pre></div>

<div class="language-python highlight"><pre><span></span><code><span id="__span-0-1"><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="kn">from</span> <span class="nn">arranges</span> <span class="kn">import</span> <span class="n">Ranges</span>
</span><span id="__span-0-2"><a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a>
</span><span id="__span-0-3"><a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a><span class="k">assert</span> <span class="nb">list</span><span class="p">(</span><span class="n">Ranges</span><span class="p">(</span><span class="s2">&quot;start:3, 10&quot;</span><span class="p">))</span> <span class="o">==</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">10</span><span class="p">]</span>
</span></code></pre></div>
<h2 id="cardinality-of-the-address-range">Cardinality of the address range</h2>
<p>The length of a range is the number of elements it contains. But because ranges
can be boundless, we have a special <code>inf</code> value that represents an infinite int
as Python doesn't have or support one.</p>
<p>This value is <code>math.inf</code>, but when returned from <code>len</code> it becomes <code>sys.maxsize</code>
as it only supports ints that are this size or smaller. When comparing <code>inf</code> to
<code>sys.maxsize</code> it'll return <code>True</code>, but this doesn't work in all directions:</p>
<div class="codehilite"><pre><span></span><code><span class="kn">import</span> <span class="nn">math</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">from</span> <span class="nn">arranges</span> <span class="kn">import</span> <span class="n">Ranges</span><span class="p">,</span> <span class="n">inf</span>

<span class="k">assert</span> <span class="nb">len</span><span class="p">(</span><span class="n">Ranges</span><span class="p">(</span><span class="s2">&quot;1,2,3,4,10:20&quot;</span><span class="p">))</span> <span class="o">==</span> <span class="mi">14</span>

<span class="n">full</span> <span class="o">=</span> <span class="n">Ranges</span><span class="p">(</span><span class="s2">&quot;:&quot;</span><span class="p">)</span>

<span class="k">assert</span> <span class="nb">len</span><span class="p">(</span><span class="n">full</span><span class="p">)</span> <span class="o">==</span> <span class="n">sys</span><span class="o">.</span><span class="n">maxsize</span>
<span class="k">assert</span> <span class="nb">len</span><span class="p">(</span><span class="n">full</span><span class="p">)</span> <span class="o">==</span> <span class="n">inf</span>
<span class="k">assert</span> <span class="n">sys</span><span class="o">.</span><span class="n">maxsize</span> <span class="o">==</span> <span class="n">inf</span>
<span class="k">assert</span> <span class="n">full</span><span class="o">.</span><span class="n">stop</span> <span class="o">==</span> <span class="n">inf</span>
<span class="k">assert</span> <span class="n">inf</span> <span class="o">-</span> <span class="mi">100</span> <span class="o">==</span> <span class="n">sys</span><span class="o">.</span><span class="n">maxsize</span>

<span class="c1"># Careful though, this does not hold</span>
<span class="k">assert</span> <span class="nb">len</span><span class="p">(</span><span class="n">full</span><span class="p">)</span> <span class="o">!=</span> <span class="n">math</span><span class="o">.</span><span class="n">inf</span>

<span class="c1"># because it&#39;s an int</span>
<span class="k">assert</span> <span class="nb">type</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">full</span><span class="p">))</span> <span class="ow">is</span> <span class="nb">int</span>
</code></pre></div>

<div class="language-python highlight"><pre><span></span><code><span id="__span-1-1"><a id="__codelineno-1-1" name="__codelineno-1-1" href="#__codelineno-1-1"></a><span class="kn">import</span> <span class="nn">math</span>
</span><span id="__span-1-2"><a id="__codelineno-1-2" name="__codelineno-1-2" href="#__codelineno-1-2"></a><span class="kn">import</span> <span class="nn">sys</span>
</span><span id="__span-1-3"><a id="__codelineno-1-3" name="__codelineno-1-3" href="#__codelineno-1-3"></a><span class="kn">from</span> <span class="nn">arranges</span> <span class="kn">import</span> <span class="n">Ranges</span><span class="p">,</span> <span class="n">inf</span>
</span><span id="__span-1-4"><a id="__codelineno-1-4" name="__codelineno-1-4" href="#__codelineno-1-4"></a>
</span><span id="__span-1-5"><a id="__codelineno-1-5" name="__codelineno-1-5" href="#__codelineno-1-5"></a><span class="k">assert</span> <span class="nb">len</span><span class="p">(</span><span class="n">Ranges</span><span class="p">(</span><span class="s2">&quot;1,2,3,4,10:20&quot;</span><span class="p">))</span> <span class="o">==</span> <span class="mi">14</span>
</span><span id="__span-1-6"><a id="__codelineno-1-6" name="__codelineno-1-6" href="#__codelineno-1-6"></a>
</span><span id="__span-1-7"><a id="__codelineno-1-7" name="__codelineno-1-7" href="#__codelineno-1-7"></a><span class="n">full</span> <span class="o">=</span> <span class="n">Ranges</span><span class="p">(</span><span class="s2">&quot;:&quot;</span><span class="p">)</span>
</span><span id="__span-1-8"><a id="__codelineno-1-8" name="__codelineno-1-8" href="#__codelineno-1-8"></a>
</span><span id="__span-1-9"><a id="__codelineno-1-9" name="__codelineno-1-9" href="#__codelineno-1-9"></a><span class="k">assert</span> <span class="nb">len</span><span class="p">(</span><span class="n">full</span><span class="p">)</span> <span class="o">==</span> <span class="n">sys</span><span class="o">.</span><span class="n">maxsize</span>
</span><span id="__span-1-10"><a id="__codelineno-1-10" name="__codelineno-1-10" href="#__codelineno-1-10"></a><span class="k">assert</span> <span class="nb">len</span><span class="p">(</span><span class="n">full</span><span class="p">)</span> <span class="o">==</span> <span class="n">inf</span>
</span><span id="__span-1-11"><a id="__codelineno-1-11" name="__codelineno-1-11" href="#__codelineno-1-11"></a><span class="k">assert</span> <span class="n">sys</span><span class="o">.</span><span class="n">maxsize</span> <span class="o">==</span> <span class="n">inf</span>
</span><span id="__span-1-12"><a id="__codelineno-1-12" name="__codelineno-1-12" href="#__codelineno-1-12"></a><span class="k">assert</span> <span class="n">full</span><span class="o">.</span><span class="n">stop</span> <span class="o">==</span> <span class="n">inf</span>
</span><span id="__span-1-13"><a id="__codelineno-1-13" name="__codelineno-1-13" href="#__codelineno-1-13"></a><span class="k">assert</span> <span class="n">inf</span> <span class="o">-</span> <span class="mi">100</span> <span class="o">==</span> <span class="n">sys</span><span class="o">.</span><span class="n">maxsize</span>
</span><span id="__span-1-14"><a id="__codelineno-1-14" name="__codelineno-1-14" href="#__codelineno-1-14"></a>
</span><span id="__span-1-15"><a id="__codelineno-1-15" name="__codelineno-1-15" href="#__codelineno-1-15"></a><span class="c1"># Careful though, this does not hold</span>
</span><span id="__span-1-16"><a id="__codelineno-1-16" name="__codelineno-1-16" href="#__codelineno-1-16"></a><span class="k">assert</span> <span class="nb">len</span><span class="p">(</span><span class="n">full</span><span class="p">)</span> <span class="o">!=</span> <span class="n">math</span><span class="o">.</span><span class="n">inf</span>
</span><span id="__span-1-17"><a id="__codelineno-1-17" name="__codelineno-1-17" href="#__codelineno-1-17"></a>
</span><span id="__span-1-18"><a id="__codelineno-1-18" name="__codelineno-1-18" href="#__codelineno-1-18"></a><span class="c1"># because it&#39;s an int</span>
</span><span id="__span-1-19"><a id="__codelineno-1-19" name="__codelineno-1-19" href="#__codelineno-1-19"></a><span class="k">assert</span> <span class="nb">type</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">full</span><span class="p">))</span> <span class="ow">is</span> <span class="nb">int</span>
</span></code></pre></div>
<h2 id="truthyness">Truthyness</h2>
<p>Empty ranges are, of course, Falsey.</p>
<div class="codehilite"><pre><span></span><code><span class="kn">from</span> <span class="nn">arranges</span> <span class="kn">import</span> <span class="n">Ranges</span>

<span class="k">assert</span> <span class="n">Ranges</span><span class="p">(</span><span class="s2">&quot;:&quot;</span><span class="p">)</span>
<span class="k">assert</span> <span class="n">Range</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>

<span class="k">assert</span> <span class="ow">not</span> <span class="n">Ranges</span><span class="p">(</span><span class="s2">&quot;&quot;</span><span class="p">)</span>
</code></pre></div>
<div class="language-python highlight"><pre><span></span><code><span id="__span-2-1"><a id="__codelineno-2-1" name="__codelineno-2-1" href="#__codelineno-2-1"></a><span class="kn">from</span> <span class="nn">arranges</span> <span class="kn">import</span> <span class="n">Ranges</span>
</span><span id="__span-2-2"><a id="__codelineno-2-2" name="__codelineno-2-2" href="#__codelineno-2-2"></a>
</span><span id="__span-2-3"><a id="__codelineno-2-3" name="__codelineno-2-3" href="#__codelineno-2-3"></a><span class="k">assert</span> <span class="n">Ranges</span><span class="p">(</span><span class="s2">&quot;:&quot;</span><span class="p">)</span>
</span><span id="__span-2-4"><a id="__codelineno-2-4" name="__codelineno-2-4" href="#__codelineno-2-4"></a><span class="k">assert</span> <span class="n">Range</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
</span><span id="__span-2-5"><a id="__codelineno-2-5" name="__codelineno-2-5" href="#__codelineno-2-5"></a>
</span><span id="__span-2-6"><a id="__codelineno-2-6" name="__codelineno-2-6" href="#__codelineno-2-6"></a><span class="k">assert</span> <span class="ow">not</span> <span class="n">Ranges</span><span class="p">(</span><span class="s2">&quot;&quot;</span><span class="p">)</span>
</span></code></pre></div>



Expand Down
36 changes: 18 additions & 18 deletions models/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -397,24 +397,24 @@

<h1 id="use-in-pydantic-models">Use in Pydantic models</h1>
<p>You'll need Pydantic &gt;=2.3:</p>
<div class="codehilite"><pre><span></span><code><span class="kn">from</span> <span class="nn">pydantic</span> <span class="kn">import</span> <span class="n">BaseModel</span><span class="p">,</span> <span class="n">Field</span>


<span class="k">class</span> <span class="nc">BinaryPatch</span><span class="p">(</span><span class="n">BaseModel</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>

<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">regions</span><span class="p">:</span> <span class="n">Ranges</span>


<span class="n">model</span> <span class="o">=</span> <span class="n">BinaryPatch</span><span class="o">.</span><span class="n">model_validate_json</span><span class="p">(</span><span class="s2">&quot;&quot;&quot;</span>
<span class="s2">{</span>
<span class="s2"> &quot;regions&quot;: &quot;0:10, 20:30, 15:, 0x0b&quot;</span>
<span class="s2">}</span>
<span class="s2">&quot;&quot;&quot;</span><span class="p">)</span>

<span class="k">assert</span> <span class="n">model</span><span class="o">.</span><span class="n">regions</span> <span class="o">==</span> <span class="s2">&quot;:10,15:&quot;</span>
</code></pre></div>
<div class="language-python highlight"><pre><span></span><code><span id="__span-0-1"><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="kn">from</span> <span class="nn">pydantic</span> <span class="kn">import</span> <span class="n">BaseModel</span><span class="p">,</span> <span class="n">Field</span>
</span><span id="__span-0-2"><a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a>
</span><span id="__span-0-3"><a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a>
</span><span id="__span-0-4"><a id="__codelineno-0-4" name="__codelineno-0-4" href="#__codelineno-0-4"></a><span class="k">class</span> <span class="nc">BinaryPatch</span><span class="p">(</span><span class="n">BaseModel</span><span class="p">):</span>
</span><span id="__span-0-5"><a id="__codelineno-0-5" name="__codelineno-0-5" href="#__codelineno-0-5"></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
</span><span id="__span-0-6"><a id="__codelineno-0-6" name="__codelineno-0-6" href="#__codelineno-0-6"></a>
</span><span id="__span-0-7"><a id="__codelineno-0-7" name="__codelineno-0-7" href="#__codelineno-0-7"></a><span class="sd"> &quot;&quot;&quot;</span>
</span><span id="__span-0-8"><a id="__codelineno-0-8" name="__codelineno-0-8" href="#__codelineno-0-8"></a> <span class="n">regions</span><span class="p">:</span> <span class="n">Ranges</span>
</span><span id="__span-0-9"><a id="__codelineno-0-9" name="__codelineno-0-9" href="#__codelineno-0-9"></a>
</span><span id="__span-0-10"><a id="__codelineno-0-10" name="__codelineno-0-10" href="#__codelineno-0-10"></a>
</span><span id="__span-0-11"><a id="__codelineno-0-11" name="__codelineno-0-11" href="#__codelineno-0-11"></a><span class="n">model</span> <span class="o">=</span> <span class="n">BinaryPatch</span><span class="o">.</span><span class="n">model_validate_json</span><span class="p">(</span><span class="s2">&quot;&quot;&quot;</span>
</span><span id="__span-0-12"><a id="__codelineno-0-12" name="__codelineno-0-12" href="#__codelineno-0-12"></a><span class="s2">{</span>
</span><span id="__span-0-13"><a id="__codelineno-0-13" name="__codelineno-0-13" href="#__codelineno-0-13"></a><span class="s2"> &quot;regions&quot;: &quot;0:10, 20:30, 15:, 0x0b&quot;</span>
</span><span id="__span-0-14"><a id="__codelineno-0-14" name="__codelineno-0-14" href="#__codelineno-0-14"></a><span class="s2">}</span>
</span><span id="__span-0-15"><a id="__codelineno-0-15" name="__codelineno-0-15" href="#__codelineno-0-15"></a><span class="s2">&quot;&quot;&quot;</span><span class="p">)</span>
</span><span id="__span-0-16"><a id="__codelineno-0-16" name="__codelineno-0-16" href="#__codelineno-0-16"></a>
</span><span id="__span-0-17"><a id="__codelineno-0-17" name="__codelineno-0-17" href="#__codelineno-0-17"></a><span class="k">assert</span> <span class="n">model</span><span class="o">.</span><span class="n">regions</span> <span class="o">==</span> <span class="s2">&quot;:10,15:&quot;</span>
</span></code></pre></div>



Expand Down
Loading

0 comments on commit b4bf983

Please sign in to comment.