Skip to content

Commit 66928ac

Browse files
committed
Memoryview
1 parent 848809d commit 66928ac

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

README.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1936,7 +1936,7 @@ b'\x00\x01\x00\x02\x00\x00\x00\x03'
19361936
#### For standard sizes start format string with:
19371937
* **`'='` - native byte order**
19381938
* **`'<'` - little-endian**
1939-
* **`'>'` - big-endian**
1939+
* **`'>'` - big-endian (also `'!'`)**
19401940

19411941
#### Integer types. Use capital letter for unsigned type. Standard sizes are in brackets:
19421942
* **`'x'` - pad byte**
@@ -1959,7 +1959,7 @@ Array
19591959
from array import array
19601960
<array> = array('<typecode>', <collection>) # Array from coll. of numbers.
19611961
<array> = array('<typecode>', <bytes>) # Array from bytes object.
1962-
<bytes> = <array>.tobytes()
1962+
<bytes> = bytes(<array>) # Or: <array>.tobytes()
19631963
```
19641964

19651965

@@ -1970,21 +1970,19 @@ Memory View
19701970
* **Order and number of elements can be changed with slicing.**
19711971

19721972
```python
1973-
<mview> = memoryview(<bytes/bytearray/array>)
1974-
<num> = <mview>[<index>] # Returns an int or a float.
1973+
<mview> = memoryview(<bytes/bytearray/array>) # Immutable if bytes, else mutable.
1974+
<real> = <mview>[<index>] # Returns an int or a float.
19751975
<mview> = <mview>[<slice>] # Mview with rearranged elements.
19761976
<mview> = <mview>.cast('<typecode>') # Casts memoryview to the new format.
1977+
<bin_file>.write(<mview>) # Appends mview to the binary file.
19771978
<mview>.release() # Releases the object's memory buffer.
19781979
```
19791980

1981+
### Decode
19801982
```python
1981-
<bin_file>.write(<mview>) # Appends mview to the binary file.
19821983
<bytes> = bytes(<mview>) # Creates a new bytes object.
19831984
<bytes> = <bytes>.join(<coll_of_mviews>) # Joins mviews using bytes object as sep.
19841985
<list> = list(<mview>) # Returns list of ints or floats.
1985-
```
1986-
1987-
```python
19881986
<str> = str(<mview>, 'utf-8')
19891987
<int> = int.from_bytes(<mview>, byteorder='big|little', signed=False)
19901988
'<hex>' = <mview>.hex()

index.html

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,7 +1713,7 @@
17131713
<div><h3 id="format-2">Format</h3></div><div><h4 id="forstandardsizesstartformatstringwith">For standard sizes start format string with:</h4><ul>
17141714
<li><strong><code class="python hljs"><span class="hljs-string">'='</span></code> - native byte order</strong></li>
17151715
<li><strong><code class="python hljs"><span class="hljs-string">'&lt;'</span></code> - little-endian</strong></li>
1716-
<li><strong><code class="python hljs"><span class="hljs-string">'&gt;'</span></code> - big-endian</strong></li>
1716+
<li><strong><code class="python hljs"><span class="hljs-string">'&gt;'</span></code> - big-endian (also <code class="python hljs"><span class="hljs-string">'!'</span></code>)</strong></li>
17171717
</ul></div><div><h4 id="integertypesusecapitalletterforunsignedtypestandardsizesareinbrackets">Integer types. Use capital letter for unsigned type. Standard sizes are in brackets:</h4><ul>
17181718
<li><strong><code class="python hljs"><span class="hljs-string">'x'</span></code> - pad byte</strong></li>
17191719
<li><strong><code class="python hljs"><span class="hljs-string">'b'</span></code> - char (1)</strong></li>
@@ -1734,31 +1734,31 @@
17341734
<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
17351735
&lt;array&gt; = array(<span class="hljs-string">'&lt;typecode&gt;'</span>, &lt;collection&gt;) <span class="hljs-comment"># Array from coll. of numbers.</span>
17361736
&lt;array&gt; = array(<span class="hljs-string">'&lt;typecode&gt;'</span>, &lt;bytes&gt;) <span class="hljs-comment"># Array from bytes object.</span>
1737-
&lt;bytes&gt; = &lt;array&gt;.tobytes()
1737+
&lt;bytes&gt; = bytes(&lt;array&gt;) <span class="hljs-comment"># Or: &lt;array&gt;.tobytes()</span>
17381738
</code></pre></div>
17391739

17401740

17411741
<div><h2 id="memoryview"><a href="#memoryview" name="memoryview">#</a>Memory View</h2><ul>
17421742
<li><strong>A sequence object that points to the memory of another object.</strong></li>
17431743
<li><strong>Each element can reference a single or multiple consecutive bytes, depending on format.</strong></li>
17441744
<li><strong>Order and number of elements can be changed with slicing.</strong></li>
1745-
</ul><pre><code class="python language-python hljs">&lt;mview&gt; = memoryview(&lt;bytes/bytearray/array&gt;)
1746-
&lt;num&gt; = &lt;mview&gt;[&lt;index&gt;] <span class="hljs-comment"># Returns an int or a float.</span>
1745+
</ul><pre><code class="python language-python hljs">&lt;mview&gt; = memoryview(&lt;bytes/bytearray/array&gt;) <span class="hljs-comment"># Immutable if bytes, else mutable.</span>
1746+
&lt;real&gt; = &lt;mview&gt;[&lt;index&gt;] <span class="hljs-comment"># Returns an int or a float.</span>
17471747
&lt;mview&gt; = &lt;mview&gt;[&lt;slice&gt;] <span class="hljs-comment"># Mview with rearranged elements.</span>
17481748
&lt;mview&gt; = &lt;mview&gt;.cast(<span class="hljs-string">'&lt;typecode&gt;'</span>) <span class="hljs-comment"># Casts memoryview to the new format.</span>
1749+
&lt;bin_file&gt;.write(&lt;mview&gt;) <span class="hljs-comment"># Appends mview to the binary file.</span>
17491750
&lt;mview&gt;.release() <span class="hljs-comment"># Releases the object's memory buffer.</span>
17501751
</code></pre></div>
17511752

17521753

1753-
<pre><code class="python language-python hljs">&lt;bin_file&gt;.write(&lt;mview&gt;) <span class="hljs-comment"># Appends mview to the binary file.</span>
1754-
&lt;bytes&gt; = bytes(&lt;mview&gt;) <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">&lt;bytes&gt; = bytes(&lt;mview&gt;) <span class="hljs-comment"># Creates a new bytes object.</span>
17551755
&lt;bytes&gt; = &lt;bytes&gt;.join(&lt;coll_of_mviews&gt;) <span class="hljs-comment"># Joins mviews using bytes object as sep.</span>
17561756
&lt;list&gt; = list(&lt;mview&gt;) <span class="hljs-comment"># Returns list of ints or floats.</span>
1757-
</code></pre>
1758-
<pre><code class="python language-python hljs">&lt;str&gt; = str(&lt;mview&gt;, <span class="hljs-string">'utf-8'</span>)
1757+
&lt;str&gt; = str(&lt;mview&gt;, <span class="hljs-string">'utf-8'</span>)
17591758
&lt;int&gt; = int.from_bytes(&lt;mview&gt;, byteorder=<span class="hljs-string">'big|little'</span>, signed=<span class="hljs-keyword">False</span>)
17601759
<span class="hljs-string">'&lt;hex&gt;'</span> = &lt;mview&gt;.hex()
1761-
</code></pre>
1760+
</code></pre></div>
1761+
17621762
<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
17631763
&lt;deque&gt; = deque(&lt;collection&gt;, maxlen=<span class="hljs-keyword">None</span>)
17641764
</code></pre></div>

0 commit comments

Comments
 (0)