|
232 | 232 |
|
233 | 233 | <a href="javascript:" id="return-to-top"><i class="icon-chevron-up"></i></a>
|
234 | 234 | <div><h1 id="comprehensivepythoncheatsheet">Comprehensive Python Cheatsheet</h1><p class="banner"><sup><a href="https://raw.githubusercontent.com/gto76/python-cheatsheet/master/README.md">Download text file</a>, <a href="https://transactions.sendowl.com/products/78175486/4422834F/view">Buy PDF</a>, <a href="https://github.com/gto76/python-cheatsheet">Fork me on GitHub</a> or <a href="https://github.com/gto76/python-cheatsheet/wiki/Frequently-Asked-Questions">Check out FAQ</a>.
|
235 |
| -</sup></p><p class="banner"><img src="web/image_888.jpeg" alt="Monty Python"></p><br><div><h2 id="toc"><a href="#toc" name="toc">#</a>Contents</h2><pre><code class="hljs bash"><strong>ToC</strong> = { |
| 235 | +</sup></p><p class="banner"><img src="web/image_888.jpeg" alt="Monty Python"></p><br><div><h2 id="toc"><a href="#toc" name="toc">#</a>Contents</h2><pre><code class="hljs bash" style="line-height: 1.3em;"><strong>ToC</strong> = { |
236 | 236 | <strong><span class="hljs-string"><span class="hljs-string">'1. Collections'</span></span></strong>: [<a href="#list">List</a>, <a href="#dictionary">Dictionary</a>, <a href="#set">Set</a>, <a href="#tuple">Tuple</a>, <a href="#range">Range</a>, <a href="#enumerate">Enumerate</a>, <a href="#iterator">Iterator</a>, <a href="#generator">Generator</a>],
|
237 | 237 | <strong><span class="hljs-string"><span class="hljs-string">'2. Types'</span></span></strong>: [<a href="#type">Type</a>, <a href="#string">String</a>, <a href="#regex">Regular_Exp</a>, <a href="#format">Format</a>, <a href="#numbers">Numbers</a>, <a href="#combinatorics">Combinatorics</a>, <a href="#datetime">Datetime</a>],
|
238 | 238 | <strong><span class="hljs-string"><span class="hljs-string">'3. Syntax'</span></span></strong>: [<a href="#arguments">Args</a>, <a href="#inline">Inline</a>, <a href="#closure">Closure</a>, <a href="#decorator">Decorator</a>, <a href="#class">Class</a>, <a href="#ducktypes">Duck_Type</a>, <a href="#enum">Enum</a>, <a href="#exceptions">Exception</a>],
|
|
275 | 275 | <ul>
|
276 | 276 | <li><strong>Module <a href="#operator">operator</a> provides functions itemgetter() and mul() that offer the same functionality as <a href="#lambda">lambda</a> expressions above.</strong></li>
|
277 | 277 | </ul>
|
278 |
| -<pre><code class="python language-python hljs"><int> = <list>.count(<el>) <span class="hljs-comment"># Returns number of occurrences. Also works on strings.</span> |
| 278 | +<pre><code class="python language-python hljs"><list>.insert(<int>, <el>) <span class="hljs-comment"># Inserts item at index and moves the rest to the right.</span> |
| 279 | +<el> = <list>.pop([<int>]) <span class="hljs-comment"># Returns and removes item at index or from the end.</span> |
| 280 | +<int> = <list>.count(<el>) <span class="hljs-comment"># Returns number of occurrences. Also works on strings.</span> |
279 | 281 | <int> = <list>.index(<el>) <span class="hljs-comment"># Returns index of the first occurrence or raises ValueError.</span>
|
280 |
| -<list>.insert(<int>, <el>) <span class="hljs-comment"># Inserts item at index and moves the rest to the right.</span> |
281 |
| -<el> = <list>.pop([<int>]) <span class="hljs-comment"># Removes and returns item at index or from the end.</span> |
282 | 282 | <list>.remove(<el>) <span class="hljs-comment"># Removes first occurrence of the item or raises ValueError.</span>
|
283 | 283 | <list>.clear() <span class="hljs-comment"># Removes all items. Also works on dictionary and set.</span>
|
284 | 284 | </code></pre>
|
|
1865 | 1865 |
|
1866 | 1866 |
|
1867 | 1867 | <pre><code class="python language-python hljs"><Exec> = ThreadPoolExecutor(max_workers=<span class="hljs-keyword">None</span>) <span class="hljs-comment"># Or: `with ThreadPoolExecutor() as <name>: …`</span>
|
1868 |
| -<Exec>.shutdown(wait=<span class="hljs-keyword">True</span>) <span class="hljs-comment"># Cleans-up the resources associated with Exec.</span> |
| 1868 | +<Exec>.shutdown(wait=<span class="hljs-keyword">True</span>) <span class="hljs-comment"># Gets called at the end of 'with' block.</span> |
1869 | 1869 | </code></pre>
|
1870 | 1870 | <pre><code class="python language-python hljs"><iter> = <Exec>.map(<func>, <args_1>, ...) <span class="hljs-comment"># A multithreaded and non-lazy map().</span>
|
1871 | 1871 | <Futr> = <Exec>.submit(<func>, <arg_1>, ...) <span class="hljs-comment"># Starts a thread and returns its Future object.</span>
|
|
1895 | 1895 | sorted_by_both = sorted(<collection>, key=op.itemgetter(<span class="hljs-number">1</span>, <span class="hljs-number">0</span>))
|
1896 | 1896 | product_of_elems = functools.reduce(op.mul, <collection>)
|
1897 | 1897 | union_of_sets = functools.reduce(op.or_, <coll_of_sets>)
|
1898 |
| -LogicOp = enum.Enum(<span class="hljs-string">'LogicOp'</span>, {<span class="hljs-string">'AND'</span>: op.and_, <span class="hljs-string">'OR'</span> : op.or_}) |
| 1898 | +LogicOp = enum.Enum(<span class="hljs-string">'LogicOp'</span>, {<span class="hljs-string">'AND'</span>: op.and_, <span class="hljs-string">'OR'</span>: op.or_}) |
1899 | 1899 | last_el = op.methodcaller(<span class="hljs-string">'pop'</span>)(<list>)
|
1900 | 1900 | </code></pre>
|
1901 | 1901 | <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>
|
|
2191 | 2191 | </code></pre></div>
|
2192 | 2192 |
|
2193 | 2193 | <div><h4 id="test">Test:</h4><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install requests</span>
|
2194 |
| -<span class="hljs-meta">>>> </span><span class="hljs-keyword">import</span> requests |
2195 |
| -<span class="hljs-meta">>>> </span>url = <span class="hljs-string">'http://localhost:8080/odds/football'</span> |
| 2194 | +<span class="hljs-meta">>>> </span><span class="hljs-keyword">import</span> threading, requests |
| 2195 | +<span class="hljs-meta">>>> </span>threading.Thread(target=run, daemon=<span class="hljs-keyword">True</span>).start() |
| 2196 | +<span class="hljs-meta">>>> </span>url = <span class="hljs-string">'http://localhost:8080/odds/football'</span> |
2196 | 2197 | <span class="hljs-meta">>>> </span>data = {<span class="hljs-string">'team'</span>: <span class="hljs-string">'arsenal f.c.'</span>}
|
2197 | 2198 | <span class="hljs-meta">>>> </span>response = requests.post(url, data=data)
|
2198 | 2199 | <span class="hljs-meta">>>> </span>response.json()
|
|
2446 | 2447 | <span class="hljs-keyword">with</span> wave.open(filename, <span class="hljs-string">'rb'</span>) <span class="hljs-keyword">as</span> file:
|
2447 | 2448 | sampwidth = file.getsampwidth()
|
2448 | 2449 | frames = file.readframes(<span class="hljs-number">-1</span>)
|
2449 |
| - bytes_samples = (frames[i: i + sampwidth] <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">0</span>, len(frames), sampwidth)) |
| 2450 | + bytes_samples = (frames[i : i+sampwidth] <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">0</span>, len(frames), sampwidth)) |
2450 | 2451 | <span class="hljs-keyword">return</span> [get_int(b) / pow(<span class="hljs-number">2</span>, sampwidth * <span class="hljs-number">8</span> - <span class="hljs-number">1</span>) <span class="hljs-keyword">for</span> b <span class="hljs-keyword">in</span> bytes_samples]
|
2451 | 2452 | </code></pre></div>
|
2452 | 2453 |
|
|
2500 | 2501 | sin_f = <span class="hljs-keyword">lambda</span> i, hz: math.sin(i * <span class="hljs-number">2</span> * math.pi * hz / F)
|
2501 | 2502 | get_wave = <span class="hljs-keyword">lambda</span> hz, seconds: (sin_f(i, hz) <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(int(seconds * F)))
|
2502 | 2503 | get_hz = <span class="hljs-keyword">lambda</span> key: <span class="hljs-number">8.176</span> * <span class="hljs-number">2</span> ** (int(key) / <span class="hljs-number">12</span>)
|
2503 |
| -parse_note = <span class="hljs-keyword">lambda</span> note: (get_hz(note[:<span class="hljs-number">2</span>]), <span class="hljs-number">0.125</span> <span class="hljs-keyword">if</span> <span class="hljs-string">'♪'</span> <span class="hljs-keyword">in</span> note <span class="hljs-keyword">else</span> <span class="hljs-number">0.25</span>) |
2504 |
| -get_samples = <span class="hljs-keyword">lambda</span> note: get_wave(*parse_note(note)) <span class="hljs-keyword">if</span> note <span class="hljs-keyword">else</span> get_pause(<span class="hljs-number">0.125</span>) |
| 2504 | +parse_note = <span class="hljs-keyword">lambda</span> note: (get_hz(note[:<span class="hljs-number">-1</span>]), <span class="hljs-number">1</span>/<span class="hljs-number">4</span> <span class="hljs-keyword">if</span> <span class="hljs-string">'♩'</span> <span class="hljs-keyword">in</span> note <span class="hljs-keyword">else</span> <span class="hljs-number">1</span>/<span class="hljs-number">8</span>) |
| 2505 | +get_samples = <span class="hljs-keyword">lambda</span> note: get_wave(*parse_note(note)) <span class="hljs-keyword">if</span> note <span class="hljs-keyword">else</span> get_pause(<span class="hljs-number">1</span>/<span class="hljs-number">8</span>) |
2505 | 2506 | samples_f = chain.from_iterable(get_samples(n) <span class="hljs-keyword">for</span> n <span class="hljs-keyword">in</span> <span class="hljs-string">f'<span class="hljs-subst">{P1}</span><span class="hljs-subst">{P1}</span><span class="hljs-subst">{P2}</span>'</span>.split(<span class="hljs-string">','</span>))
|
2506 | 2507 | samples_b = <span class="hljs-string">b''</span>.join(struct.pack(<span class="hljs-string">'<h'</span>, int(f * <span class="hljs-number">30000</span>)) <span class="hljs-keyword">for</span> f <span class="hljs-keyword">in</span> samples_f)
|
2507 | 2508 | simpleaudio.play_buffer(samples_b, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, F)
|
|
2546 | 2547 | <Surf>.set_at((x, y), color) <span class="hljs-comment"># Updates pixel.</span>
|
2547 | 2548 | <Surf>.blit(<Surf>, (x, y)) <span class="hljs-comment"># Draws passed surface to the surface.</span>
|
2548 | 2549 | </code></pre>
|
2549 |
| -<pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> pygame.transform <span class="hljs-keyword">as</span> tr |
2550 |
| -<Surf> = tr.scale(<Surf>, (width, height)) <span class="hljs-comment"># Returns scaled surface.</span> |
2551 |
| -<Surf> = tr.rotate(<Surf>, degrees) <span class="hljs-comment"># Returns rotated and scaled surface.</span> |
2552 |
| -<Surf> = tr.flip(<Surf>, x_bool, y_bool) <span class="hljs-comment"># Returns flipped surface.</span> |
| 2550 | +<pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> pygame.transform <span class="hljs-keyword">import</span> * |
| 2551 | +<Surf> = scale(<Surf>, (width, height)) <span class="hljs-comment"># Returns scaled surface.</span> |
| 2552 | +<Surf> = rotate(<Surf>, degrees) <span class="hljs-comment"># Returns rotated and scaled surface.</span> |
| 2553 | +<Surf> = flip(<Surf>, x_bool, y_bool) <span class="hljs-comment"># Returns flipped surface.</span> |
2553 | 2554 | </code></pre>
|
2554 |
| -<pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> pygame.draw <span class="hljs-keyword">import</span> line, arc, rect |
| 2555 | +<pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> pygame.draw <span class="hljs-keyword">import</span> * |
2555 | 2556 | line(<Surf>, color, (x1, y1), (x2, y2), width) <span class="hljs-comment"># Draws a line to the surface.</span>
|
2556 | 2557 | arc(<Surf>, color, <Rect>, from_rad, to_rad) <span class="hljs-comment"># Also: ellipse(<Surf>, color, <Rect>)</span>
|
2557 | 2558 | rect(<Surf>, color, <Rect>) <span class="hljs-comment"># Also: polygon(<Surf>, color, points)</span>
|
|
2998 | 2999 | main()
|
2999 | 3000 | </code></pre></div>
|
3000 | 3001 |
|
3001 |
| -<div><h2 id="index"><a href="#index" name="index">#</a>Index</h2><ul><li><strong>Only available in <a href="https://transactions.sendowl.com/products/78175486/4422834F/view">PDF</a>.</strong></li> |
| 3002 | +<div><h2 id="index"><a href="#index" name="index">#</a>Index</h2><ul><li><strong>Only available in the <a href="https://transactions.sendowl.com/products/78175486/4422834F/view">PDF</a>.</strong></li> |
3002 | 3003 | <li><strong>Ctrl+F / ⌘F is usually sufficient.</strong></li>
|
3003 | 3004 | <li><strong>Searching <code class="python hljs"><span class="hljs-string">'#<title>'</span></code> will limit the search to the titles.</strong></li>
|
3004 | 3005 | </ul></div>
|
|
0 commit comments