8000 CSV, Introspection, Metaprogramming · fluencydoc/python-cheatsheet@fd1df4a · GitHub
[go: up one dir, main page]

Skip to content

Commit fd1df4a

Browse files
committed
CSV, Introspection, Metaprogramming
1 parent cae27c1 commit fd1df4a

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ Type
257257

258258
#### Some types do not have built-in names, so they must be imported:
259259
```python
260-
from types import FunctionType, MethodType, LambdaType, GeneratorType
260+
from types import FunctionType, MethodType, LambdaType, GeneratorType, ModuleType
261261
```
262262

263263
### Abstract Base Classes
@@ -1813,7 +1813,7 @@ import csv
18131813
* **`'skipinitialspace'` - Whether whitespace after delimiter gets stripped.**
18141814
* **`'lineterminator'` - Specifies how writer terminates rows.**
18151815
* **`'quoting'` - Controls the amount of quoting: 0 - as necessary, 1 - all.**
1816-
* **`'escapechar'` - Character for escaping 'quotechar' if 'doublequote' is False.**
1816+
* **`'escapechar'` - Character for escaping quotechars if doublequote is False.**
18171817

18181818
### Dialects
18191819
```text
@@ -2165,8 +2165,8 @@ Introspection
21652165
<dict> = vars(<object>) # Dict of writable attributes. Also <obj>.__dict__.
21662166
<bool> = hasattr(<object>, '<attr_name>') # Checks if getattr() raises an AttributeError.
21672167
value = getattr(<object>, '<attr_name>') # Raises AttributeError if attribute is missing.
2168-
setattr(<object>, '<attr_name>', value) # Only works on objects with __dict__ attribute.
2169-
delattr(<object>, '<attr_name>') # Equivalent to `del <object>.<attr_name>`.
2168+
setattr(<object>, '<attr_name>', value) # Only works on objects with '__dict__' attribute.
2169+
delattr(<object>, '<attr_name>') # Same. Also `del <object>.<attr_name>`.
21702170
```
21712171

21722172
### Parameters
@@ -2187,7 +2187,7 @@ Metaprogramming
21872187
**Type is the root class. If only passed an object it returns its type (class). Otherwise it creates a new class.**
21882188

21892189
```python
2190-
<class> = type('<class_name>', <parents_tuple>, <attributes_dict>)
2190+
<class> = type('<class_name>', <tuple_of_parents>, <dict_of_class_attributes>)
21912191
```
21922192

21932193
```python

index.html

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

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

@@ -412,7 +412,7 @@
412412
<pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span>type(<span class="hljs-string">'a'</span>), <span class="hljs-string">'a'</span>.__class__, str
413413
(&lt;<span class="hljs-class"><span class="hljs-title">class</span> '<span class="hljs-title">str</span>'&gt;, &lt;<span class="hljs-title">class</span> '<span class="hljs-title">str</span>'&gt;, &lt;<span class="hljs-title">class</span> '<span class="hljs-title">str</span>'&gt;)
414414
</span></code></pre>
415-
<div><h4 id="sometypesdonothavebuiltinnamessotheymustbeimported">Some types do not have built-in names, so they must be imported:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> types <span class="hljs-keyword">import</span> FunctionType, MethodType, LambdaType, GeneratorType
415+
<div><h4 id="sometypesdonothavebuiltinnamessotheymustbeimported">Some types do not have built-in names, so they must be imported:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> types <span class="hljs-keyword">import</span> FunctionType, MethodType, LambdaType, GeneratorType, ModuleType
416416
</code></pre></div>
417417

418418
<div><h3 id="abstractbaseclasses">Abstract Base Classes</h3><p><strong>Each abstract base class specifies a set of virtual subclasses. These classes are then recognized by isinstance() and issubclass() as subclasses of the ABC, although they are really not. ABC can also manually decide whether or not a specific class is its virtual subclass, usually based on which methods the class has implemented. For instance, Iterable ABC looks for method iter() while Collection ABC looks for methods iter(), contains() and len().</strong></p><pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> collections.abc <span class="hljs-keyword">import</span> Sequence, Collection, Iterable
@@ -1661,7 +1661,7 @@
16611661
<li><strong><code class="python hljs"><span class="hljs-string">'skipinitialspace'</span></code> - Whether whitespace after delimiter gets stripped.</strong></li>
16621662
<li><strong><code class="python hljs"><span class="hljs-string">'lineterminator'</span></code> - Specifies how writer terminates rows.</strong></li>
16631663
<li><strong><code class="python hljs"><span class="hljs-string">'quoting'</span></code> - Controls the amount of quoting: 0 - as necessary, 1 - all.</strong></li>
1664-
<li><strong><code class="python hljs"><span class="hljs-string">'escapechar'</span></code> - Character for escaping 'quotechar' if 'doublequote' is False.</strong></li>
1664+
<li><strong><code class="python hljs"><span class="hljs-string">'escapechar'</span></code> - Character for escaping quotechars if doublequote is False.</strong></li>
16651665
</ul><div><h3 id="dialects">Dialects</h3><pre><code class="text language-text">┏━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━┓
16661666
┃ │ excel │ excel-tab │ unix ┃
16671667
┠──────────────────┼──────────────┼──────────────┼──────────────┨
@@ -1927,8 +1927,8 @@
19271927
&lt;dict&gt; = vars(&lt;object&gt;) <span class="hljs-comment"># Dict of writable attributes. Also &lt;obj&gt;.__dict__.</span>
19281928
&lt;bool&gt; = hasattr(&lt;object&gt;, <span class="hljs-string">'&lt;attr_name&gt;'</span>) <span class="hljs-comment"># Checks if getattr() raises an AttributeError.</span>
19291929
value = getattr(&lt;object&gt;, <span class="hljs-string">'&lt;attr_name&gt;'</span>) <span class="hljs-comment"># Raises AttributeError if attribute is missing.</span>
1930-
setattr(&lt;object&gt;, <span class="hljs-string">'&lt;attr_name&gt;'</span>, value) <span class="hljs-comment"># Only works on objects with __dict__ attribute.</span>
1931-
delattr(&lt;object&gt;, <span class="hljs-string">'&lt;attr_name&gt;'</span>) <span class="hljs-comment"># Equivalent to `del &lt;object&gt;.&lt;attr_name&gt;`.</span>
1930+
setattr(&lt;object&gt;, <span class="hljs-string">'&lt;attr_name&gt;'</span>, value) <span class="hljs-comment"># Only works on objects with '__dict__' attribute.</span>
1931+
delattr(&lt;object&gt;, <span class="hljs-string">'&lt;attr_name&gt;'</span>) <span class="hljs-comment"># Same. Also `del &lt;object&gt;.&lt;attr_name&gt;`.</span>
19321932
</code></pre></div>
19331933

19341934
<div><h3 id="parameters-1">Parameters</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> inspect <span class="hljs-keyword">import</span> signature
@@ -1938,7 +1938,7 @@
19381938
&lt;memb&gt; = &lt;Param&gt;.kind <span class="hljs-comment"># Member of ParameterKind enum.</span>
19391939
</code></pre></div>
19401940

1941-
<div><h2 id="metaprogramming"><a href="#metaprogramming" name="metaprogramming">#</a>Metaprogramming</h2><p><strong>Code that generates code.</strong></p><div><h3 id="type-1">Type</h3><p><strong>Type is the root class. If only passed an object it returns its type (class). Otherwise it creates a new class.</strong></p><pre><code class="python language-python hljs">&lt;class&gt; = type(<span class="hljs-string">'&lt;class_name&gt;'</span>, &lt;parents_tuple&gt;, &lt;attributes_dict&gt;)</code></pre></div></div>
1941+
<div><h2 id="metaprogramming"><a href="#metaprogramming" name="metaprogramming">#</a>Metaprogramming</h2><p><strong>Code that generates code.</strong></p><div><h3 id="type-1">Type</h3><p><strong>Type is the root class. If only passed an object it returns its type (class). Otherwise it creates a new class.</strong></p><pre><code class="python language-python hljs">&lt;class&gt; = type(<span class="hljs-string">'&lt;class_name&gt;'</span>, &lt;tuple_of_parents&gt;, &lt;dict_of_class_attributes&gt;)</code></pre></div></div>
19421942

19431943

19441944

@@ -3017,7 +3017,7 @@
30173017

30183018

30193019
<footer>
3020-
<aside>October 16, 2021</aside>
3020+
<aside>October 20, 2021</aside>
30213021
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
30223022
</footer>
30233023

parse.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ const OS_RENAME =
7171
'os.replace(from, to) <span class="hljs-comment"># Same, but overwrites \'to\' if it exists.</span>\n';
7272

7373
const TYPE =
74-
'&lt;class&gt; = type(<span class="hljs-string">\'&lt;class_name&gt;\'</span>, &lt;parents_tuple&gt;, &lt;attributes_dict&gt;)';
74+
'&lt;class&gt; = type(<span class="hljs-string">\'&lt;class_name&gt;\'</span>, &lt;tuple_of_parents&gt;, &lt;dict_of_class_attributes&gt;)';
7575

7676
const EVAL =
7777
'<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> ast <span class="hljs-keyword">import</span> literal_eval\n' +
@@ -526,7 +526,7 @@ function fixHighlights() {
526526
$(`code:contains(make_dataclass(\'<class_name>\')`).html(DATACLASS);
527527
$(`code:contains(shutil.copy)`).html(SHUTIL_COPY);
528528
$(`code:contains(os.rename)`).html(OS_RENAME);
529-
$(`code:contains(\'<class_name>\', <parents_tuple>, <attributes_dict>)`).html(TYPE);
529+
$(`code:contains(\'<class_name>\', <tuple_of_parents>, <dict_of_class_attributes>)`).html(TYPE);
530530
$(`code:contains(ValueError: malformed node)`).html(EVAL);
531531
$(`code:contains(pip3 install tqdm)`).html(PROGRESS_BAR);
532532
$(`code:contains(pip3 install pyinstaller)`).html(PYINSTALLER);

0 commit comments

Comments
 (0)
0