Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2be9c2d

Browse files
committed
Format, Class, Enum, Exception
1 parent bbde7a7 commit 2be9c2d

File tree

3 files changed

+23
-23
lines changed

3 files changed

+23
-23
lines changed

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -425,12 +425,12 @@ Format
425425

426426
### Numbers
427427
```python
428-
{ 123456:10} # ' 123456'
429-
{ 123456:10,} # ' 123,456'
430-
{ 123456:10_} # ' 123_456'
431-
{ 123456:+10} # ' +123456'
432-
{-123456:=10} # '- 123456'
433-
{ 123456: } # ' 123456'
428+
{123456:10} # ' 123456'
429+
{123456:10,} # ' 123,456'
430+
{123456:10_} # ' 123_456'
431+
{123456:+10} # ' +123456'
432+
{123456:=+10} # '+ 123456'
433+
{123456: } # ' 123456'
434434
{-123456: } # '-123456'
435435
```
436436

@@ -1031,6 +1031,7 @@ class <class_name>:
10311031
<attr_name_2>: <type> = <default_value>
10321032
<attr_name_3>: list/dict/set = field(default_factory=list/dict/set)
10331033
```
1034+
* **For arbitrary type use `'typing.Any'`.**
10341035
* **Objects can be made sortable with `'order=True'` and immutable with `'frozen=True'`.**
10351036
* **For object to be hashable, all attributes must be hashable and frozen must be True.**
10361037
* **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})
13331334
```python
13341335
from functools import partial
13351336
LogicOp = Enum('LogicOp', {'AND': partial(lambda l, r: l and r),
1336-
'OR' : partial(lambda l, r: l or r)})
1337+
'OR': partial(lambda l, r: l or r)})
13371338
```
1338-
* **Another solution in this particular case is to use functions and\_() and or\_() from the module [operator](#operator).**
1339+
* **Member names are in all caps because trying to access a member that is named after a reserved keyword raises SyntaxError.**
13391340

13401341

13411342
Exceptions
@@ -1362,7 +1363,7 @@ finally:
13621363
<code_3>
13631364
```
13641365
* **Code inside the `'else'` block will only be executed if `'try'` block had no exceptions.**
1365-
* **Code inside the `'finally'` block will always be executed.**
1366+
* **Code inside the `'finally'` block will always be executed (unless a signal is received).**
13661367

13671368
### Catching Exceptions
13681369
```python
@@ -2131,7 +2132,6 @@ sorted_by_second = sorted(<collection>, key=op.itemgetter(1))
21312132
sorted_by_both = sorted(<collection>, key=op.itemgetter(1, 0))
21322133
product_of_elems = functools.reduce(op.mul, <collection>)
21332134
union_of_sets = functools.reduce(op.or_, <coll_of_sets>)
2134-
LogicOp = enum.Enum('LogicOp', {'AND': op.and_, 'OR': op.or_})
21352135
last_el = op.methodcaller('pop')(<list>)
21362136
```
21372137

index.html

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@
226226

227227
<body>
228228
<header>
229-
<aside>October 4, 2021</aside>
229+
<aside>October 7, 2021</aside>
230230
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
231231
</header>
232232

@@ -546,12 +546,12 @@
546546
{<span class="hljs-string">'abcde'</span>!r:<span class="hljs-number">10</span>} <span class="hljs-comment"># "'abcde' "</span>
547547
</code></pre></div>
548548

549-
<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>
550-
{ <span class="hljs-number">123456</span>:<span class="hljs-number">10</span>,} <span class="hljs-comment"># ' 123,456'</span>
551-
{ <span class="hljs-number">123456</span>:<span class="hljs-number">10</span>_} <span class="hljs-comment"># ' 123_456'</span>
552-
{ <span class="hljs-number">123456</span>:+<span class="hljs-number">10</span>} <span class="hljs-comment"># ' +123456'</span>
553-
{<span class="hljs-number">-123456</span>:=<span class="hljs-number">10</span>} <span class="hljs-comment"># '- 123456'</span>
554-
{ <span class="hljs-number">123456</span>: } <span class="hljs-comment"># ' 123456'</span>
549+
<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>
550+
{<span class="hljs-number">123456</span>:<span class="hljs-number">10</span>,} <span class="hljs-comment"># ' 123,456'</span>
551+
{<span class="hljs-number">123456</span>:<span class="hljs-number">10</span>_} <span class="hljs-comment"># ' 123_456'</span>
552+
{<span class="hljs-number">123456</span>:+<span class="hljs-number">10</span>} <span class="hljs-comment"># ' +123456'</span>
553+
{<span class="hljs-number">123456</span>:=+<span class="hljs-number">10</span>} <span class="hljs-comment"># '+ 123456'</span>
554+
{<span class="hljs-number">123456</span>: } <span class="hljs-comment"># ' 123456'</span>
555555
{<span class="hljs-number">-123456</span>: } <span class="hljs-comment"># '-123456'</span>
556556
</code></pre></div>
557557

@@ -1025,6 +1025,7 @@
10251025

10261026

10271027
<ul>
1028+
<li><strong>For arbitrary type use <code class="python hljs"><span class="hljs-string">'typing.Any'</span></code>.</strong></li>
10281029
<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>
10291030
<li><strong>For object to be hashable, all attributes must be hashable and frozen must be True.</strong></li>
10301031
<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 @@
12911292

12921293
<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
12931294
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),
1294-
<span class="hljs-string">'OR'</span> : partial(<span class="hljs-keyword">lambda</span> l, r: l <span class="hljs-keyword">or</span> r)})
1295+
<span class="hljs-string">'OR'</span>: partial(<span class="hljs-keyword">lambda</span> l, r: l <span class="hljs-keyword">or</span> r)})
12951296
</code></pre></div>
12961297

12971298
<ul>
1298-
<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>
1299+
<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>
12991300
</ul>
13001301
<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>:
13011302
&lt;code&gt;
@@ -1318,7 +1319,7 @@
13181319

13191320
<ul>
13201321
<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>
1321-
<li><strong>Code inside the <code class="python hljs"><span class="hljs-string">'finally'</span></code> block will always be executed.</strong></li>
1322+
<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>
13221323
</ul>
13231324
<div><h3 id="catchingexceptions">Catching Exceptions</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">except</span> &lt;exception&gt;:
13241325
<span class="hljs-keyword">except</span> &lt;exception&gt; <span class="hljs-keyword">as</span> &lt;name&gt;:
@@ -1903,7 +1904,6 @@
19031904
sorted_by_both = sorted(&lt;collection&gt;, key=op.itemgetter(<span class="hljs-number">1</span>, <span class="hljs-number">0</span>))
19041905
product_of_elems = functools.reduce(op.mul, &lt;collection&gt;)
19051906
union_of_sets = functools.reduce(op.or_, &lt;coll_of_sets&gt;)
1906-
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_})
19071907
last_el = op.methodcaller(<span class="hljs-string">'pop'</span>)(&lt;list&gt;)
19081908
</code></pre>
19091909
<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 @@
30073007

30083008

30093009
<footer>
3010-
<aside>October 4, 2021</aside>
3010+
<aside>October 7, 2021</aside>
30113011
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
30123012
</footer>
30133013

web/faq.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<details open><summary><strong>Python 2 or Python 3?</strong></summary><br>
2-
&nbsp;&nbsp;&nbsp;&nbsp;Python 3.6 (Except for dataclasses and asyncio that require version 3.7).
2+
&nbsp;&nbsp;&nbsp;&nbsp;Python 3.7
33
</details><br>
44

55
<details open><summary><strong>What is the best way to use it?</strong></summary><br>

0 commit comments

Comments
 (0)