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

Skip to content

Commit 9c81a49

Browse files
committed
Class, Duck types, Open, Memory view, Operator, Metaprogramming, Web, Profile
1 parent 0564709 commit 9c81a49

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,7 +1068,7 @@ from dataclasses import make_dataclass
10681068

10691069
#### Rest of type annotations (CPython interpreter ignores them all):
10701070
```python
1071-
import typing as tp, collections.abc as abc
1071+
import collections.abc as abc, typing as tp
10721072
<var_name>: list/set/abc.Iterable/abc.Sequence/tp.Optional[<type>] [= <obj>]
10731073
<var_name>: dict/tuple/tp.Union[<type>, ...] [= <obj>]
10741074
def func(<arg_name>: <type> [= <obj>]) -> <type>: ...
@@ -1252,7 +1252,7 @@ True
12521252
### Collection
12531253
* **Only required methods are iter() and len(). Len() should return the number of items.**
12541254
* **This cheatsheet actually means `'<iterable>'` when it uses `'<collection>'`.**
1255-
* **I chose not to use the name 'iterable' because it sounds scarier and more vague than 'collection'. The only drawback of this decision is that a 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.**
1255+
* **I chose not to use the name 'iterable' because it sounds scarier and more vague than 'collection'. The only 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.**
12561256
```python
12571257
class MyCollection:
12581258
def __init__(self, a):
@@ -1606,7 +1606,7 @@ Open
16061606
<file>.writelines(<collection>) # Writes a coll. of strings or bytes objects.
16071607
<file>.flush() # Flushes write buffer. Runs every 4096/8192 B.
16081608
```
1609-
* **Methods do not add or strip trailing newlines, even writelines().**
1609+
* **Methods do not add or strip trailing newlines, not even writelines().**
16101610

16111611
### Read Text from File
16121612
```python
@@ -2045,7 +2045,7 @@ from array import array
20452045

20462046
Memory View
20472047
-----------
2048-
* **A sequence object that points to the memory of another object.**
2048+
* **A sequence object that points to the memory of another bytes-like object.**
20492049
* **Each element can reference a single or multiple consecutive bytes, depending on format.**
20502050
* **Order and number of elements can be changed with slicing.**
20512051
* **Casting only works between char and other types and uses system's sizes.**
@@ -2177,7 +2177,7 @@ product_of_elems = functools.reduce(op.mul, <collection>)
21772177
union_of_sets = functools.reduce(op.or_, <coll_of_sets>)
21782178
first_element = op.methodcaller('pop', 0)(<list>)
21792179
```
2180-
* **Binary operators require objects to have and(), or(), xor() and invert() special methods, unlike logical operators that work on all types of objects.**
2180+
* **Bitwise operators require objects to have and(), or(), xor() and invert() special methods, unlike logical operators that work on all types of objects.**
21812181
* **Also: `'<bool> = <bool> &|^ <bool>'` and `'<int> = <bool> &|^ <int>'`.**
21822182

21832183

@@ -2250,7 +2250,7 @@ class MyMetaClass(type):
22502250
* **The only difference between the examples above is that my\_meta\_class() returns a class of type type, while MyMetaClass() returns a class of type MyMetaClass.**
22512251

22522252
### Metaclass Attribute
2253-
**Right before a class is created it checks if it has the 'metaclass' attribute defined. If not, it recursively checks if any of his parents has it defined and eventually comes to type().**
2253+
**Right before a class is created it checks if it has the 'metaclass' attribute defined. If not, it recursively checks if any of its parents has it defined and eventually comes to type().**
22542254

22552255
```python
22562256
class MyClass(metaclass=MyMetaClass):
@@ -2535,7 +2535,7 @@ from flask import Flask, send_from_directory, render_template_string, request
25352535

25362536
```python
25372537
app = Flask(__name__)
2538-
app.run(host=None, debug=None)
2538+
app.run(host=None, port=None, debug=None)
25392539
```
25402540
* **Starts the app at `'http://localhost:5000'`. Use `'host="0.0.0.0"'` to run externally.**
25412541
* **Install a WSGI server like [Waitress](https://flask.palletsprojects.com/en/latest/deploying/waitress/) and a HTTP server such as [Nginx](https://flask.palletsprojects.com/en/latest/deploying/nginx/) for better security.**
@@ -2599,11 +2599,11 @@ duration_in_seconds = perf_counter() - start_time
25992599
### Profiling by Line
26002600
```text
26012601
$ pip3 install line_profiler
2602-
$ echo "@profile
2602+
$ echo '@profile
26032603
def main():
26042604
a = list(range(10000))
26052605
b = set(range(10000))
2606-
main()" > test.py
2606+
main()' > test.py
26072607
$ kernprof -lv test.py
26082608
Line # Hits Time Per Hit % Time Line Contents
26092609
=======================================================

index.html

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454

5555
<body>
5656
<header>
57-
<aside>July 19, 2023</aside>
57+
<aside>July 22, 2023</aside>
5858
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
5959
</header>
6060

@@ -903,7 +903,7 @@
903903
&lt;class&gt; = make_dataclass(<span class="hljs-string">'&lt;class_name&gt;'</span>, &lt;coll_of_tuples&gt;)
904904
&lt;tuple&gt; = (<span class="hljs-string">'&lt;attr_name&gt;'</span>, &lt;type&gt; [, &lt;default_value&gt;])</code></pre></div>
905905

906-
<div><h4 id="restoftypeannotationscpythoninterpreterignoresthemall">Rest of type annotations (CPython interpreter ignores them all):</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> typing <span class="hljs-keyword">as</span> tp, collections.abc <span class="hljs-keyword">as</span> abc
906+
<div><h4 id="restoftypeannotationscpythoninterpreterignoresthemall">Rest of type annotations (CPython interpreter ignores them all):</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> collections.abc <span class="hljs-keyword">as</span> abc, typing <span class="hljs-keyword">as</span> tp
907907
&lt;var_name&gt;: list/set/abc.Iterable/abc.Sequence/tp.Optional[&lt;type&gt;] [= &lt;obj&gt;]
908908
&lt;var_name&gt;: dict/tuple/tp.Union[&lt;type&gt;, ...] [= &lt;obj&gt;]
909909
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">func</span><span class="hljs-params">(&lt;arg_name&gt;: &lt;type&gt; [= &lt;obj&gt;])</span> -&gt; &lt;type&gt;:</span> ...
@@ -1067,7 +1067,7 @@
10671067
<div><h3 id="collection">Collection</h3><ul>
10681068
<li><strong>Only required methods are iter() and len(). Len() should return the number of items.</strong></li>
10691069
<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>
1070-
<li><strong>I chose not to use the name 'iterable' because it sounds scarier and more vague than 'collection'. The only drawback of this decision is that a 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>
1070+
<li><strong>I chose not to use the name 'iterable' because it sounds scarier and more vague than 'collection'. The only 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>
10711071
</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>
10721072
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, a)</span>:</span>
10731073
self.a = a
@@ -1371,7 +1371,7 @@
13711371
&lt;file&gt;.flush() <span class="hljs-comment"># Flushes write buffer. Runs every 4096/8192 B.</span>
13721372
</code></pre>
13731373
<ul>
1374-
<li><strong>Methods do not add or strip trailing newlines, even writelines().</strong></li>
1374+
<li><strong>Methods do not add or strip trailing newlines, not even writelines().</strong></li>
13751375
</ul>
13761376
<div><h3 id="readtextfromfile">Read Text from File</h3><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">read_file</span><span class="hljs-params">(filename)</span>:</span>
13771377
<span class="hljs-keyword">with</span> open(filename, encoding=<span class="hljs-string">'utf-8'</span>) <span class="hljs-keyword">as</span> file:
@@ -1697,7 +1697,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
16971697

16981698

16991699
<div><h2 id="memoryview"><a href="#memoryview" name="memoryview">#</a>Memory View</h2><ul>
1700-
<li><strong>A sequence object that points to the memory of another object.</strong></li>
1700+
<li><strong>A sequence object that points to the memory of another bytes-like object.</strong></li>
17011701
<li><strong>Each element can reference a single or multiple consecutive bytes, depending on format.</strong></li>
17021702
<li><strong>Order and number of elements can be changed with slicing.</strong></li>
17031703
<li><strong>Casting only works between char and other types and uses system's sizes.</strong></li>
@@ -1801,7 +1801,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
18011801
first_element = op.methodcaller(<span class="hljs-string">'pop'</span>, <span class="hljs-number">0</span>)(&lt;list&gt;)
18021802
</code></pre>
18031803
<ul>
1804-
<li><strong>Binary operators require objects to have and(), or(), xor() and invert() special methods, unlike logical operators that work on all types of objects.</strong></li>
1804+
<li><strong>Bitwise operators require objects to have and(), or(), xor() and invert() special methods, unlike logical operators that work on all types of objects.</strong></li>
18051805
<li><strong>Also: <code class="python hljs"><span class="hljs-string">'&lt;bool&gt; = &lt;bool&gt; &amp;|^ &lt;bool&gt;'</span></code> and <code class="python hljs"><span class="hljs-string">'&lt;int&gt; = &lt;bool&gt; &amp;|^ &lt;int&gt;'</span></code>.</strong></li>
18061806
</ul>
18071807
<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>
@@ -1852,7 +1852,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
18521852
<li><strong>Like in our case, new() can also be called directly, usually from a new() method of a child class (</strong><code class="python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__new__</span><span class="hljs-params">(cls)</span>:</span> <span class="hljs-keyword">return</span> super().__new__(cls)</code><strong>).</strong></li>
18531853
<li><strong>The only difference between the examples above is that my_meta_class() returns a class of type type, while MyMetaClass() returns a class of type MyMetaClass.</strong></li>
18541854
</ul>
1855-
<div><h3 id="metaclassattribute">Metaclass Attribute</h3><p><strong>Right before a class is created it checks if it has the 'metaclass' attribute defined. If not, it recursively checks if any of his parents has it defined and eventually comes to type().</strong></p><pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyClass</span><span class="hljs-params">(metaclass=MyMetaClass)</span>:</span>
1855+
<div><h3 id="metaclassattribute">Metaclass Attribute</h3><p><strong>Right before a class is created it checks if it has the 'metaclass' attribute defined. If not, it recursively checks if any of its parents has it defined and eventually comes to type().</strong></p><pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyClass</span><span class="hljs-params">(metaclass=MyMetaClass)</span>:</span>
18561856
b = <span class="hljs-number">12345</span>
18571857
</code></pre></div>
18581858

@@ -2080,7 +2080,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
20802080

20812081

20822082
<pre><code class="python language-python hljs">app = Flask(__name__)
2083-
app.run(host=<span class="hljs-keyword">None</span>, debug=<span class="hljs-keyword">None</span>)
2083+
app.run(host=<span class="hljs-keyword">None</span>, port=<span class="hljs-keyword">None</span>, debug=<span class="hljs-keyword">None</span>)
20842084
</code></pre>
20852085
<ul>
20862086
<li><strong>Starts the app at <code class="python hljs"><span class="hljs-string">'http://localhost:5000'</span></code>. Use <code class="python hljs"><span class="hljs-string">'host="0.0.0.0"'</span></code> to run externally.</strong></li>
@@ -2130,11 +2130,11 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
21302130
</code></pre></div>
21312131

21322132
<div><h3 id="profilingbyline">Profiling by Line</h3><pre><code class="text language-text">$ pip3 install line_profiler
2133-
$ echo "@profile
2133+
$ echo '@profile
21342134
def main():
21352135
a = list(range(10000))
21362136
b = set(range(10000))
2137-
main()" &gt; test.py
2137+
main()' &gt; test.py
21382138
$ kernprof -lv test.py
21392139
Line # Hits Time Per Hit % Time Line Contents
21402140
=======================================================
@@ -2933,7 +2933,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
29332933

29342934

29352935
<footer>
2936-
<aside>July 19, 2023</aside>
2936+
<aside>July 22, 2023</aside>
29372937
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
29382938
</footer>
29392939

0 commit comments

Comments
 (0)