Skip to content

Commit 39ca9bc

Browse files
committed
Match statement, Threading
1 parent fb82911 commit 39ca9bc

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,7 +1247,7 @@ True
12471247

12481248
### Collection
12491249
* **Only required methods are iter() and len(). Len() should return the number of items.**
1250-
* **This cheatsheet actually means `'<iterable>'` when it uses `'<collection>'`.**
1250+
* **This cheatsheet actually means `'<iterable>'` when it uses the `'<collection>'`.**
12511251
* **I chose not to use the name 'iterable' because it sounds scarier and more vague than 'collection'. The main drawback of this decision is that the reader could think a certain function doesn't accept iterators when it does, since iterators are the only built-in objects that are iterable but are not collections.**
12521252
```python
12531253
class MyCollection:
@@ -2141,11 +2141,11 @@ match <object/expression>:
21412141
<mapping_patt> = {<value_pattern>: <patt>, ...} # Matches a dict that has matching items.
21422142
<class_pattern> = <type>(<attr_name>=<patt>, ...) # Matches an object if attributes match.
21432143
```
2144-
* **Sequence pattern can also be written as a tuple, i.e. `'(<pattern_1>, [...])'`.**
2144+
* **Sequence pattern can also be written as a tuple, either with or without the brackets.**
21452145
* **Use `'*<name>'` and `'**<name>'` in sequence/mapping patterns to bind remaining items.**
21462146
* **Sequence pattern must match all items of the collection, while mapping pattern does not.**
2147-
* **Patterns can be surrounded with brackets to override precedence (`'|'` > `'as'` > `','`).**
2148-
* **All names that are bound in the matching case, as well as variables initialized in its block, are visible after the match statement.**
2147+
* **Patterns can be surrounded with brackets to override their precedence: `'|'` > `'as'` > `','`. For example, `'[1, 2]'` gets caught by the `'case 1 | 0, 2 as x if x == 2'` clause.**
2148+
* **All names that are bound in the matching case, as well as variables initialized in its block, are visible after the match statement (only the function block delimits scope).**
21492149

21502150
### Example
21512151
```python
@@ -2300,7 +2300,7 @@ with <lock>: # Enters the block by calling acq
23002300
<iter> = as_completed(<coll_of_Futures>) # `next(<iter>)` returns next completed Future.
23012301
```
23022302
* **Map() and as\_completed() also accept 'timeout'. It causes futures.TimeoutError when next() is called/blocking. Map() times from original call and as_completed() from first call to next(). As\_completed() fails if next() is called too late, even if all threads are done.**
2303-
* **Exceptions that happen inside threads are raised when map iterator's next() or Future's result() are called. Future's exception() method returns exception object or None.**
2303+
* **Exceptions that happen inside threads are raised when map iterator's next() or Future's result() are called. Future's exception() method returns an exception object or None.**
23042304
* **ProcessPoolExecutor provides true parallelism but: everything sent to/from workers must be [pickable](#pickle), queues must be sent using executor's 'initargs' and 'initializer' parameters, and executor should only be reachable via `'if __name__ == "__main__": ...'`.**
23052305

23062306

index.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,7 +1070,7 @@
10701070
</code></pre>
10711071
<div><h3 id="collection">Collection</h3><ul>
10721072
<li><strong>Only required methods are iter() and len(). Len() should return the number of items.</strong></li>
1073-
<li><strong>This cheatsheet actually means <code class="python hljs"><span class="hljs-string">'&lt;iterable&gt;'</span></code> when it uses <code class="python hljs"><span class="hljs-string">'&lt;collection&gt;'</span></code>.</strong></li>
1073+
<li><strong>This cheatsheet actually means <code class="python hljs"><span class="hljs-string">'&lt;iterable&gt;'</span></code> when it uses the <code class="python hljs"><span class="hljs-string">'&lt;collection&gt;'</span></code>.</strong></li>
10741074
<li><strong>I chose not to use the name 'iterable' because it sounds scarier and more vague than 'collection'. The main drawback of this decision is that the reader could think a certain function doesn't accept iterators when it does, since iterators are the only built-in objects that are iterable but are not collections.</strong></li>
10751075
</ul><pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyCollection</span>:</span>
10761076
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, a)</span>:</span>
@@ -1772,11 +1772,11 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
17721772
</code></pre></div>
17731773

17741774
<ul>
1775-
<li><strong>Sequence pattern can also be written as a tuple, i.e. <code class="python hljs"><span class="hljs-string">'(&lt;pattern_1&gt;, [...])'</span></code>.</strong></li>
1775+
<li><strong>Sequence pattern can also be written as a tuple, either with or without the brackets.</strong></li>
17761776
<li><strong>Use <code class="python hljs"><span class="hljs-string">'*&lt;name&gt;'</span></code> and <code class="python hljs"><span class="hljs-string">'**&lt;name&gt;'</span></code> in sequence/mapping patterns to bind remaining items.</strong></li>
17771777
<li><strong>Sequence pattern must match all items of the collection, while mapping pattern does not.</strong></li>
1778-
<li><strong>Patterns can be surrounded with brackets to override precedence (<code class="python hljs"><span class="hljs-string">'|'</span></code> &gt; <code class="python hljs"><span class="hljs-string">'as'</span></code> &gt; <code class="python hljs"><span class="hljs-string">','</span></code>).</strong></li>
1779-
<li><strong>All names that are bound in the matching case, as well as variables initialized in its block, are visible after the match statement.</strong></li>
1778+
<li><strong>Patterns can be surrounded with brackets to override their precedence: <code class="python hljs"><span class="hljs-string">'|'</span></code> &gt; <code class="python hljs"><span class="hljs-string">'as'</span></code> &gt; <code class="python hljs"><span class="hljs-string">','</span></code>. For example, <code class="python hljs"><span class="hljs-string">'[1, 2]'</span></code> gets caught by the <code class="python hljs"><span class="hljs-string">'case 1 | 0, 2 as x if x == 2'</span></code> clause.</strong></li>
1779+
<li><strong>All names that are bound in the matching case, as well as variables initialized in its block, are visible after the match statement (only the function block delimits scope).</strong></li>
17801780
</ul>
17811781
<div><h3 id="example-2">Example</h3><pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> pathlib <span class="hljs-keyword">import</span> Path
17821782
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">match</span> Path(<span class="hljs-string">'/home/gto/python-cheatsheet/README.md'</span>):
@@ -1897,7 +1897,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
18971897
</code></pre>
18981898
<ul>
18991899
<li><strong>Map() and as_completed() also accept 'timeout'. It causes futures.TimeoutError when next() is called/blocking. Map() times from original call and as_completed() from first call to next(). As_completed() fails if next() is called too late, even if all threads are done.</strong></li>
1900-
<li><strong>Exceptions that happen inside threads are raised when map iterator's next() or Future's result() are called. Future's exception() method returns exception object or None.</strong></li>
1900+
<li><strong>Exceptions that happen inside threads are raised when map iterator's next() or Future's result() are called. Future's exception() method returns an exception object or None.</strong></li>
19011901
<li><strong>ProcessPoolExecutor provides true parallelism but: everything sent to/from workers must be <a href="#pickle">pickable</a>, queues must be sent using executor's 'initargs' and 'initializer' parameters, and executor should only be reachable via <code class="python hljs"><span class="hljs-string">'if __name__ == "__main__": ...'</span></code>.</strong></li>
19021902
</ul>
19031903
<div><h2 id="coroutines"><a href="#coroutines" name="coroutines">#</a>Coroutines</h2><ul>

0 commit comments

Comments
 (0)