|
1701 | 1701 |
|
1702 | 1702 | <div><h3 id="encode-1">Encode</h3><pre><code class="python language-python hljs"><bytes> = bytes(<coll_of_ints>) <span class="hljs-comment"># Ints must be in range from 0 to 255.</span>
|
1703 | 1703 | <bytes> = bytes(<str>, <span class="hljs-string">'utf-8'</span>) <span class="hljs-comment"># Or: <str>.encode('utf-8')</span>
|
1704 |
| -<bytes> = <int>.to_bytes(n_bytes, byteorder=<span class="hljs-string">'big/little'</span>, signed=<span class="hljs-keyword">False</span>) |
1705 |
| -<bytes> = bytes.fromhex(<span class="hljs-string">'<hex>'</span>) |
| 1704 | +<bytes> = <int>.to_bytes(n_bytes) <span class="hljs-comment"># Also: `byteorder='big/little', signed=False`.</span> |
| 1705 | +<bytes> = bytes.fromhex(<span class="hljs-string">'<hex>'</span>) <span class="hljs-comment"># Hex numbers can be separated by spaces.</span> |
1706 | 1706 | </code></pre></div>
|
1707 | 1707 |
|
1708 | 1708 | <div><h3 id="decode-1">Decode</h3><pre><code class="python language-python hljs"><list> = list(<bytes>) <span class="hljs-comment"># Returns ints in range from 0 to 255.</span>
|
1709 | 1709 | <str> = str(<bytes>, <span class="hljs-string">'utf-8'</span>) <span class="hljs-comment"># Or: <bytes>.decode('utf-8')</span>
|
1710 |
| -<int> = int.from_bytes(<bytes>, byteorder=<span class="hljs-string">'big/little'</span>, signed=<span class="hljs-keyword">False</span>) |
1711 |
| -<span class="hljs-string">'<hex>'</span> = <bytes>.hex() |
| 1710 | +<int> = int.from_bytes(<bytes>) <span class="hljs-comment"># Also: `byteorder='big/little', signed=False`.</span> |
| 1711 | +<span class="hljs-string">'<hex>'</span> = <bytes>.hex() <span class="hljs-comment"># Returns a string of hexadecimal numbers.</span> |
1712 | 1712 | </code></pre></div>
|
1713 | 1713 |
|
1714 | 1714 | <div><h3 id="readbytesfromfile">Read Bytes from File</h3><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">read_bytes</span><span class="hljs-params">(filename)</span>:</span>
|
|
1782 | 1782 | <bytes> = bytes(<mview>) <span class="hljs-comment"># Creates a new bytes object.</span>
|
1783 | 1783 | <bytes> = <bytes>.join(<coll_of_mviews>) <span class="hljs-comment"># Joins mviews using bytes object as sep.</span>
|
1784 | 1784 | <array> = array(<span class="hljs-string">'<typecode>'</span>, <mview>) <span class="hljs-comment"># Treats mview as a sequence of numbers.</span>
|
1785 |
| -<list> = list(<mview>) <span class="hljs-comment"># Returns list of ints or floats.</span> |
1786 |
| -<str> = str(<mview>, <span class="hljs-string">'utf-8'</span>) <span class="hljs-comment"># Treats mview as a bytes object.</span> |
1787 |
| -<int> = int.from_bytes(<mview>, byteorder=<span class="hljs-string">'big/little'</span>, signed=<span class="hljs-keyword">False</span>) |
1788 |
| -<span class="hljs-string">'<hex>'</span> = <mview>.hex() |
1789 | 1785 | </code></pre></div>
|
1790 | 1786 |
|
| 1787 | +<pre><code class="python language-python hljs"><list> = list(<mview>) <span class="hljs-comment"># Returns list of ints or floats.</span> |
| 1788 | +<str> = str(<mview>, <span class="hljs-string">'utf-8'</span>) <span class="hljs-comment"># Treats mview as a bytes object.</span> |
| 1789 | +<int> = int.from_bytes(<mview>, …) <span class="hljs-comment"># `byteorder='big/little', signed=False`.</span> |
| 1790 | +<span class="hljs-string">'<hex>'</span> = <mview>.hex() <span class="hljs-comment"># Treats mview as a bytes object.</span> |
| 1791 | +</code></pre> |
1791 | 1792 | <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
|
1792 | 1793 | <deque> = deque(<collection>, maxlen=<span class="hljs-keyword">None</span>)
|
1793 | 1794 | </code></pre></div>
|
|
0 commit comments