|
1752 | 1752 |
|
1753 | 1753 | <div><h2 id="struct"><a href="#struct" name="struct">#</a>Struct</h2><ul>
|
1754 | 1754 | <li><strong>Module that performs conversions between a sequence of numbers and a bytes object.</strong></li>
|
1755 |
| -<li><strong>Machine’s native type sizes and byte order are used by default.</strong></li> |
| 1755 | +<li><strong>System’s native type sizes and byte order are used by default.</strong></li> |
1756 | 1756 | </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
|
1757 | 1757 | </code></pre></div>
|
1758 | 1758 |
|
|
1792 | 1792 | <array> = array(<span class="hljs-string">'<typecode>'</span>, <bytes>) <span class="hljs-comment"># Array from bytes object.</span>
|
1793 | 1793 | <array> = array(<span class="hljs-string">'<typecode>'</span>, <array>) <span class="hljs-comment"># Treats array as a sequence of numbers.</span>
|
1794 | 1794 | <bytes> = bytes(<array>) <span class="hljs-comment"># Or: <array>.tobytes()</span>
|
| 1795 | +<file>.write(<array>) <span class="hljs-comment"># Writes array to the binary file.</span> |
1795 | 1796 | </code></pre></div>
|
1796 | 1797 |
|
1797 | 1798 |
|
1798 | 1799 | <div><h2 id="memoryview"><a href="#memoryview" name="memoryview">#</a>Memory View</h2><ul>
|
1799 | 1800 | <li><strong>A sequence object that points to the memory of another object.</strong></li>
|
1800 | 1801 | <li><strong>Each element can reference a single or multiple consecutive bytes, depending on format.</strong></li>
|
1801 | 1802 | <li><strong>Order and number of elements can be changed with slicing.</strong></li>
|
| 1803 | +<li><strong>Casting only works between char and other types and always uses native size and b. order.</strong></li> |
1802 | 1804 | </ul><pre><code class="python language-python hljs"><mview> = memoryview(<bytes/bytearray/array>) <span class="hljs-comment"># Immutable if bytes, else mutable.</span>
|
1803 | 1805 | <real> = <mview>[<index>] <span class="hljs-comment"># Returns an int or a float.</span>
|
1804 | 1806 | <mview> = <mview>[<slice>] <span class="hljs-comment"># Mview with rearranged elements.</span>
|
|
1807 | 1809 | </code></pre></div>
|
1808 | 1810 |
|
1809 | 1811 |
|
1810 |
| -<div><h3 id="decode-2">Decode</h3><pre><code class="python language-python hljs"><bin_file>.write(<mview>) <span class="hljs-comment"># Writes mview to the binary file.</span> |
1811 |
| -<bytes> = bytes(<mview>) <span class="hljs-comment"># Creates a new bytes object.</span> |
| 1812 | +<div><h3 id="decode-2">Decode</h3><pre><code class="python language-python hljs"><bytes> = bytes(<mview>) <span class="hljs-comment"># Creates a new bytes object.</span> |
1812 | 1813 | <bytes> = <bytes>.join(<coll_of_mviews>) <span class="hljs-comment"># Joins mviews using bytes object as sep.</span>
|
1813 | 1814 | <array> = array(<span class="hljs-string">'<typecode>'</span>, <mview>) <span class="hljs-comment"># Treats mview as a sequence of numbers.</span>
|
| 1815 | +<file>.write(<mview>) <span class="hljs-comment"># Writes mview to the binary file.</span> |
1814 | 1816 | </code></pre></div>
|
1815 | 1817 |
|
1816 | 1818 | <pre><code class="python language-python hljs"><list> = list(<mview>) <span class="hljs-comment"># Returns list of ints or floats.</span>
|
|
2061 | 2063 |
|
2062 | 2064 | <div><h2 id="plot"><a href="#plot" name="plot">#</a>Plot</h2><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install matplotlib</span>
|
2063 | 2065 | <span class="hljs-keyword">import</span> matplotlib.pyplot <span class="hljs-keyword">as</span> plt
|
2064 |
| -plt.plot(<y_data> [, label=<str>]) |
2065 |
| -plt.plot(<x_data>, <y_data>) |
| 2066 | +plt.plot(<x_data>, <y_data> [, label=<str>]) <span class="hljs-comment"># Or: plt.plot(<y_data>)</span> |
2066 | 2067 | plt.legend() <span class="hljs-comment"># Adds a legend.</span>
|
2067 | 2068 | plt.savefig(<path>) <span class="hljs-comment"># Saves the figure.</span>
|
2068 | 2069 | plt.show() <span class="hljs-comment"># Displays the figure.</span>
|
|
2080 | 2081 |
|
2081 | 2082 |
|
2082 | 2083 | <div><h2 id="curses"><a href="#curses" name="curses">#</a>Curses</h2><div><h4 id="runsabasicfileexplorerintheterminal">Runs a basic file explorer in the terminal:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> curses <span class="hljs-keyword">import</span> wrapper, ascii, A_REVERSE, KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT, KEY_ENTER
|
2083 |
| -<span class="hljs-keyword">from</span> os <span class="hljs-keyword">import</span> listdir, chdir, path |
| 2084 | +<span class="hljs-keyword">from</span> os <span class="hljs-keyword">import</span> listdir, path, chdir |
2084 | 2085 |
|
2085 | 2086 | <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span><span class="hljs-params">(screen)</span>:</span>
|
2086 | 2087 | ch, first, selected, paths = <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, listdir()
|
|
0 commit comments