Browse Source

Duck types

pull/36/head
Jure Šorn 5 years ago
parent
commit
befee8a316
2 changed files with 11 additions and 11 deletions
  1. 11
      README.md
  2. 11
      index.html

11
README.md

@ -1154,17 +1154,13 @@ class MyOpen():
Hello World! Hello World!
``` ```
#### Context managers:
#### List of existing context managers:
```python ```python
with open('<path>', ...) as file: ... with open('<path>', ...) as file: ...
with wave.open('<path>', ...) as wave_file: ... with wave.open('<path>', ...) as wave_file: ...
with memoryview(<bytes/bytearray/array>) as view: ... with memoryview(<bytes/bytearray/array>) as view: ...
```
#### Reusable context managers:
```python
db = sqlite3.connect('<path>'); with db: db.execute('<insert_query>')
lock = threading.RLock(); with lock: ... lock = threading.RLock(); with lock: ...
con = sqlite3.connect('<path>'); with con: con.execute('<insert_query>')
``` ```
@ -1232,6 +1228,7 @@ class MySequence:
* **It's a richer interface than the basic sequence.** * **It's a richer interface than the basic sequence.**
* **Extending it generates iter(), contains(), reversed(), index(), and count().** * **Extending it generates iter(), contains(), reversed(), index(), and count().**
* **Unlike `'abc.Iterable'` and `'abc.Collection'`, it is not a duck type. That is why `'issubclass(MySequence, collections.abc.Sequence)'` would return 'False' even if it had all the methods defined.** * **Unlike `'abc.Iterable'` and `'abc.Collection'`, it is not a duck type. That is why `'issubclass(MySequence, collections.abc.Sequence)'` would return 'False' even if it had all the methods defined.**
```python ```python
class MyAbcSequence(collections.abc.Sequence): class MyAbcSequence(collections.abc.Sequence):
def __init__(self, a): def __init__(self, a):
@ -1258,6 +1255,8 @@ class MyAbcSequence(collections.abc.Sequence):
+------------+----------+------------+----------+--------------+ +------------+----------+------------+----------+--------------+
``` ```
* **Other useful ABCs that automatically generate missing methods are: MutableSequence, Set, MutableSet, Mapping and MutableMapping.**
Enum Enum
---- ----

11
index.html

@ -1075,14 +1075,12 @@ Z = dataclasses.make_dataclass(<span class="hljs-string">'Z'</span>, [<span clas
<span class="hljs-meta">... </span> print(file.read()) <span class="hljs-meta">... </span> print(file.read())
Hello World! Hello World!
</code></pre> </code></pre>
<h4 id="contextmanagers">Context managers:</h4>
<h4 id="listofexistingcontextmanagers">List of existing context managers:</h4>
<pre><code class="python language-python hljs"><span class="hljs-keyword">with</span> open(<span class="hljs-string">'&lt;path&gt;'</span>, ...) <span class="hljs-keyword">as</span> file: ... <pre><code class="python language-python hljs"><span class="hljs-keyword">with</span> open(<span class="hljs-string">'&lt;path&gt;'</span>, ...) <span class="hljs-keyword">as</span> file: ...
<span class="hljs-keyword">with</span> wave.open(<span class="hljs-string">'&lt;path&gt;'</span>, ...) <span class="hljs-keyword">as</span> wave_file: ... <span class="hljs-keyword">with</span> wave.open(<span class="hljs-string">'&lt;path&gt;'</span>, ...) <span class="hljs-keyword">as</span> wave_file: ...
<span class="hljs-keyword">with</span> memoryview(&lt;bytes/bytearray/array&gt;) <span class="hljs-keyword">as</span> view: ... <span class="hljs-keyword">with</span> memoryview(&lt;bytes/bytearray/array&gt;) <span class="hljs-keyword">as</span> view: ...
</code></pre>
<h4 id="reusablecontextmanagers">Reusable context managers:</h4>
<pre><code class="python language-python hljs">lock = threading.RLock(); <span class="hljs-keyword">with</span> lock: ...
con = sqlite3.connect(<span class="hljs-string">'&lt;path&gt;'</span>); <span class="hljs-keyword">with</span> con: con.execute(<span class="hljs-string">'&lt;insert_query&gt;'</span>)
db = sqlite3.connect(<span class="hljs-string">'&lt;path&gt;'</span>); <span class="hljs-keyword">with</span> db: db.execute(<span class="hljs-string">'&lt;insert_query&gt;'</span>)
lock = threading.RLock(); <span class="hljs-keyword">with</span> lock: ...
</code></pre> </code></pre>
<h2 id="iterableducktypes"><a href="#iterableducktypes" name="iterableducktypes">#</a>Iterable Duck Types</h2> <h2 id="iterableducktypes"><a href="#iterableducktypes" name="iterableducktypes">#</a>Iterable Duck Types</h2>
<h3 id="iterable">Iterable</h3> <h3 id="iterable">Iterable</h3>
@ -1168,6 +1166,9 @@ con = sqlite3.connect(<span class="hljs-string">'&lt;path&gt;'</span>); <span c
┃ count() │ │ │ │ ✓ ┃ ┃ count() │ │ │ │ ✓ ┃
┗━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━━━━━┛ ┗━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━━━━━┛
</code></pre> </code></pre>
<ul>
<li><strong>Other useful ABCs that automatically generate missing methods are: MutableSequence, Set, MutableSet, Mapping and MutableMapping.</strong></li>
</ul>
<h2 id="enum"><a href="#enum" name="enum">#</a>Enum</h2> <h2 id="enum"><a href="#enum" name="enum">#</a>Enum</h2>
<pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> enum <span class="hljs-keyword">import</span> Enum, auto <pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> enum <span class="hljs-keyword">import</span> Enum, auto

Loading…
Cancel
Save