From 9e40184dc6d6db57cf0ecfb0eb1ee3f6c995ba2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Wed, 10 Mar 2021 14:33:05 +0100 Subject: [PATCH] Toc, List, Threading, Web, Synthesizer, Pygame --- README.md | 31 ++++++++++++++++--------------- index.html | 35 ++++++++++++++++++----------------- parse.js | 4 ++-- 3 files changed, 36 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index dfb6fd8..1ea48ce 100644 --- a/README.md +++ b/README.md @@ -56,10 +56,10 @@ list_of_chars = list() * **Module [operator](#operator) provides functions itemgetter() and mul() that offer the same functionality as [lambda](#lambda) expressions above.** ```python +.insert(, ) # Inserts item at index and moves the rest to the right. + = .pop([]) # Returns and removes item at index or from the end. = .count() # Returns number of occurrences. Also works on strings. = .index() # Returns index of the first occurrence or raises ValueError. -.insert(, ) # Inserts item at index and moves the rest to the right. - = .pop([]) # Removes and returns item at index or from the end. .remove() # Removes first occurrence of the item or raises ValueError. .clear() # Removes all items. Also works on dictionary and set. ``` @@ -2087,7 +2087,7 @@ from concurrent.futures import ThreadPoolExecutor ```python = ThreadPoolExecutor(max_workers=None) # Or: `with ThreadPoolExecutor() as : …` -.shutdown(wait=True) # Cleans-up the resources associated with Exec. +.shutdown(wait=True) # Gets called at the end of 'with' block. ``` ```python @@ -2129,7 +2129,7 @@ sorted_by_second = sorted(, key=op.itemgetter(1)) sorted_by_both = sorted(, key=op.itemgetter(1, 0)) product_of_elems = functools.reduce(op.mul, ) union_of_sets = functools.reduce(op.or_, ) -LogicOp = enum.Enum('LogicOp', {'AND': op.and_, 'OR' : op.or_}) +LogicOp = enum.Enum('LogicOp', {'AND': op.and_, 'OR': op.or_}) last_el = op.methodcaller('pop')() ``` @@ -2511,8 +2511,9 @@ def odds_handler(sport): #### Test: ```python # $ pip3 install requests ->>> import requests ->>> url = 'http://localhost:8080/odds/football' +>>> import threading, requests +>>> threading.Thread(target=run, daemon=True).start() +>>> url = 'http://localhost:8080/odds/football' >>> data = {'team': 'arsenal f.c.'} >>> response = requests.post(url, data=data) >>> response.json() @@ -2838,7 +2839,7 @@ def read_wav_file(filename): with wave.open(filename, 'rb') as file: sampwidth = file.getsampwidth() frames = file.readframes(-1) - bytes_samples = (frames[i: i + sampwidth] for i in range(0, len(frames), sampwidth)) + bytes_samples = (frames[i : i+sampwidth] for i in range(0, len(frames), sampwidth)) return [get_int(b) / pow(2, sampwidth * 8 - 1) for b in bytes_samples] ``` @@ -2907,8 +2908,8 @@ get_pause = lambda seconds: repeat(0, int(seconds * F)) sin_f = lambda i, hz: math.sin(i * 2 * math.pi * hz / F) get_wave = lambda hz, seconds: (sin_f(i, hz) for i in range(int(seconds * F))) get_hz = lambda key: 8.176 * 2 ** (int(key) / 12) -parse_note = lambda note: (get_hz(note[:2]), 0.125 if '♪' in note else 0.25) -get_samples = lambda note: get_wave(*parse_note(note)) if note else get_pause(0.125) +parse_note = lambda note: (get_hz(note[:-1]), 1/4 if '♩' in note else 1/8) +get_samples = lambda note: get_wave(*parse_note(note)) if note else get_pause(1/8) samples_f = chain.from_iterable(get_samples(n) for n in f'{P1}{P1}{P2}'.split(',')) samples_b = b''.join(struct.pack(' = tr.scale(, (width, height)) # Returns scaled surface. - = tr.rotate(, degrees) # Returns rotated and scaled surface. - = tr.flip(, x_bool, y_bool) # Returns flipped surface. +from pygame.transform import * + = scale(, (width, height)) # Returns scaled surface. + = rotate(, degrees) # Returns rotated and scaled surface. + = flip(, x_bool, y_bool) # Returns flipped surface. ``` ```python -from pygame.draw import line, arc, rect +from pygame.draw import * line(, color, (x1, y1), (x2, y2), width) # Draws a line to the surface. arc(, color, , from_rad, to_rad) # Also: ellipse(, color, ) rect(, color, ) # Also: polygon(, color, points) @@ -3517,6 +3518,6 @@ if __name__ == '__main__': Index ----- -* **Only available in [PDF](https://transactions.sendowl.com/products/78175486/4422834F/view).** +* **Only available in the [PDF](https://transactions.sendowl.com/products/78175486/4422834F/view).** * **Ctrl+F / ⌘F is usually sufficient.** * **Searching `'#'` on a [webpage](https://gto76.github.io/python-cheatsheet/) will limit the search to the titles.** diff --git a/index.html b/index.html index e222b71..0acc544 100644 --- a/index.html +++ b/index.html @@ -232,7 +232,7 @@ pre.prettyprint { <a href="javascript:" id="return-to-top"><i class="icon-chevron-up"></i></a> <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>. -</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> = { +</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> = { <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>], <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>], <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,10 +275,10 @@ list_of_chars = list(<str>) <ul> <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> </ul> -<pre><code class="python language-python hljs"><int> = <list>.count(<el>) <span class="hljs-comment"># Returns number of occurrences. Also works on strings.</span> +<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> +<el> = <list>.pop([<int>]) <span class="hljs-comment"># Returns and removes item at index or from the end.</span> +<int> = <list>.count(<el>) <span class="hljs-comment"># Returns number of occurrences. Also works on strings.</span> <int> = <list>.index(<el>) <span class="hljs-comment"># Returns index of the first occurrence or raises ValueError.</span> -<list>.insert(<int>, <el>) <span class="hljs-comment"># Inserts item at index and moves the rest to the right.</span> -<el> = <list>.pop([<int>]) <span class="hljs-comment"># Removes and returns item at index or from the end.</span> <list>.remove(<el>) <span class="hljs-comment"># Removes first occurrence of the item or raises ValueError.</span> <list>.clear() <span class="hljs-comment"># Removes all items. Also works on dictionary and set.</span> </code></pre> @@ -1865,7 +1865,7 @@ CompletedProcess(args=[<span class="hljs-string">'bc'</span>, <span class="hljs- <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> -<Exec>.shutdown(wait=<span class="hljs-keyword">True</span>) <span class="hljs-comment"># Cleans-up the resources associated with Exec.</span> +<Exec>.shutdown(wait=<span class="hljs-keyword">True</span>) <span class="hljs-comment"># Gets called at the end of 'with' block.</span> </code></pre> <pre><code class="python language-python hljs"><iter> = <Exec>.map(<func>, <args_1>, ...) <span class="hljs-comment"># A multithreaded and non-lazy map().</span> <Futr> = <Exec>.submit(<func>, <arg_1>, ...) <span class="hljs-comment"># Starts a thread and returns its Future object.</span> @@ -1895,7 +1895,7 @@ sorted_by_second = sorted(<collection>, key=op.itemgetter(<span class="hlj sorted_by_both = sorted(<collection>, key=op.itemgetter(<span class="hljs-number">1</span>, <span class="hljs-number">0</span>)) product_of_elems = functools.reduce(op.mul, <collection>) union_of_sets = functools.reduce(op.or_, <coll_of_sets>) -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_}) +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_}) last_el = op.methodcaller(<span class="hljs-string">'pop'</span>)(<list>) </code></pre> <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,8 +2191,9 @@ run(host=<span class="hljs-string">'0.0.0.0'</span>, port=<span class="hljs-numb </code></pre></div> <div><h4 id="test">Test:</h4><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install requests</span> -<span class="hljs-meta">>>> </span><span class="hljs-keyword">import</span> requests -<span class="hljs-meta">>>> </span>url = <span class="hljs-string">'http://localhost:8080/odds/football'</span> +<span class="hljs-meta">>>> </span><span class="hljs-keyword">import</span> threading, requests +<span class="hljs-meta">>>> </span>threading.Thread(target=run, daemon=<span class="hljs-keyword">True</span>).start() +<span class="hljs-meta">>>> </span>url = <span class="hljs-string">'http://localhost:8080/odds/football'</span> <span class="hljs-meta">>>> </span>data = {<span class="hljs-string">'team'</span>: <span class="hljs-string">'arsenal f.c.'</span>} <span class="hljs-meta">>>> </span>response = requests.post(url, data=data) <span class="hljs-meta">>>> </span>response.json() @@ -2446,7 +2447,7 @@ nframes = <Wave_read>.getnframes() <span class="hljs-comment" <span class="hljs-keyword">with</span> wave.open(filename, <span class="hljs-string">'rb'</span>) <span class="hljs-keyword">as</span> file: sampwidth = file.getsampwidth() frames = file.readframes(<span class="hljs-number">-1</span>) - 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)) + 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)) <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] </code></pre></div> @@ -2500,8 +2501,8 @@ get_pause = <span class="hljs-keyword">lambda</span> seconds: repeat(<span cla sin_f = <span class="hljs-keyword">lambda</span> i, hz: math.sin(i * <span class="hljs-number">2</span> * math.pi * hz / F) 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))) 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>) -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>) -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>) +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>) +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>) 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>)) 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) simpleaudio.play_buffer(samples_b, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, F) @@ -2546,12 +2547,12 @@ rect = pg.Rect(<span class="hljs-number">240</span>, <span class="hljs-number">2 <Surf>.set_at((x, y), color) <span class="hljs-comment"># Updates pixel.</span> <Surf>.blit(<Surf>, (x, y)) <span class="hljs-comment"># Draws passed surface to the surface.</span> </code></pre> -<pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> pygame.transform <span class="hljs-keyword">as</span> tr -<Surf> = tr.scale(<Surf>, (width, height)) <span class="hljs-comment"># Returns scaled surface.</span> -<Surf> = tr.rotate(<Surf>, degrees) <span class="hljs-comment"># Returns rotated and scaled surface.</span> -<Surf> = tr.flip(<Surf>, x_bool, y_bool) <span class="hljs-comment"># Returns flipped surface.</span> +<pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> pygame.transform <span class="hljs-keyword">import</span> * +<Surf> = scale(<Surf>, (width, height)) <span class="hljs-comment"># Returns scaled surface.</span> +<Surf> = rotate(<Surf>, degrees) <span class="hljs-comment"># Returns rotated and scaled surface.</span> +<Surf> = flip(<Surf>, x_bool, y_bool) <span class="hljs-comment"># Returns flipped surface.</span> </code></pre> -<pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> pygame.draw <span class="hljs-keyword">import</span> line, arc, rect +<pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> pygame.draw <span class="hljs-keyword">import</span> * line(<Surf>, color, (x1, y1), (x2, y2), width) <span class="hljs-comment"># Draws a line to the surface.</span> arc(<Surf>, color, <Rect>, from_rad, to_rad) <span class="hljs-comment"># Also: ellipse(<Surf>, color, <Rect>)</span> rect(<Surf>, color, <Rect>) <span class="hljs-comment"># Also: polygon(<Surf>, color, points)</span> @@ -2998,7 +2999,7 @@ $ pyinstaller script.py --add-data '<path>:.' <span class="hljs-comment"> main() </code></pre></div> -<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> +<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> <li><strong>Ctrl+F / ⌘F is usually sufficient.</strong></li> <li><strong>Searching <code class="python hljs"><span class="hljs-string">'#<title>'</span></code> will limit the search to the titles.</strong></li> </ul></div> diff --git a/parse.js b/parse.js index 04999db..606524f 100755 --- a/parse.js +++ b/parse.js @@ -18,7 +18,7 @@ const hljs = require('highlightjs'); const TOC = '<br>' + '<h2 id="toc">Contents</h2>\n' + - '<pre><code class="hljs bash"><strong>ToC</strong> = {\n' + + '<pre><code class="hljs bash" style="line-height: 1.3em;"><strong>ToC</strong> = {\n' + ' <strong><span class="hljs-string">\'1. Collections\'</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>],\n' + ' <strong><span class="hljs-string">\'2. Types\'</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>],\n' + ' <strong><span class="hljs-string">\'3. Syntax\'</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>],\n' + @@ -96,7 +96,7 @@ const PYINSTALLER = '$ pyinstaller script.py --add-data \'<path>:.\' <span class="hljs-comment"># Adds file to the root of the executable.</span>\n'; const INDEX = - '<li><strong>Only available in <a href="https://transactions.sendowl.com/products/78175486/4422834F/view">PDF</a>.</strong></li>\n' + + '<li><strong>Only available in the <a href="https://transactions.sendowl.com/products/78175486/4422834F/view">PDF</a>.</strong></li>\n' + '<li><strong>Ctrl+F / ⌘F is usually sufficient.</strong></li>\n' + '<li><strong>Searching <code class="python hljs"><span class="hljs-string">\'#<title>\'</span></code> will limit the search to the titles.</strong></li>\n';