|
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>],
|
239 | 239 | <strong><span class="hljs-string"><span class="hljs-string">'4. System'</span></span></strong>: [<a href="#exit">Exit</a>, <a href="#print">Print</a>, <a href="#input">Input</a>, <a href="#commandlinearguments">Command_Line_Arguments</a>, <a href="#open">Open</a>, <a href="#paths">Path</a>, <a href="#oscommands">OS_Commands</a>],
|
240 | 240 | <strong><span class="hljs-string"><span class="hljs-string">'5. Data'</span></span></strong>: [<a href="#json">JSON</a>, <a href="#pickle">Pickle</a>, <a href="#csv">CSV</a>, <a href="#sqlite">SQLite</a>, <a href="#bytes">Bytes</a>, <a href="#struct">Struct</a>, <a href="#array">Array</a>, <a href="#memoryview">Memory_View</a>, <a href="#deque">Deque</a>],
|
241 |
| - <strong><span class="hljs-string"><span class="hljs-string">'6. Advanced'</span></span></strong>: [<a href="#threading">Threading</a>, <a href="#operator">Operator</a>, <a href="#introspection">Introspection</a>, <a href="#metaprograming">Metaprograming</a>, <a href="#eval">Eval</a>, <a href="#coroutines">Coroutine</a>], |
| 241 | + <strong><span class="hljs-string"><span class="hljs-string">'6. Advanced'</span></span></strong>: [<a href="#threading">Threading</a>, <a href="#operator">Operator</a>, <a href="#introspection">Introspection</a>, <a href="#metaprogramming">Metaprograming</a>, <a href="#eval">Eval</a>, <a href="#coroutines">Coroutine</a>], |
242 | 242 | <strong><span class="hljs-string"><span class="hljs-string">'7. Libraries'</span></span></strong>: [<a href="#progressbar">Progress_Bar</a>, <a href="#plot">Plot</a>, <a href="#table">Table</a>, <a href="#curses">Curses</a>, <a href="#logging">Logging</a>, <a href="#scraping">Scraping</a>, <a href="#web">Web</a>, <a href="#profiling">Profile</a>,
|
243 | 243 | <a href="#numpy">NumPy</a>, <a href="#image">Image</a>, <a href="#audio">Audio</a>, <a href="#pygame">Games</a>, <a href="#pandas">Data</a>]
|
244 | 244 | }
|
|
1556 | 1556 |
|
1557 | 1557 | <pre><code class="python language-python hljs">os.chdir(<path>) <span class="hljs-comment"># Changes the current working directory.</span>
|
1558 | 1558 | os.mkdir(<path>, mode=<span class="hljs-number">0o777</span>) <span class="hljs-comment"># Creates a directory. Mode is in octal.</span>
|
| 1559 | +os.makedirs(<path>, mode=<span class="hljs-number">0o777</span>) <span class="hljs-comment"># Creates all directories in the path.</span> |
1559 | 1560 | </code></pre>
|
1560 | 1561 | <pre><code class="python language-python hljs">shutil.copy(from, to) <span class="hljs-comment"># Copies the file. 'to' can exist or be a dir.</span>
|
1561 | 1562 | shutil.copytree(from, to) <span class="hljs-comment"># Copies the directory. 'to' must not exist.</span>
|
|
1686 | 1687 |
|
1687 | 1688 |
|
1688 | 1689 | <div><h3 id="write-1">Write</h3><pre><code class="python language-python hljs"><conn>.execute(<span class="hljs-string">'<query>'</span>) <span class="hljs-comment"># Can raise a subclass of sqlite3.Error.</span>
|
1689 |
| -<conn>.commit() <span class="hljs-comment"># Commits all transactions since last commit.</span> |
| 1690 | +<conn>.commit() <span class="hljs-comment"># Saves all changes since the last commit.</span> |
| 1691 | +<conn>.rollback() <span class="hljs-comment"># Discards all changes since the last commit.</span> |
1690 | 1692 | </code></pre></div>
|
1691 | 1693 |
|
1692 |
| -<div><h4 id="or">Or:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">with</span> <conn>: |
1693 |
| - <conn>.execute(<span class="hljs-string">'<query>'</span>) |
| 1694 | +<div><h4 id="or">Or:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">with</span> <conn>: <span class="hljs-comment"># Exits block with commit() or rollback(),</span> |
| 1695 | + <conn>.execute(<span class="hljs-string">'<query>'</span>) <span class="hljs-comment"># depending on whether an exception occurred.</span> |
1694 | 1696 | </code></pre></div>
|
1695 | 1697 |
|
1696 | 1698 | <div><h3 id="placeholders">Placeholders</h3><ul>
|
|
1724 | 1726 | <div><h2 id="bytes"><a href="#bytes" name="bytes">#</a>Bytes</h2><p><strong>Bytes object is an immutable sequence of single bytes. Mutable version is called bytearray.</strong></p><pre><code class="python language-python hljs"><bytes> = <span class="hljs-string">b'<str>'</span> <span class="hljs-comment"># Only accepts ASCII characters and \x00-\xff.</span>
|
1725 | 1727 | <int> = <bytes>[<index>] <span class="hljs-comment"># Returns int in range from 0 to 255.</span>
|
1726 | 1728 | <bytes> = <bytes>[<slice>] <span class="hljs-comment"># Returns bytes even if it has only one element.</span>
|
1727 |
| -<bytes> = <bytes>.join(<coll_of_bytes>) <span class="hljs-comment"># Joins elements using bytes object as separator.</span> |
| 1729 | +<bytes> = <bytes>.join(<coll_of_bytes>) <span class="hljs-comment"># Joins elements using bytes as a separator.</span> |
1728 | 1730 | </code></pre></div>
|
1729 | 1731 |
|
1730 | 1732 |
|
|
1761 | 1763 | <tuple> = unpack(<span class="hljs-string">'<format>'</span>, <bytes>)
|
1762 | 1764 | <tuples> = iter_unpack(<span class="hljs-string">'<format>'</span>, <bytes>)
|
1763 | 1765 | </code></pre>
|
1764 |
| -<pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>pack(<span class="hljs-string">'>hhl'</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>) |
| 1766 | +<div><h3 id="example-1">Example</h3><pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>pack(<span class="hljs-string">'>hhl'</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>) |
1765 | 1767 | <span class="hljs-string">b'\x00\x01\x00\x02\x00\x00\x00\x03'</span>
|
1766 | 1768 | <span class="hljs-meta">>>> </span>unpack(<span class="hljs-string">'>hhl'</span>, <span class="hljs-string">b'\x00\x01\x00\x02\x00\x00\x00\x03'</span>)
|
1767 | 1769 | (<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>)
|
1768 |
| -</code></pre> |
| 1770 | +</code></pre></div> |
| 1771 | + |
1769 | 1772 | <div><h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesstartformatstringwith">For standard type sizes start format string with:</h4><ul>
|
1770 | 1773 | <li><strong><code class="python hljs"><span class="hljs-string">'='</span></code> - native byte order (usually little-endian)</strong></li>
|
1771 | 1774 | <li><strong><code class="python hljs"><span class="hljs-string">'<'</span></code> - little-endian</strong></li>
|
|
1921 | 1924 | <memb> = <Param>.kind <span class="hljs-comment"># Member of ParameterKind enum.</span>
|
1922 | 1925 | </code></pre></div>
|
1923 | 1926 |
|
1924 |
| -<div><h2 id="metaprograming"><a href="#metaprograming" name="metaprograming">#</a>Metaprograming</h2><p><strong>Code that generates code.</strong></p><div><h3 id="type-1">Type</h3><p><strong>Type is the root class. If only passed an object it returns its type (class). Otherwise it creates a new class.</strong></p><pre><code class="python language-python hljs"><class> = type(<span class="hljs-string">'<class_name>'</span>, <parents_tuple>, <attributes_dict>)</code></pre></div></div> |
| 1927 | +<div><h2 id="metaprogramming"><a href="#metaprogramming" name="metaprogramming">#</a>Metaprogramming</h2><p><strong>Code that generates code.</strong></p><div><h3 id="type-1">Type</h3><p><strong>Type is the root class. If only passed an object it returns its type (class). Otherwise it creates a new class.</strong></p><pre><code class="python language-python hljs"><class> = type(<span class="hljs-string">'<class_name>'</span>, <parents_tuple>, <attributes_dict>)</code></pre></div></div> |
1925 | 1928 |
|
1926 | 1929 |
|
1927 | 1930 |
|
|
2300 | 2303 | 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-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>], [<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, 3) <- !</span>
|
2301 | 2304 | </code></pre></div>
|
2302 | 2305 |
|
2303 |
| -<div><h4 id="3ifneithernonmatchingdimensionhassize1raiseanerror">3. If neither non-matching dimension has size 1, raise an error.</h4><div><h3 id="example-1">Example</h3><div><h4 id="foreachpointreturnsindexofitsnearestpoint010608121">For each point returns index of its nearest point (<code class="python hljs">[<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>] => [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">1</span>]</code>):</h4><pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>points = np.array([<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>]) |
| 2306 | +<div><h4 id="3ifneithernonmatchingdimensionhassize1raiseanerror">3. If neither non-matching dimension has size 1, raise an error.</h4><div><h3 id="example-2">Example</h3><div><h4 id="foreachpointreturnsindexofitsnearestpoint010608121">For each point returns index of its nearest point (<code class="python hljs">[<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>] => [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">1</span>]</code>):</h4><pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>points = np.array([<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>]) |
2304 | 2307 | [ <span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>]
|
2305 | 2308 | <span class="hljs-meta">>>> </span>wrapped_points = points.reshape(<span class="hljs-number">3</span>, <span class="hljs-number">1</span>)
|
2306 | 2309 | [[ <span class="hljs-number">0.1</span>],
|
|
0 commit comments