Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e2cdb93

Browse files
committed
Struct, Array, Memoryview, Plot
1 parent ead3362 commit e2cdb93

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1938,7 +1938,7 @@ def write_bytes(filename, bytes_obj):
19381938
Struct
19391939
------
19401940
* **Module that performs conversions between a sequence of numbers and a bytes object.**
1941-
* **Machine’s native type sizes and byte order are used by default.**
1941+
* **System’s native type sizes and byte order are used by default.**
19421942

19431943
```python
19441944
from struct import pack, unpack, iter_unpack
@@ -1986,6 +1986,7 @@ from array import array
19861986
<array> = array('<typecode>', <bytes>) # Array from bytes object.
19871987
<array> = array('<typecode>', <array>) # Treats array as a sequence of numbers.
19881988
<bytes> = bytes(<array>) # Or: <array>.tobytes()
1989+
<file>.write(<array>) # Writes array to the binary file.
19891990
```
19901991

19911992

@@ -1994,6 +1995,7 @@ Memory View
19941995
* **A sequence object that points to the memory of another object.**
19951996
* **Each element can reference a single or multiple consecutive bytes, depending on format.**
19961997
* **Order and number of elements can be changed with slicing.**
1998+
* **Casting only works between char and other types and always uses native size and b. order.**
19971999

19982000
```python
19992001
<mview> = memoryview(<bytes/bytearray/array>) # Immutable if bytes, else mutable.
@@ -2005,10 +2007,10 @@ Memory View
20052007

20062008
### Decode
20072009
```python
2008-
<bin_file>.write(<mview>) # Writes mview to the binary file.
20092010
<bytes> = bytes(<mview>) # Creates a new bytes object.
20102011
<bytes> = <bytes>.join(<coll_of_mviews>) # Joins mviews using bytes object as sep.
20112012
<array> = array('<typecode>', <mview>) # Treats mview as a sequence of numbers.
2013+
<file>.write(<mview>) # Writes mview to the binary file.
20122014
```
20132015

20142016
```python
@@ -2342,8 +2344,7 @@ Plot
23422344
```python
23432345
# $ pip3 install matplotlib
23442346
import matplotlib.pyplot as plt
2345-
plt.plot(<y_data> [, label=<str>])
2346-
plt.plot(<x_data>, <y_data>)
2347+
plt.plot(<x_data>, <y_data> [, label=<str>]) # Or: plt.plot(<y_data>)
23472348
plt.legend() # Adds a legend.
23482349
plt.savefig(<path>) # Saves the figure.
23492350
plt.show() # Displays the figure.
@@ -2370,7 +2371,7 @@ Curses
23702371
#### Runs a basic file explorer in the terminal:
23712372
```python
23722373
from curses import wrapper, ascii, A_REVERSE, KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT, KEY_ENTER
2373-
from os import listdir, chdir, path
2374+
from os import listdir, path, chdir
23742375

23752376
def main(screen):
23762377
ch, first, selected, paths = 0, 0, 0, listdir()

index.html

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,7 +1752,7 @@
17521752

17531753
<div><h2 id="struct"><a href="#struct" name="struct">#</a>Struct</h2><ul>
17541754
<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>
17561756
</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
17571757
</code></pre></div>
17581758

@@ -1792,13 +1792,15 @@
17921792
&lt;array&gt; = array(<span class="hljs-string">'&lt;typecode&gt;'</span>, &lt;bytes&gt;) <span class="hljs-comment"># Array from bytes object.</span>
17931793
&lt;array&gt; = array(<span class="hljs-string">'&lt;typecode&gt;'</span>, &lt;array&gt;) <span class="hljs-comment"># Treats array as a sequence of numbers.</span>
17941794
&lt;bytes&gt; = bytes(&lt;array&gt;) <span class="hljs-comment"># Or: &lt;array&gt;.tobytes()</span>
1795+
&lt;file&gt;.write(&lt;array&gt;) <span class="hljs-comment"># Writes array to the binary file.</span>
17951796
</code></pre></div>
17961797

17971798

17981799
<div><h2 id="memoryview"><a href="#memoryview" name="memoryview">#</a>Memory View</h2><ul>
17991800
<li><strong>A sequence object that points to the memory of another object.</strong></li>
18001801
<li><strong>Each element can reference a single or multiple consecutive bytes, depending on format.</strong></li>
18011802
<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>
18021804
</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>
18031805
&lt;real&gt; = &lt;mview&gt;[&lt;index&gt;] <span class="hljs-comment"># Returns an int or a float.</span>
18041806
&lt;mview&gt; = &lt;mview&gt;[&lt;slice&gt;] <span class="hljs-comment"># Mview with rearranged elements.</span>
@@ -1807,10 +1809,10 @@
18071809
</code></pre></div>
18081810

18091811

1810-
<div><h3 id="decode-2">Decode</h3><pre><code class="python language-python hljs">&lt;bin_file&gt;.write(&lt;mview&gt;) <span class="hljs-comment"># Writes mview to the binary file.</span>
1811-
&lt;bytes&gt; = bytes(&lt;mview&gt;) <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">&lt;bytes&gt; = bytes(&lt;mview&gt;) <span class="hljs-comment"># Creates a new bytes object.</span>
18121813
&lt;bytes&gt; = &lt;bytes&gt;.join(&lt;coll_of_mviews&gt;) <span class="hljs-comment"># Joins mviews using bytes object as sep.</span>
18131814
&lt;array&gt; = array(<span class="hljs-string">'&lt;typecode&gt;'</span>, &lt;mview&gt;) <span class="hljs-comment"># Treats mview as a sequence of numbers.</span>
1815+
&lt;file&gt;.write(&lt;mview&gt;) <span class="hljs-comment"># Writes mview to the binary file.</span>
18141816
</code></pre></div>
18151817

18161818
<pre><code class="python language-python hljs">&lt;list&gt; = list(&lt;mview&gt;) <span class="hljs-comment"># Returns list of ints or floats.</span>
@@ -2061,8 +2063,7 @@
20612063

20622064
<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>
20632065
<span class="hljs-keyword">import</span> matplotlib.pyplot <span class="hljs-keyword">as</span> plt
2064-
plt.plot(&lt;y_data&gt; [, label=&lt;str&gt;])
2065-
plt.plot(&lt;x_data&gt;, &lt;y_data&gt;)
2066+
plt.plot(&lt;x_data&gt;, &lt;y_data&gt; [, label=&lt;str&gt;]) <span class="hljs-comment"># Or: plt.plot(&lt;y_data&gt;)</span>
20662067
plt.legend() <span class="hljs-comment"># Adds a legend.</span>
20672068
plt.savefig(&lt;path&gt;) <span class="hljs-comment"># Saves the figure.</span>
20682069
plt.show() <span class="hljs-comment"># Displays the figure.</span>
@@ -2080,7 +2081,7 @@
20802081

20812082

20822083
<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
20842085

20852086
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span><span class="hljs-params">(screen)</span>:</span>
20862087
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

Comments
 (0)