|
508 | 508 |
|
509 | 509 | <div><h3 id="specialsequences">Special Sequences</h3><ul> |
510 | 510 | <li><strong>By default, decimal characters, alphanumerics and whitespaces from all alphabets are matched unless <code class="python hljs"><span class="hljs-string">'flags=re.ASCII'</span></code> argument is used.</strong></li> |
511 | | -<li><strong>As shown below, it restricts special sequence matches to <code class="python hljs"><span class="hljs-string">'[\x00-\x7f]'</span></code> and prevents <code class="python hljs"><span class="hljs-string">'\s'</span></code> from accepting <code class="python hljs"><span class="hljs-string">'[\x1c\x1d\x1e\x1f]'</span></code>.</strong></li> |
| 511 | +<li><strong>As shown below, it restricts special sequence matches to the first 128 characters and prevents <code class="python hljs"><span class="hljs-string">'\s'</span></code> from accepting <code class="python hljs"><span class="hljs-string">'[\x1c\x1d\x1e\x1f]'</span></code>.</strong></li> |
512 | 512 | <li><strong>Use a capital letter for negation.</strong></li> |
513 | 513 | </ul><pre><code class="python language-python hljs"><span class="hljs-string">'\d'</span> == <span class="hljs-string">'[0-9]'</span> <span class="hljs-comment"># Matches decimal characters.</span> |
514 | 514 | <span class="hljs-string">'\w'</span> == <span class="hljs-string">'[a-zA-Z0-9_]'</span> <span class="hljs-comment"># Matches alphanumerics and underscore.</span> |
|
536 | 536 | {<el>:<span class="hljs-number">0</span>} <span class="hljs-comment"># '<el>'</span> |
537 | 537 | </code></pre></div> |
538 | 538 |
|
539 | | -<div><h3 id="strings">Strings</h3><p><strong><code class="python hljs"><span class="hljs-string">'!r'</span></code> calls object's <a href="#class">repr()</a> method, instead of <a href="#class">str()</a>, to get a string.</strong></p><pre><code class="python language-python hljs">{<span class="hljs-string">'abcde'</span>!r:<span class="hljs-number">10</span>} <span class="hljs-comment"># "'abcde' "</span> |
| 539 | +<ul> |
| 540 | +<li><strong>Use <code class="python hljs"><span class="hljs-string">'{<el>:{<str/int/float>}[...]}'</span></code> to set options dynamically.</strong></li> |
| 541 | +<li><strong>Adding <code class="python hljs"><span class="hljs-string">'!r'</span></code> before the colon converts object to string by calling its <a href="#class">repr()</a> method.</strong></li> |
| 542 | +</ul> |
| 543 | +<div><h3 id="strings">Strings</h3><pre><code class="python language-python hljs">{<span class="hljs-string">'abcde'</span>!r:<span class="hljs-number">10</span>} <span class="hljs-comment"># "'abcde' "</span> |
540 | 544 | {<span class="hljs-string">'abcde'</span>:<span class="hljs-number">10.3</span>} <span class="hljs-comment"># 'abc '</span> |
541 | 545 | {<span class="hljs-string">'abcde'</span>:<span class="hljs-number">.3</span>} <span class="hljs-comment"># 'abc'</span> |
542 | 546 | </code></pre></div> |
543 | 547 |
|
544 | | - |
545 | 548 | <div><h3 id="numbers-1">Numbers</h3><pre><code class="python language-python hljs">{ <span class="hljs-number">123456</span>:<span class="hljs-number">10</span>,} <span class="hljs-comment"># ' 123,456'</span> |
546 | 549 | { <span class="hljs-number">123456</span>:<span class="hljs-number">10</span>_} <span class="hljs-comment"># ' 123_456'</span> |
547 | 550 | { <span class="hljs-number">123456</span>:+<span class="hljs-number">10</span>} <span class="hljs-comment"># ' +123456'</span> |
|
1252 | 1255 | <li><strong>Names of their required methods are stored in <code class="python hljs"><span class="hljs-string">'<abc>.__abstractmethods__'</span></code>.</strong></li> |
1253 | 1256 | </ul> |
1254 | 1257 | <div><h2 id="enum"><a href="#enum" name="enum">#</a>Enum</h2><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> enum <span class="hljs-keyword">import</span> Enum, auto |
| 1258 | +</code></pre></div> |
1255 | 1259 |
|
1256 | | -<span class="hljs-class"><span class="hljs-keyword">class</span> <<span class="hljs-title">enum_name</span>><span class="hljs-params">(Enum)</span>:</span> |
| 1260 | +<pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <<span class="hljs-title">enum_name</span>><span class="hljs-params">(Enum)</span>:</span> |
1257 | 1261 | <member_name_1> = <value_1> |
1258 | 1262 | <member_name_2> = <value_2_a>, <value_2_b> |
1259 | 1263 | <member_name_3> = auto() |
1260 | | -</code></pre></div> |
1261 | | - |
| 1264 | +</code></pre> |
1262 | 1265 | <ul> |
1263 | 1266 | <li><strong>If there are no numeric values before auto(), it returns 1.</strong></li> |
1264 | 1267 | <li><strong>Otherwise it returns an increment of the last numeric value.</strong></li> |
|
1691 | 1694 | <conn>.rollback() <span class="hljs-comment"># Discards all changes since the last commit.</span> |
1692 | 1695 | </code></pre></div> |
1693 | 1696 |
|
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> |
| 1697 | +<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 the block with commit() or rollback(),</span> |
1695 | 1698 | <conn>.execute(<span class="hljs-string">'<query>'</span>) <span class="hljs-comment"># depending on whether an exception occurred.</span> |
1696 | 1699 | </code></pre></div> |
1697 | 1700 |
|
|
1754 | 1757 |
|
1755 | 1758 | <div><h2 id="struct"><a href="#struct" name="struct">#</a>Struct</h2><ul> |
1756 | 1759 | <li><strong>Module that performs conversions between a sequence of numbers and a bytes object.</strong></li> |
1757 | | -<li><strong>System’s native type sizes and byte order are used by default.</strong></li> |
| 1760 | +<li><strong>System’s type sizes and byte order are used by default.</strong></li> |
1758 | 1761 | </ul><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> struct <span class="hljs-keyword">import</span> pack, unpack, iter_unpack |
1759 | 1762 | </code></pre></div> |
1760 | 1763 |
|
|
1770 | 1773 | </code></pre></div> |
1771 | 1774 |
|
1772 | 1775 | <div><h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesstartformatstringwith">For standard type sizes start format string with:</h4><ul> |
1773 | | -<li><strong><code class="python hljs"><span class="hljs-string">'='</span></code> - native byte order (usually little-endian)</strong></li> |
| 1776 | +<li><strong><code class="python hljs"><span class="hljs-string">'='</span></code> - system's byte order (usually little-endian)</strong></li> |
1774 | 1777 | <li><strong><code class="python hljs"><span class="hljs-string">'<'</span></code> - little-endian</strong></li> |
1775 | 1778 | <li><strong><code class="python hljs"><span class="hljs-string">'>'</span></code> - big-endian (also <code class="python hljs"><span class="hljs-string">'!'</span></code>)</strong></li> |
1776 | 1779 | </ul></div></div><div><h4 id="integertypesuseacapitalletterforunsignedtypeminimumandstandardsizesareinbrackets">Integer types. Use a capital letter for unsigned type. Minimum and standard sizes are in brackets:</h4><ul> |
|
1803 | 1806 | <li><strong>A sequence object that points to the memory of another object.</strong></li> |
1804 | 1807 | <li><strong>Each element can reference a single or multiple consecutive bytes, depending on format.</strong></li> |
1805 | 1808 | <li><strong>Order and number of elements can be changed with slicing.</strong></li> |
1806 | | -<li><strong>Casting only works between char and other types and always uses native size and b. order.</strong></li> |
| 1809 | +<li><strong>Casting only works between char and other types and uses system's sizes and byte order.</strong></li> |
1807 | 1810 | </ul><pre><code class="python language-python hljs"><mview> = memoryview(<bytes/bytearray/array>) <span class="hljs-comment"># Immutable if bytes, else mutable.</span> |
1808 | 1811 | <real> = <mview>[<index>] <span class="hljs-comment"># Returns an int or a float.</span> |
1809 | 1812 | <mview> = <mview>[<slice>] <span class="hljs-comment"># Mview with rearranged elements.</span> |
|
1841 | 1844 | </code></pre></div> |
1842 | 1845 |
|
1843 | 1846 |
|
1844 | | -<div><h3 id="thread">Thread</h3><pre><code class="python language-python hljs"><Thread> = Thread(target=<function>) <span class="hljs-comment"># Use `args=<collection>` to set arguments.</span> |
| 1847 | +<div><h3 id="thread">Thread</h3><pre><code class="python language-python hljs"><Thread> = Thread(target=<function>) <span class="hljs-comment"># Use `args=<collection>` to set the arguments.</span> |
1845 | 1848 | <Thread>.start() <span class="hljs-comment"># Starts the thread.</span> |
1846 | | -<bool> = <Thread>.is_alive() <span class="hljs-comment"># Checks if thread has finished executing.</span> |
1847 | | -<Thread>.join() <span class="hljs-comment"># Waits for thread to finish.</span> |
| 1849 | +<bool> = <Thread>.is_alive() <span class="hljs-comment"># Checks if the thread has finished executing.</span> |
| 1850 | +<Thread>.join() <span class="hljs-comment"># Waits for the thread to finish.</span> |
1848 | 1851 | </code></pre></div> |
1849 | 1852 |
|
1850 | 1853 | <ul> |
1851 | 1854 | <li><strong>Use <code class="python hljs"><span class="hljs-string">'kwargs=<dict>'</span></code> to pass keyword arguments to the function.</strong></li> |
1852 | 1855 | <li><strong>Use <code class="python hljs"><span class="hljs-string">'daemon=True'</span></code>, or the program will not be able to exit while the thread is alive.</strong></li> |
1853 | 1856 | </ul> |
1854 | 1857 | <div><h3 id="lock">Lock</h3><pre><code class="python language-python hljs"><lock> = RLock() <span class="hljs-comment"># Lock that can only be released by the owner.</span> |
1855 | | -<lock>.acquire() <span class="hljs-comment"># Waits for lock to be available.</span> |
1856 | | -<lock>.release() <span class="hljs-comment"># Makes lock available again.</span> |
| 1858 | +<lock>.acquire() <span class="hljs-comment"># Waits for the lock to be available.</span> |
| 1859 | +<lock>.release() <span class="hljs-comment"># Makes the lock available again.</span> |
1857 | 1860 | </code></pre></div> |
1858 | 1861 |
|
1859 | | -<div><h4 id="or-1">Or:</h4><pre><code class="python language-python hljs">lock = RLock() |
1860 | | -<span class="hljs-keyword">with</span> lock: |
1861 | | - ... |
| 1862 | +<div><h4 id="or-1">Or:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">with</span> <lock>: <span class="hljs-comment"># Enters the block by calling acquire(),</span> |
| 1863 | + ... <span class="hljs-comment"># and exits it with release().</span> |
1862 | 1864 | </code></pre></div> |
1863 | 1865 |
|
1864 | 1866 | <div><h3 id="semaphoreeventbarrier">Semaphore, Event, Barrier</h3><pre><code class="python language-python hljs"><Semaphore> = Semaphore(value=<span class="hljs-number">1</span>) <span class="hljs-comment"># Lock that can be acquired by 'value' threads.</span> |
|
0 commit comments