|
606 | 606 | </code></pre></div>
|
607 | 607 |
|
608 | 608 | <div><h3 id="math">Math</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> math <span class="hljs-keyword">import</span> e, pi, inf, nan, isinf, isnan
|
609 |
| -<span class="hljs-keyword">from</span> math <span class="hljs-keyword">import</span> cos, acos, sin, asin, tan, atan, degrees, radians |
| 609 | +<span class="hljs-keyword">from</span> math <span class="hljs-keyword">import</span> cos, sin, tan, acos, asin, atan, degrees, radians |
610 | 610 | <span class="hljs-keyword">from</span> math <span class="hljs-keyword">import</span> log, log10, log2
|
611 | 611 | </code></pre></div>
|
612 | 612 |
|
|
681 | 681 | <ul>
|
682 | 682 | <li><strong>Use <code class="python hljs"><span class="hljs-string">'<D/DT>.weekday()'</span></code> to get the day of the week (Mon == 0).</strong></li>
|
683 | 683 | <li><strong><code class="python hljs"><span class="hljs-string">'fold=1'</span></code> means the second pass in case of time jumping back for one hour.</strong></li>
|
684 |
| -<li><strong><code class="python hljs"><span class="hljs-string">'<DTa> = resolve_imaginary(<DTa>)'</span></code> fixes DTs that fall into the missing hour.</strong></li> |
| 684 | +<li><strong><code class="python hljs"><span class="hljs-string">'<DTa> = resolve_imaginary(<DTa>)'</span></code> fixes DTs that fall into missing hour.</strong></li> |
685 | 685 | </ul>
|
686 | 686 | <div><h3 id="now">Now</h3><pre><code class="python language-python hljs"><D/DTn> = D/DT.today() <span class="hljs-comment"># Current local date or naive datetime.</span>
|
687 | 687 | <DTn> = DT.utcnow() <span class="hljs-comment"># Naive datetime from current UTC time.</span>
|
|
1626 | 1626 | </code></pre></div>
|
1627 | 1627 |
|
1628 | 1628 | <ul>
|
1629 |
| -<li><strong>File must be opened with <code class="python hljs"><span class="hljs-string">'newline=""'</span></code> argument, or newlines embedded inside quoted fields will not be interpreted correctly!</strong></li> |
| 1629 | +<li><strong>File must be opened with a <code class="python hljs"><span class="hljs-string">'newline=""'</span></code> argument, or newlines embedded inside quoted fields will not be interpreted correctly!</strong></li> |
1630 | 1630 | </ul>
|
1631 | 1631 | <div><h3 id="write">Write</h3><pre><code class="python language-python hljs"><writer> = csv.writer(<file>) <span class="hljs-comment"># Also: `dialect='excel', delimiter=','`.</span>
|
1632 | 1632 | <writer>.writerow(<collection>) <span class="hljs-comment"># Encodes objects using `str(<el>)`.</span>
|
1633 | 1633 | <writer>.writerows(<coll_of_coll>) <span class="hljs-comment"># Appends multiple rows.</span>
|
1634 | 1634 | </code></pre></div>
|
1635 | 1635 |
|
1636 | 1636 | <ul>
|
1637 |
| -<li><strong>File must be opened with <code class="python hljs"><span class="hljs-string">'newline=""'</span></code> argument, or '\r' will be added in front of every '\n' on platforms that use '\r\n' line endings!</strong></li> |
| 1637 | +<li><strong>File must be opened with a <code class="python hljs"><span class="hljs-string">'newline=""'</span></code> argument, or '\r' will be added in front of every '\n' on platforms that use '\r\n' line endings!</strong></li> |
1638 | 1638 | </ul>
|
1639 | 1639 | <div><h3 id="parameters">Parameters</h3><ul>
|
1640 | 1640 | <li><strong><code class="python hljs"><span class="hljs-string">'dialect'</span></code> - Master parameter that sets the default values.</strong></li>
|
|
1646 | 1646 | <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>
|
1647 | 1647 | <li><strong><code class="python hljs"><span class="hljs-string">'escapechar'</span></code> - Character for escaping 'quotechar' if 'doublequote' is False.</strong></li>
|
1648 | 1648 | </ul><div><h3 id="dialects">Dialects</h3><pre><code class="text language-text">┏━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━┓
|
1649 |
| -┃ │ excel │ excel-tab │ unix ┃ |
| 1649 | +┃ │ excel │ excel-tab │ unix ┃ |
1650 | 1650 | ┠──────────────────┼──────────────┼──────────────┼──────────────┨
|
1651 |
| -┃ delimiter │ ',' │ '\t' │ ',' ┃ |
1652 |
| -┃ quotechar │ '"' │ '"' │ '"' ┃ |
1653 |
| -┃ doublequote │ True │ True │ True ┃ |
1654 |
| -┃ skipinitialspace │ False │ False │ False ┃ |
1655 |
| -┃ lineterminator │ '\r\n' │ '\r\n' │ '\n' ┃ |
1656 |
| -┃ quoting │ 0 │ 0 │ 1 ┃ |
1657 |
| -┃ escapechar │ None │ None │ None ┃ |
| 1651 | +┃ delimiter │ ',' │ '\t' │ ',' ┃ |
| 1652 | +┃ quotechar │ '"' │ '"' │ '"' ┃ |
| 1653 | +┃ doublequote │ True │ True │ True ┃ |
| 1654 | +┃ skipinitialspace │ False │ False │ False ┃ |
| 1655 | +┃ lineterminator │ '\r\n' │ '\r\n' │ '\n' ┃ |
| 1656 | +┃ quoting │ 0 │ 0 │ 1 ┃ |
| 1657 | +┃ escapechar │ None │ None │ None ┃ |
1658 | 1658 | ┗━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━┛
|
1659 | 1659 | </code></pre></div></div>
|
1660 | 1660 |
|
|
1703 | 1703 |
|
1704 | 1704 |
|
1705 | 1705 | <div><h3 id="example">Example</h3><p><strong>In this example values are not actually saved because <code class="python hljs"><span class="hljs-string">'conn.commit()'</span></code> is omitted!</strong></p><pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>conn = sqlite3.connect(<span class="hljs-string">'test.db'</span>)
|
1706 |
| -<span class="hljs-meta">>>> </span>conn.execute(<span class="hljs-string">'create table person (person_id integer primary key, name, height)'</span>) |
1707 |
| -<span class="hljs-meta">>>> </span>conn.execute(<span class="hljs-string">'insert into person values (null, ?, ?)'</span>, (<span class="hljs-string">'Jean-Luc'</span>, <span class="hljs-number">187</span>)).lastrowid |
| 1706 | +<span class="hljs-meta">>>> </span>conn.execute(<span class="hljs-string">'CREATE TABLE person (person_id INTEGER PRIMARY KEY, name, height)'</span>) |
| 1707 | +<span class="hljs-meta">>>> </span>conn.execute(<span class="hljs-string">'INSERT INTO person VALUES (NULL, ?, ?)'</span>, (<span class="hljs-string">'Jean-Luc'</span>, <span class="hljs-number">187</span>)).lastrowid |
1708 | 1708 | <span class="hljs-number">1</span>
|
1709 |
| -<span class="hljs-meta">>>> </span>conn.execute(<span class="hljs-string">'select * from person'</span>).fetchall() |
| 1709 | +<span class="hljs-meta">>>> </span>conn.execute(<span class="hljs-string">'SELECT * FROM person'</span>).fetchall() |
1710 | 1710 | [(<span class="hljs-number">1</span>, <span class="hljs-string">'Jean-Luc'</span>, <span class="hljs-number">187</span>)]
|
1711 | 1711 | </code></pre></div>
|
1712 | 1712 |
|
|
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"># Gets called at the end of 'with' block.</span> |
| 1868 | +<Exec>.shutdown(wait=<span class="hljs-keyword">True</span>) <span class="hljs-comment"># Blocks until all threads finish executing.</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>
|
|
2214 | 2214 | </code></pre></div>
|
2215 | 2215 |
|
2216 | 2216 | <div><h3 id="timingasnippet">Timing a Snippet</h3><pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> timeit <span class="hljs-keyword">import</span> timeit
|
2217 |
| -<span class="hljs-meta">>>> </span>timeit(<span class="hljs-string">'"".join(str(i) for i in range(100))'</span>, |
| 2217 | +<span class="hljs-meta">>>> </span>timeit(<span class="hljs-string">"''.join(str(i) for i in range(100))"</span>, |
2218 | 2218 | <span class="hljs-meta">... </span> number=<span class="hljs-number">10000</span>, globals=globals(), setup=<span class="hljs-string">'pass'</span>)
|
2219 | 2219 | <span class="hljs-number">0.34986</span>
|
2220 | 2220 | </code></pre></div>
|
|
2275 | 2275 | <li><strong>Shape is a tuple of dimension sizes.</strong></li>
|
2276 | 2276 | <li><strong>Axis is an index of the dimension that gets collapsed. Leftmost dimension has index 0.</strong></li>
|
2277 | 2277 | </ul>
|
2278 |
| -<div><h3 id="indexing">Indexing</h3><pre><code class="python language-python hljs"><el> = <2d_array>[<span class="hljs-number">0</span>, <span class="hljs-number">0</span>] <span class="hljs-comment"># First element.</span> |
2279 |
| -<1d_view> = <2d_array>[<span class="hljs-number">0</span>] <span class="hljs-comment"># First row.</span> |
2280 |
| -<1d_view> = <2d_array>[:, <span class="hljs-number">0</span>] <span class="hljs-comment"># First column. Also [..., 0].</span> |
2281 |
| -<3d_view> = <2d_array>[<span class="hljs-keyword">None</span>, :, :] <span class="hljs-comment"># Expanded by dimension of size 1.</span> |
| 2278 | +<div><h3 id="indexing">Indexing</h3><pre><code class="bash language-bash hljs"><el> = <2d_array>[row_index, column_index] |
| 2279 | +<1d_view> = <2d_array>[row_index] |
| 2280 | +<1d_view> = <2d_array>[:, column_index] |
2282 | 2281 | </code></pre></div>
|
2283 | 2282 |
|
2284 |
| -<pre><code class="python language-python hljs"><1d_array> = <2d_array>[<1d_row_indexes>, <1d_column_indexes>] |
2285 |
| -<2d_array> = <2d_array>[<2d_row_indexes>, <2d_column_indexes>] |
| 2283 | +<pre><code class="bash language-bash hljs"><1d_array> = <2d_array>[row_indexes, column_indexes] |
| 2284 | +<2d_array> = <2d_array>[row_indexes] |
| 2285 | +<2d_array> = <2d_array>[:, column_indexes] |
2286 | 2286 | </code></pre>
|
2287 |
| -<pre><code class="python language-python hljs"><2d_bools> = <2d_array> > <span class="hljs-number">0</span> |
| 2287 | +<pre><code class="bash language-bash hljs"><2d_bools> = <2d_array> ><== <el> |
2288 | 2288 | <1d_array> = <2d_array>[<2d_bools>]
|
2289 | 2289 | </code></pre>
|
2290 |
| -<ul> |
2291 |
| -<li><strong>If row and column indexes differ in shape, they are combined with broadcasting.</strong></li> |
2292 |
| -</ul> |
2293 | 2290 | <div><h3 id="broadcasting">Broadcasting</h3><p><strong>Broadcasting is a set of rules by which NumPy functions operate on arrays of different sizes and/or dimensions.</strong></p><pre><code class="python language-python hljs">left = [[<span class="hljs-number">0.1</span>], [<span class="hljs-number">0.6</span>], [<span class="hljs-number">0.8</span>]] <span class="hljs-comment"># Shape: (3, 1)</span>
|
2294 | 2291 | right = [ <span class="hljs-number">0.1</span> , <span class="hljs-number">0.6</span> , <span class="hljs-number">0.8</span> ] <span class="hljs-comment"># Shape: (3)</span>
|
2295 | 2292 | </code></pre></div>
|
|
2492 | 2489 | </code></pre></div>
|
2493 | 2490 |
|
2494 | 2491 | <div><h2 id="synthesizer"><a href="#synthesizer" name="synthesizer">#</a>Synthesizer</h2><div><h4 id="playspopcornbygershonkingsley">Plays Popcorn by Gershon Kingsley:</h4><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install simpleaudio</span>
|
2495 |
| -<span class="hljs-keyword">import</span> simpleaudio, math, struct |
2496 |
| -<span class="hljs-keyword">from</span> itertools <span class="hljs-keyword">import</span> chain, repeat |
| 2492 | +<span class="hljs-keyword">import</span> math, struct, simpleaudio |
| 2493 | +<span class="hljs-keyword">from</span> itertools <span class="hljs-keyword">import</span> repeat, chain |
2497 | 2494 | F = <span class="hljs-number">44100</span>
|
2498 | 2495 | P1 = <span class="hljs-string">'71♩,69♪,,71♩,66♪,,62♩,66♪,,59♩,,,'</span>
|
2499 | 2496 | P2 = <span class="hljs-string">'71♩,73♪,,74♩,73♪,,74♪,,71♪,,73♩,71♪,,73♪,,69♪,,71♩,69♪,,71♪,,67♪,,71♩,,,'</span>
|
|
0 commit comments