Skip to content

Commit

Permalink
Deployed ac79ec8 with MkDocs version: 1.5.3
Browse files Browse the repository at this point in the history
  • Loading branch information
redjax committed Apr 23, 2024
1 parent 4dbcedd commit 0c192e8
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
91 changes: 91 additions & 0 deletions programming/python/dataclasses.html
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,15 @@
</ul>
</nav>

</li>

<li class="md-nav__item">
<a href="#validating-a-dataclass" class="md-nav__link">
<span class="md-ellipsis">
Validating a Dataclass
</span>
</a>

</li>

<li class="md-nav__item">
Expand Down Expand Up @@ -2090,6 +2099,15 @@
</ul>
</nav>

</li>

<li class="md-nav__item">
<a href="#validating-a-dataclass" class="md-nav__link">
<span class="md-ellipsis">
Validating a Dataclass
</span>
</a>

</li>

<li class="md-nav__item">
Expand Down Expand Up @@ -2347,6 +2365,79 @@ <h3 id="jsonencodemixin">JSONEncodeMixin</h3>
</span><span id="__span-3-12"><a id="__codelineno-3-12" name="__codelineno-3-12"></a><span class="n">person</span> <span class="o">=</span> <span class="n">Person</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">&quot;Alice&quot;</span><span class="p">,</span> <span class="n">age</span><span class="o">=</span><span class="mi">25</span><span class="p">)</span>
</span><span id="__span-3-13"><a id="__codelineno-3-13" name="__codelineno-3-13"></a><span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">person</span><span class="p">,</span> <span class="bp">cls</span><span class="o">=</span><span class="n">DataclassEncoder</span><span class="p">)</span> <span class="c1"># Returns &#39;{&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25}&#39;</span>
</span></code></pre></div></td></tr></table></div>
<h2 id="validating-a-dataclass">Validating a Dataclass</h2>
<p>Python dataclasses do not have built-in validation, like a <code>Pydantic</code> class. You can still use type hints to define variables, like <code>name: str = None</code>, but it has no actual effect on the dataclass.</p>
<p>You can use the <code>__post_init__(self)</code> method of a dataclass to perform data validation. A few examples below:</p>
<div class="language-py highlight"><table class="highlighttable"><tr><th colspan="2" class="filename"><span class="filename">Dataclass validation</span></th></tr><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"><a href="#__codelineno-4-1"> 1</a></span>
<span class="normal"><a href="#__codelineno-4-2"> 2</a></span>
<span class="normal"><a href="#__codelineno-4-3"> 3</a></span>
<span class="normal"><a href="#__codelineno-4-4"> 4</a></span>
<span class="normal"><a href="#__codelineno-4-5"> 5</a></span>
<span class="normal"><a href="#__codelineno-4-6"> 6</a></span>
<span class="normal"><a href="#__codelineno-4-7"> 7</a></span>
<span class="normal"><a href="#__codelineno-4-8"> 8</a></span>
<span class="normal"><a href="#__codelineno-4-9"> 9</a></span>
<span class="normal"><a href="#__codelineno-4-10">10</a></span>
<span class="normal"><a href="#__codelineno-4-11">11</a></span>
<span class="normal"><a href="#__codelineno-4-12">12</a></span>
<span class="normal"><a href="#__codelineno-4-13">13</a></span>
<span class="normal"><a href="#__codelineno-4-14">14</a></span>
<span class="normal"><a href="#__codelineno-4-15">15</a></span>
<span class="normal"><a href="#__codelineno-4-16">16</a></span>
<span class="normal"><a href="#__codelineno-4-17">17</a></span>
<span class="normal"><a href="#__codelineno-4-18">18</a></span>
<span class="normal"><a href="#__codelineno-4-19">19</a></span>
<span class="normal"><a href="#__codelineno-4-20">20</a></span>
<span class="normal"><a href="#__codelineno-4-21">21</a></span>
<span class="normal"><a href="#__codelineno-4-22">22</a></span>
<span class="normal"><a href="#__codelineno-4-23">23</a></span>
<span class="normal"><a href="#__codelineno-4-24">24</a></span>
<span class="normal"><a href="#__codelineno-4-25">25</a></span>
<span class="normal"><a href="#__codelineno-4-26">26</a></span>
<span class="normal"><a href="#__codelineno-4-27">27</a></span>
<span class="normal"><a href="#__codelineno-4-28">28</a></span>
<span class="normal"><a href="#__codelineno-4-29">29</a></span>
<span class="normal"><a href="#__codelineno-4-30">30</a></span>
<span class="normal"><a href="#__codelineno-4-31">31</a></span>
<span class="normal"><a href="#__codelineno-4-32">32</a></span>
<span class="normal"><a href="#__codelineno-4-33">33</a></span>
<span class="normal"><a href="#__codelineno-4-34">34</a></span>
<span class="normal"><a href="#__codelineno-4-35">35</a></span></pre></div></td><td class="code"><div><pre><span></span><code><span id="__span-4-1"><a id="__codelineno-4-1" name="__codelineno-4-1"></a><span class="kn">from</span> <span class="nn">dataclasses</span> <span class="kn">import</span> <span class="n">dataclass</span>
</span><span id="__span-4-2"><a id="__codelineno-4-2" name="__codelineno-4-2"></a><span class="kn">import</span> <span class="nn">typing</span> <span class="k">as</span> <span class="nn">t</span>
</span><span id="__span-4-3"><a id="__codelineno-4-3" name="__codelineno-4-3"></a><span class="kn">from</span> <span class="nn">pathlib</span> <span class="kn">import</span> <span class="n">Path</span>
</span><span id="__span-4-4"><a id="__codelineno-4-4" name="__codelineno-4-4"></a>
</span><span id="__span-4-5"><a id="__codelineno-4-5" name="__codelineno-4-5"></a>
</span><span id="__span-4-6"><a id="__codelineno-4-6" name="__codelineno-4-6"></a><span class="k">def</span> <span class="nf">validate_path</span><span class="p">(</span><span class="n">p</span><span class="p">:</span> <span class="n">t</span><span class="o">.</span><span class="n">Union</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">Path</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Path</span><span class="p">:</span>
</span><span id="__span-4-7"><a id="__codelineno-4-7" name="__codelineno-4-7"></a> <span class="k">assert</span> <span class="n">p</span><span class="p">,</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Missing an input path to validate.&quot;</span><span class="p">)</span>
</span><span id="__span-4-8"><a id="__codelineno-4-8" name="__codelineno-4-8"></a> <span class="k">assert</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="nb">str</span><span class="p">)</span> <span class="ow">or</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">Path</span><span class="p">),</span> <span class="ne">TypeError</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;p must be a str or Path. Got type: (</span><span class="si">{</span><span class="nb">type</span><span class="p">(</span><span class="n">p</span><span class="p">)</span><span class="si">}</span><span class="s2">)&quot;</span><span class="p">)</span>
</span><span id="__span-4-9"><a id="__codelineno-4-9" name="__codelineno-4-9"></a>
</span><span id="__span-4-10"><a id="__codelineno-4-10" name="__codelineno-4-10"></a> <span class="n">p</span><span class="p">:</span> <span class="n">Path</span> <span class="o">=</span> <span class="n">Path</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">p</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>
</span><span id="__span-4-11"><a id="__codelineno-4-11" name="__codelineno-4-11"></a> <span class="k">if</span> <span class="s2">&quot;~&quot;</span> <span class="ow">in</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">p</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">:</span>
</span><span id="__span-4-12"><a id="__codelineno-4-12" name="__codelineno-4-12"></a> <span class="n">p</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">expanduser</span><span class="p">()</span>
</span><span id="__span-4-13"><a id="__codelineno-4-13" name="__codelineno-4-13"></a>
</span><span id="__span-4-14"><a id="__codelineno-4-14" name="__codelineno-4-14"></a> <span class="k">return</span> <span class="n">p</span>
</span><span id="__span-4-15"><a id="__codelineno-4-15" name="__codelineno-4-15"></a>
</span><span id="__span-4-16"><a id="__codelineno-4-16" name="__codelineno-4-16"></a>
</span><span id="__span-4-17"><a id="__codelineno-4-17" name="__codelineno-4-17"></a><span class="nd">@dataclass</span>
</span><span id="__span-4-18"><a id="__codelineno-4-18" name="__codelineno-4-18"></a><span class="k">class</span> <span class="nc">ComputerDirectory</span><span class="p">:</span>
</span><span id="__span-4-19"><a id="__codelineno-4-19" name="__codelineno-4-19"></a> <span class="c1">## Use | None in the annotation to denote an optional value</span>
</span><span id="__span-4-20"><a id="__codelineno-4-20" name="__codelineno-4-20"></a> <span class="n">dir_name</span><span class="p">:</span> <span class="nb">str</span> <span class="o">|</span> <span class="kc">None</span> <span class="o">=</span> <span class="kc">None</span>
</span><span id="__span-4-21"><a id="__codelineno-4-21" name="__codelineno-4-21"></a> <span class="n">dir_path</span><span class="p">:</span> <span class="n">t</span><span class="o">.</span><span class="n">Union</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">Path</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>
</span><span id="__span-4-22"><a id="__codelineno-4-22" name="__codelineno-4-22"></a>
</span><span id="__span-4-23"><a id="__codelineno-4-23" name="__codelineno-4-23"></a> <span class="k">def</span> <span class="nf">__post_init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
</span><span id="__span-4-24"><a id="__codelineno-4-24" name="__codelineno-4-24"></a> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">dir_name</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
</span><span id="__span-4-25"><a id="__codelineno-4-25" name="__codelineno-4-25"></a> <span class="c1">## self.dir_name is allowed to be None</span>
</span><span id="__span-4-26"><a id="__codelineno-4-26" name="__codelineno-4-26"></a> <span class="k">pass</span>
</span><span id="__span-4-27"><a id="__codelineno-4-27" name="__codelineno-4-27"></a> <span class="k">else</span><span class="p">:</span>
</span><span id="__span-4-28"><a id="__codelineno-4-28" name="__codelineno-4-28"></a> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">dir_name</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
</span><span id="__span-4-29"><a id="__codelineno-4-29" name="__codelineno-4-29"></a> <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;dir_name should be a string.&quot;</span><span class="p">)</span>
</span><span id="__span-4-30"><a id="__codelineno-4-30" name="__codelineno-4-30"></a>
</span><span id="__span-4-31"><a id="__codelineno-4-31" name="__codelineno-4-31"></a> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">dir_path</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
</span><span id="__span-4-32"><a id="__codelineno-4-32" name="__codelineno-4-32"></a> <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Missing required parameter: dir_path&quot;</span><span class="p">)</span>
</span><span id="__span-4-33"><a id="__codelineno-4-33" name="__codelineno-4-33"></a> <span class="k">else</span><span class="p">:</span>
</span><span id="__span-4-34"><a id="__codelineno-4-34" name="__codelineno-4-34"></a> <span class="c1">## Validate self.dir_path with the validate_path() function</span>
</span><span id="__span-4-35"><a id="__codelineno-4-35" name="__codelineno-4-35"></a> <span class="bp">self</span><span class="o">.</span><span class="n">dir_path</span> <span class="o">=</span> <span class="n">validate_path</span><span class="p">(</span><span class="n">p</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">dir_path</span><span class="p">)</span>
</span></code></pre></div></td></tr></table></div>
<h2 id="links-extra-reading">Links &amp; Extra Reading</h2>
<ul>
<li><a href="https://docs.python.org/3/library/dataclasses.html">Python docs: dataclasses</a></li>
Expand Down
2 changes: 1 addition & 1 deletion search/search_index.json

Large diffs are not rendered by default.

Binary file modified sitemap.xml.gz
Binary file not shown.

0 comments on commit 0c192e8

Please sign in to comment.