|
54 | 54 |
|
55 | 55 | <body>
|
56 | 56 | <header>
|
57 |
| - <aside>July 19, 2023</aside> |
| 57 | + <aside>July 22, 2023</aside> |
58 | 58 | <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
|
59 | 59 | </header>
|
60 | 60 |
|
|
903 | 903 | <class> = make_dataclass(<span class="hljs-string">'<class_name>'</span>, <coll_of_tuples>)
|
904 | 904 | <tuple> = (<span class="hljs-string">'<attr_name>'</span>, <type> [, <default_value>])</code></pre></div>
|
905 | 905 |
|
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 |
907 | 907 | <var_name>: list/set/abc.Iterable/abc.Sequence/tp.Optional[<type>] [= <obj>]
|
908 | 908 | <var_name>: dict/tuple/tp.Union[<type>, ...] [= <obj>]
|
909 | 909 | <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">func</span><span class="hljs-params">(<arg_name>: <type> [= <obj>])</span> -> <type>:</span> ...
|
|
1067 | 1067 | <div><h3 id="collection">Collection</h3><ul>
|
1068 | 1068 | <li><strong>Only required methods are iter() and len(). Len() should return the number of items.</strong></li>
|
1069 | 1069 | <li><strong>This cheatsheet actually means <code class="python hljs"><span class="hljs-string">'<iterable>'</span></code> when it uses <code class="python hljs"><span class="hljs-string">'<collection>'</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> |
1071 | 1071 | </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>
|
1072 | 1072 | <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, a)</span>:</span>
|
1073 | 1073 | self.a = a
|
|
1371 | 1371 | <file>.flush() <span class="hljs-comment"># Flushes write buffer. Runs every 4096/8192 B.</span>
|
1372 | 1372 | </code></pre>
|
1373 | 1373 | <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> |
1375 | 1375 | </ul>
|
1376 | 1376 | <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>
|
1377 | 1377 | <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
|
1697 | 1697 |
|
1698 | 1698 |
|
1699 | 1699 | <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> |
1701 | 1701 | <li><strong>Each element can reference a single or multiple consecutive bytes, depending on format.</strong></li>
|
1702 | 1702 | <li><strong>Order and number of elements can be changed with slicing.</strong></li>
|
1703 | 1703 | <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
|
1801 | 1801 | first_element = op.methodcaller(<span class="hljs-string">'pop'</span>, <span class="hljs-number">0</span>)(<list>)
|
1802 | 1802 | </code></pre>
|
1803 | 1803 | <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> |
1805 | 1805 | <li><strong>Also: <code class="python hljs"><span class="hljs-string">'<bool> = <bool> &|^ <bool>'</span></code> and <code class="python hljs"><span class="hljs-string">'<int> = <bool> &|^ <int>'</span></code>.</strong></li>
|
1806 | 1806 | </ul>
|
1807 | 1807 | <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"><list> = 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
|
1852 | 1852 | <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>
|
1853 | 1853 | <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>
|
1854 | 1854 | </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> |
1856 | 1856 | b = <span class="hljs-number">12345</span>
|
1857 | 1857 | </code></pre></div>
|
1858 | 1858 |
|
@@ -2080,7 +2080,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
|
2080 | 2080 |
|
2081 | 2081 |
|
2082 | 2082 | <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>) |
2084 | 2084 | </code></pre>
|
2085 | 2085 | <ul>
|
2086 | 2086 | <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
|
2130 | 2130 | </code></pre></div>
|
2131 | 2131 |
|
2132 | 2132 | <div><h3 id="profilingbyline">Profiling by Line</h3><pre><code class="text language-text">$ pip3 install line_profiler
|
2133 |
| -$ echo "@profile |
| 2133 | +$ echo '@profile |
2134 | 2134 | def main():
|
2135 | 2135 | a = list(range(10000))
|
2136 | 2136 | b = set(range(10000))
|
2137 |
| -main()" > test.py |
| 2137 | +main()' > test.py |
2138 | 2138 | $ kernprof -lv test.py
|
2139 | 2139 | Line # Hits Time Per Hit % Time Line Contents
|
2140 | 2140 | =======================================================
|
@@ -2933,7 +2933,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
|
2933 | 2933 |
|
2934 | 2934 |
|
2935 | 2935 | <footer>
|
2936 |
| - <aside>July 19, 2023</aside> |
| 2936 | + <aside>July 22, 2023</aside> |
2937 | 2937 | <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
|
2938 | 2938 | </footer>
|
2939 | 2939 |
|
|
0 commit comments