|
1713 | 1713 | <div><h3 id="format-2">Format</h3></div><div><h4 id="forstandardsizesstartformatstringwith">For standard sizes start format string with:</h4><ul>
|
1714 | 1714 | <li><strong><code class="python hljs"><span class="hljs-string">'='</span></code> - native byte order</strong></li>
|
1715 | 1715 | <li><strong><code class="python hljs"><span class="hljs-string">'<'</span></code> - little-endian</strong></li>
|
1716 |
| -<li><strong><code class="python hljs"><span class="hljs-string">'>'</span></code> - big-endian</strong></li> |
| 1716 | +<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> |
1717 | 1717 | </ul></div><div><h4 id="integertypesusecapitalletterforunsignedtypestandardsizesareinbrackets">Integer types. Use capital letter for unsigned type. Standard sizes are in brackets:</h4><ul>
|
1718 | 1718 | <li><strong><code class="python hljs"><span class="hljs-string">'x'</span></code> - pad byte</strong></li>
|
1719 | 1719 | <li><strong><code class="python hljs"><span class="hljs-string">'b'</span></code> - char (1)</strong></li>
|
|
1734 | 1734 | <div><h2 id="array"><a href="#array" name="array">#</a>Array</h2><p><strong>List that can only hold numbers of a predefined type. Available types and their sizes in bytes are listed above.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> array <span class="hljs-keyword">import</span> array
|
1735 | 1735 | <array> = array(<span class="hljs-string">'<typecode>'</span>, <collection>) <span class="hljs-comment"># Array from coll. of numbers.</span>
|
1736 | 1736 | <array> = array(<span class="hljs-string">'<typecode>'</span>, <bytes>) <span class="hljs-comment"># Array from bytes object.</span>
|
1737 |
| -<bytes> = <array>.tobytes() |
| 1737 | +<bytes> = bytes(<array>) <span class="hljs-comment"># Or: <array>.tobytes()</span> |
1738 | 1738 | </code></pre></div>
|
1739 | 1739 |
|
1740 | 1740 |
|
1741 | 1741 | <div><h2 id="memoryview"><a href="#memoryview" name="memoryview">#</a>Memory View</h2><ul>
|
1742 | 1742 | <li><strong>A sequence object that points to the memory of another object.</strong></li>
|
1743 | 1743 | <li><strong>Each element can reference a single or multiple consecutive bytes, depending on format.</strong></li>
|
1744 | 1744 | <li><strong>Order and number of elements can be changed with slicing.</strong></li>
|
1745 |
| -</ul><pre><code class="python language-python hljs"><mview> = memoryview(<bytes/bytearray/array>) |
1746 |
| -<num> = <mview>[<index>] <span class="hljs-comment"># Returns an int or a float.</span> |
| 1745 | +</ul><pre><code class="python language-python hljs"><mview> = memoryview(<bytes/bytearray/array>) <span class="hljs-comment"># Immutable if bytes, else mutable.</span> |
| 1746 | +<real> = <mview>[<index>] <span class="hljs-comment"># Returns an int or a float.</span> |
1747 | 1747 | <mview> = <mview>[<slice>] <span class="hljs-comment"># Mview with rearranged elements.</span>
|
1748 | 1748 | <mview> = <mview>.cast(<span class="hljs-string">'<typecode>'</span>) <span class="hljs-comment"># Casts memoryview to the new format.</span>
|
| 1749 | +<bin_file>.write(<mview>) <span class="hljs-comment"># Appends mview to the binary file.</span> |
1749 | 1750 | <mview>.release() <span class="hljs-comment"># Releases the object's memory buffer.</span>
|
1750 | 1751 | </code></pre></div>
|
1751 | 1752 |
|
1752 | 1753 |
|
1753 |
| -<pre><code class="python language-python hljs"><bin_file>.write(<mview>) <span class="hljs-comment"># Appends mview to the binary file.</span> |
1754 |
| -<bytes> = bytes(<mview>) <span class="hljs-comment"># Creates a new bytes object.</span> |
| 1754 | +<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> |
1755 | 1755 | <bytes> = <bytes>.join(<coll_of_mviews>) <span class="hljs-comment"># Joins mviews using bytes object as sep.</span>
|
1756 | 1756 | <list> = list(<mview>) <span class="hljs-comment"># Returns list of ints or floats.</span>
|
1757 |
| -</code></pre> |
1758 |
| -<pre><code class="python language-python hljs"><str> = str(<mview>, <span class="hljs-string">'utf-8'</span>) |
| 1757 | +<str> = str(<mview>, <span class="hljs-string">'utf-8'</span>) |
1759 | 1758 | <int> = int.from_bytes(<mview>, byteorder=<span class="hljs-string">'big|little'</span>, signed=<span class="hljs-keyword">False</span>)
|
1760 | 1759 | <span class="hljs-string">'<hex>'</span> = <mview>.hex()
|
1761 |
| -</code></pre> |
| 1760 | +</code></pre></div> |
| 1761 | + |
1762 | 1762 | <div><h2 id="deque"><a href="#deque" name="deque">#</a>Deque</h2><p><strong>A thread-safe list with efficient appends and pops from either side. Pronounced "deck".</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> collections <span class="hljs-keyword">import</span> deque
|
1763 | 1763 | <deque> = deque(<collection>, maxlen=<span class="hljs-keyword">None</span>)
|
1764 | 1764 | </code></pre></div>
|
|
0 commit comments