Skip to content

Commit 7a2574d

Browse files
committed
MemoryView
1 parent 1d5bfb2 commit 7a2574d

File tree

3 files changed

+53
-14
lines changed

3 files changed

+53
-14
lines changed

README.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1943,11 +1943,26 @@ from array import array
19431943

19441944
Memory View
19451945
-----------
1946-
**Used for accessing the internal data of an object that supports the buffer protocol.**
1946+
* **Memory View is a seqence that points to the memory of bytes, bytearray or array objects.**
1947+
* **Each element can reference a single or multiple consecutive bytes.**
1948+
* **Referenced elements can be narrowed down with slicing.**
19471949

19481950
```python
1949-
<mview> = memoryview(<bytes> / <bytearray> / <array>)
1950-
<mview>.release() # Releases the buffer.
1951+
<mview> = memoryview(<bytes/bytearray/array>)
1952+
<num> = <mview>[<index>] # Can be int or float.
1953+
<mview> = <mview>[<slice>] # Mview with rearanged elements.
1954+
<mview> = <mview>.cast('<typecode>') # Cast a memoryview to a new format.
1955+
<mview>.release() # Releases the buffer.
1956+
```
1957+
1958+
```python
1959+
<bin_file>.write(<mview>)
1960+
<bytes> = bytes(<mview>) # Or: <mview>.tobytes()
1961+
<bytes> = <bytes>.join(<coll_of_mviews>) # Joins mviews using bytes object as sep.
1962+
<list> = list(<mview>) # Returns numbers. Or: <mview>.tolist()
1963+
<str> = str(<mview>, 'utf-8') # Or: <bytes>.decode('utf-8')
1964+
<int> = int.from_bytes(<mview>, byteorder='big|little', signed=False)
1965+
'<hex>' = <mview>.hex()
19511966
```
19521967

19531968

@@ -2217,7 +2232,7 @@ def printer():
22172232
reader(adder(printer())) # 100, 101, ..., 109
22182233
```
22192234

2220-
<br><br>
2235+
<br>
22212236

22222237
Libraries
22232238
=========
@@ -2253,7 +2268,7 @@ Table
22532268
```python
22542269
# $ pip3 install tabulate
22552270
import csv, tabulate
2256-
with open(<filename>, encoding='utf-8', newline='') as file:
2271+
with open('test.csv', encoding='utf-8', newline='') as file:
22572272
rows = csv.reader(file)
22582273
header = [a.title() for a in next(rows)]
22592274
table = tabulate.tabulate(rows, header)

index.html

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,16 +1394,16 @@
13941394
<li><strong><code class="python hljs"><span class="hljs-string">'a+'</span></code> - Read and write from the end.</strong></li>
13951395
<li><strong><code class="python hljs"><span class="hljs-string">'t'</span></code> - Text mode (default).</strong></li>
13961396
<li><strong><code class="python hljs"><span class="hljs-string">'b'</span></code> - Binary mode.</strong></li>
1397-
</ul><div><h3 id="exceptions-1">Exceptions</h3><ul>
1397+
</ul></div><div><h3 id="exceptions-1">Exceptions</h3><ul>
13981398
<li><strong><code class="python hljs"><span class="hljs-string">'FileNotFoundError'</span></code> can be risen when reading with <code class="python hljs"><span class="hljs-string">'r'</span></code> or <code class="python hljs"><span class="hljs-string">'r+'</span></code>.</strong></li>
13991399
<li><strong><code class="python hljs"><span class="hljs-string">'FileExistsError'</span></code> can be risen when writing with <code class="python hljs"><span class="hljs-string">'x'</span></code>.</strong></li>
14001400
<li><strong><code class="python hljs"><span class="hljs-string">'IsADirectoryError'</span></code> and <code class="python hljs"><span class="hljs-string">'PermissionError'</span></code> can be risen by any.</strong></li>
14011401
<li><strong><code class="python hljs"><span class="hljs-string">'OSError'</span></code> is the parent class of all listed exceptions.</strong></li>
1402-
</ul><div><h3 id="file">File</h3><pre><code class="python language-python hljs">&lt;file&gt;.seek(<span class="hljs-number">0</span>) <span class="hljs-comment"># Moves to the start of the file.</span>
1402+
</ul></div><div><h3 id="file">File</h3><pre><code class="python language-python hljs">&lt;file&gt;.seek(<span class="hljs-number">0</span>) <span class="hljs-comment"># Moves to the start of the file.</span>
14031403
&lt;file&gt;.seek(offset) <span class="hljs-comment"># Moves 'offset' chars/bytes from the start.</span>
14041404
&lt;file&gt;.seek(<span class="hljs-number">0</span>, <span class="hljs-number">2</span>) <span class="hljs-comment"># Moves to the end of the file.</span>
14051405
&lt;bin_file&gt;.seek(±offset, &lt;anchor&gt;) <span class="hljs-comment"># Anchor: 0 start, 1 current pos., 2 end.</span>
1406-
</code></pre></div></div></div>
1406+
</code></pre></div>
14071407

14081408

14091409

@@ -1724,11 +1724,26 @@
17241724
</code></pre></div>
17251725

17261726

1727-
<div><h2 id="memoryview"><a href="#memoryview" name="memoryview">#</a>Memory View</h2><p><strong>Used for accessing the internal data of an object that supports the buffer protocol.</strong></p><pre><code class="python language-python hljs">&lt;mview&gt; = memoryview(&lt;bytes&gt; / &lt;bytearray&gt; / &lt;array&gt;)
1728-
&lt;mview&gt;.release() <span class="hljs-comment"># Releases the buffer.</span>
1727+
<div><h2 id="memoryview"><a href="#memoryview" name="memoryview">#</a>Memory View</h2><ul>
1728+
<li><strong>Memory View is a seqence that points to the memory of bytes, bytearray or array objects.</strong></li>
1729+
<li><strong>Each element can reference a single or multiple consecutive bytes.</strong></li>
1730+
<li><strong>Referenced elements can be narrowed down with slicing.</strong></li>
1731+
</ul><pre><code class="python language-python hljs">&lt;mview&gt; = memoryview(&lt;bytes/bytearray/array&gt;)
1732+
&lt;num&gt; = &lt;mview&gt;[&lt;index&gt;] <span class="hljs-comment"># Can be int or float.</span>
1733+
&lt;mview&gt; = &lt;mview&gt;[&lt;slice&gt;] <span class="hljs-comment"># Mview with rearanged elements.</span>
1734+
&lt;mview&gt; = &lt;mview&gt;.cast(<span class="hljs-string">'&lt;typecode&gt;'</span>) <span class="hljs-comment"># Cast a memoryview to a new format.</span>
1735+
&lt;mview&gt;.release() <span class="hljs-comment"># Releases the buffer.</span>
17291736
</code></pre></div>
17301737

17311738

1739+
<pre><code class="python language-python hljs">&lt;bin_file&gt;.write(&lt;mview&gt;)
1740+
&lt;bytes&gt; = bytes(&lt;mview&gt;) <span class="hljs-comment"># Or: &lt;mview&gt;.tobytes()</span>
1741+
&lt;bytes&gt; = &lt;bytes&gt;.join(&lt;coll_of_mviews&gt;) <span class="hljs-comment"># Joins mviews using bytes object as sep.</span>
1742+
&lt;list&gt; = list(&lt;mview&gt;) <span class="hljs-comment"># Returns numbers. Or: &lt;mview&gt;.tolist()</span>
1743+
&lt;str&gt; = str(&lt;mview&gt;, <span class="hljs-string">'utf-8'</span>) <span class="hljs-comment"># Or: &lt;bytes&gt;.decode('utf-8')</span>
1744+
&lt;int&gt; = int.from_bytes(&lt;mview&gt;, byteorder=<span class="hljs-string">'big|little'</span>, signed=<span class="hljs-keyword">False</span>)
1745+
<span class="hljs-string">'&lt;hex&gt;'</span> = &lt;mview&gt;.hex()
1746+
</code></pre>
17321747
<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
17331748
&lt;deque&gt; = deque(&lt;collection&gt;, maxlen=<span class="hljs-keyword">None</span>)
17341749
</code></pre></div>
@@ -1925,8 +1940,8 @@
19251940
reader(adder(printer())) <span class="hljs-comment"># 100, 101, ..., 109</span>
19261941
</code></pre></div>
19271942

1928-
<p><br><br></p>
1929-
<div class="pagebreak"></div><div><h1 id="libraries">Libraries</h1><div><h2 id="progressbar"><a href="#progressbar" name="progressbar">#</a>Progress Bar</h2><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install tqdm</span>
1943+
<p><br></p>
1944+
<div><h1 id="libraries">Libraries</h1><div><h2 id="progressbar"><a href="#progressbar" name="progressbar">#</a>Progress Bar</h2><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install tqdm</span>
19301945
<span class="hljs-keyword">from</span> tqdm <span class="hljs-keyword">import</span> tqdm
19311946
<span class="hljs-keyword">from</span> time <span class="hljs-keyword">import</span> sleep
19321947
<span class="hljs-keyword">for</span> el <span class="hljs-keyword">in</span> tqdm([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]):
@@ -1946,7 +1961,7 @@
19461961

19471962
<div><h2 id="table"><a href="#table" name="table">#</a>Table</h2><div><h4 id="printsacsvfileasanasciitable">Prints a CSV file as an ASCII table:</h4><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install tabulate</span>
19481963
<span class="hljs-keyword">import</span> csv, tabulate
1949-
<span class="hljs-keyword">with</span> open(&lt;filename&gt;, encoding=<span class="hljs-string">'utf-8'</span>, newline=<span class="hljs-string">''</span>) <span class="hljs-keyword">as</span> file:
1964+
<span class="hljs-keyword">with</span> open(<span class="hljs-string">'test.csv'</span>, encoding=<span class="hljs-string">'utf-8'</span>, newline=<span class="hljs-string">''</span>) <span class="hljs-keyword">as</span> file:
19501965
rows = csv.reader(file)
19511966
header = [a.title() <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> next(rows)]
19521967
table = tabulate.tabulate(rows, header)

parse.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,8 @@ function highlightCode() {
303303
fixClasses();
304304
fixHighlights();
305305
preventPageBreaks();
306-
insertPageBreak();
306+
fixPageBreaks();
307+
// insertPageBreak();
307308
}
308309

309310
function setApaches(elements) {
@@ -338,6 +339,14 @@ function preventPageBreaks() {
338339
});
339340
}
340341

342+
function fixPageBreaks() {
343+
const fileDiv = $('#file').parent()
344+
const modesDiv = $('#file').parent().parent().parent()
345+
modesDiv.after(fileDiv)
346+
const exceptDiv = $('#exceptions-1').parent()
347+
modesDiv.after(exceptDiv)
348+
}
349+
341350
function insertPageBreak() {
342351
$('<div class="pagebreak"></div>').insertBefore($('#libraries').parent())
343352
}

0 commit comments

Comments
 (0)