Browse Source

Format, Class, Enum, Exception

pull/115/head
Jure Šorn 3 years ago
parent
commit
2be9c2df86
3 changed files with 23 additions and 23 deletions
  1. 20
      README.md
  2. 24
      index.html
  3. 2
      web/faq.html

20
README.md

@ -425,12 +425,12 @@ Format
### Numbers
```python
{ 123456:10} # ' 123456'
{ 123456:10,} # ' 123,456'
{ 123456:10_} # ' 123_456'
{ 123456:+10} # ' +123456'
{-123456:=10} # '- 123456'
{ 123456: } # ' 123456'
{123456:10} # ' 123456'
{123456:10,} # ' 123,456'
{123456:10_} # ' 123_456'
{123456:+10} # ' +123456'
{123456:=+10} # '+ 123456'
{123456: } # ' 123456'
{-123456: } # '-123456'
```
@ -1031,6 +1031,7 @@ class <class_name>:
<attr_name_2>: <type> = <default_value>
<attr_name_3>: list/dict/set = field(default_factory=list/dict/set)
```
* **For arbitrary type use `'typing.Any'`.**
* **Objects can be made sortable with `'order=True'` and immutable with `'frozen=True'`.**
* **For object to be hashable, all attributes must be hashable and frozen must be True.**
* **Function field() is needed because `'<attr_name>: list = []'` would make a list that is shared among all instances.**
@ -1333,9 +1334,9 @@ Cutlery = Enum('Cutlery', {'fork': 1, 'knife': 2, 'spoon': 3})
```python
from functools import partial
LogicOp = Enum('LogicOp', {'AND': partial(lambda l, r: l and r),
'OR' : partial(lambda l, r: l or r)})
'OR': partial(lambda l, r: l or r)})
```
* **Another solution in this particular case is to use functions and\_() and or\_() from the module [operator](#operator).**
* **Member names are in all caps because trying to access a member that is named after a reserved keyword raises SyntaxError.**
Exceptions
@ -1362,7 +1363,7 @@ finally:
<code_3>
```
* **Code inside the `'else'` block will only be executed if `'try'` block had no exceptions.**
* **Code inside the `'finally'` block will always be executed.**
* **Code inside the `'finally'` block will always be executed (unless a signal is received).**
### Catching Exceptions
```python
@ -2131,7 +2132,6 @@ sorted_by_second = sorted(<collection>, key=op.itemgetter(1))
sorted_by_both = sorted(<collection>, key=op.itemgetter(1, 0))
product_of_elems = functools.reduce(op.mul, <collection>)
union_of_sets = functools.reduce(op.or_, <coll_of_sets>)
LogicOp = enum.Enum('LogicOp', {'AND': op.and_, 'OR': op.or_})
last_el = op.methodcaller('pop')(<list>)
```

24
index.html

@ -226,7 +226,7 @@ pre.prettyprint {
<body>
<header>
<aside>October 4, 2021</aside>
<aside>October 7, 2021</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</header>
@ -546,12 +546,12 @@ to_exclusive = &lt;range&gt;.stop
{<span class="hljs-string">'abcde'</span>!r:<span class="hljs-number">10</span>} <span class="hljs-comment"># "'abcde' "</span>
</code></pre></div>
<div><h3 id="numbers-1">Numbers</h3><pre><code class="python language-python hljs">{ <span class="hljs-number">123456</span>:<span class="hljs-number">10</span>} <span class="hljs-comment"># ' 123456'</span>
{ <span class="hljs-number">123456</span>:<span class="hljs-number">10</span>,} <span class="hljs-comment"># ' 123,456'</span>
{ <span class="hljs-number">123456</span>:<span class="hljs-number">10</span>_} <span class="hljs-comment"># ' 123_456'</span>
{ <span class="hljs-number">123456</span>:+<span class="hljs-number">10</span>} <span class="hljs-comment"># ' +123456'</span>
{<span class="hljs-number">-123456</span>:=<span class="hljs-number">10</span>} <span class="hljs-comment"># '- 123456'</span>
{ <span class="hljs-number">123456</span>: } <span class="hljs-comment"># ' 123456'</span>
<div><h3 id="numbers-1">Numbers</h3><pre><code class="python language-python hljs">{<span class="hljs-number">123456</span>:<span class="hljs-number">10</span>} <span class="hljs-comment"># ' 123456'</span>
{<span class="hljs-number">123456</span>:<span class="hljs-number">10</span>,} <span class="hljs-comment"># ' 123,456'</span>
{<span class="hljs-number">123456</span>:<span class="hljs-number">10</span>_} <span class="hljs-comment"># ' 123_456'</span>
{<span class="hljs-number">123456</span>:+<span class="hljs-number">10</span>} <span class="hljs-comment"># ' +123456'</span>
{<span class="hljs-number">123456</span>:=+<span class="hljs-number">10</span>} <span class="hljs-comment"># '+ 123456'</span>
{<span class="hljs-number">123456</span>: } <span class="hljs-comment"># ' 123456'</span>
{<span class="hljs-number">-123456</span>: } <span class="hljs-comment"># '-123456'</span>
</code></pre></div>
@ -1025,6 +1025,7 @@ Z = dataclasses.make_dataclass(<span class="hljs-string">'Z'</span>, [<span clas
<ul>
<li><strong>For arbitrary type use <code class="python hljs"><span class="hljs-string">'typing.Any'</span></code>.</strong></li>
<li><strong>Objects can be made sortable with <code class="python hljs"><span class="hljs-string">'order=True'</span></code> and immutable with <code class="python hljs"><span class="hljs-string">'frozen=True'</span></code>.</strong></li>
<li><strong>For object to be hashable, all attributes must be hashable and frozen must be True.</strong></li>
<li><strong>Function field() is needed because <code class="python hljs"><span class="hljs-string">'&lt;attr_name&gt;: list = []'</span></code> would make a list that is shared among all instances.</strong></li>
@ -1291,11 +1292,11 @@ Cutlery = Enum(<span class="hljs-string">'Cutlery'</span>, {<span class="hljs-st
<div><h4 id="userdefinedfunctionscannotbevaluessotheymustbewrapped">User-defined functions cannot be values, so they must be wrapped:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> functools <span class="hljs-keyword">import</span> partial
LogicOp = Enum(<span class="hljs-string">'LogicOp'</span>, {<span class="hljs-string">'AND'</span>: partial(<span class="hljs-keyword">lambda</span> l, r: l <span class="hljs-keyword">and</span> r),
<span class="hljs-string">'OR'</span> : partial(<span class="hljs-keyword">lambda</span> l, r: l <span class="hljs-keyword">or</span> r)})
<span class="hljs-string">'OR'</span>: partial(<span class="hljs-keyword">lambda</span> l, r: l <span class="hljs-keyword">or</span> r)})
</code></pre></div>
<ul>
<li><strong>Another solution in this particular case is to use functions and_() and or_() from the module <a href="#operator">operator</a>.</strong></li>
<li><strong>Member names are in all caps because trying to access a member that is named after a reserved keyword raises SyntaxError.</strong></li>
</ul>
<div><h2 id="exceptions"><a href="#exceptions" name="exceptions">#</a>Exceptions</h2><div><h3 id="basicexample">Basic Example</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">try</span>:
&lt;code&gt;
@ -1318,7 +1319,7 @@ LogicOp = Enum(<span class="hljs-string">'LogicOp'</span>, {<span class="hljs-st
<ul>
<li><strong>Code inside the <code class="python hljs"><span class="hljs-string">'else'</span></code> block will only be executed if <code class="python hljs"><span class="hljs-string">'try'</span></code> block had no exceptions.</strong></li>
<li><strong>Code inside the <code class="python hljs"><span class="hljs-string">'finally'</span></code> block will always be executed.</strong></li>
<li><strong>Code inside the <code class="python hljs"><span class="hljs-string">'finally'</span></code> block will always be executed (unless a signal is received).</strong></li>
</ul>
<div><h3 id="catchingexceptions">Catching Exceptions</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">except</span> &lt;exception&gt;:
<span class="hljs-keyword">except</span> &lt;exception&gt; <span class="hljs-keyword">as</span> &lt;name&gt;:
@ -1903,7 +1904,6 @@ sorted_by_second = sorted(&lt;collection&gt;, key=op.itemgetter(<span class="hlj
sorted_by_both = sorted(&lt;collection&gt;, key=op.itemgetter(<span class="hljs-number">1</span>, <span class="hljs-number">0</span>))
product_of_elems = functools.reduce(op.mul, &lt;collection&gt;)
union_of_sets = functools.reduce(op.or_, &lt;coll_of_sets&gt;)
LogicOp = enum.Enum(<span class="hljs-string">'LogicOp'</span>, {<span class="hljs-string">'AND'</span>: op.and_, <span class="hljs-string">'OR'</span>: op.or_})
last_el = op.methodcaller(<span class="hljs-string">'pop'</span>)(&lt;list&gt;)
</code></pre>
<div><h2 id="introspection"><a href="#introspection" name="introspection">#</a>Introspection</h2><p><strong>Inspecting code at runtime.</strong></p><div><h3 id="variables">Variables</h3><pre><code class="python language-python hljs">&lt;list&gt; = dir() <span class="hljs-comment"># Names of local variables (incl. functions).</span>
@ -3007,7 +3007,7 @@ $ pyinstaller script.py --add-data '&lt;path&gt;:.' <span class="hljs-comment">
<footer>
<aside>October 4, 2021</aside>
<aside>October 7, 2021</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</footer>

2
web/faq.html

@ -1,5 +1,5 @@
<details open><summary><strong>Python 2 or Python 3?</strong></summary><br>
&nbsp;&nbsp;&nbsp;&nbsp;Python 3.6 (Except for dataclasses and asyncio that require version 3.7).
&nbsp;&nbsp;&nbsp;&nbsp;Python 3.7
</details><br>
<details open><summary><strong>What is the best way to use it?</strong></summary><br>

Loading…
Cancel
Save