|
1394 | 1394 | <li><strong><code class="python hljs"><span class="hljs-string">'a+'</span></code> - Read and write from the end.</strong></li>
|
1395 | 1395 | <li><strong><code class="python hljs"><span class="hljs-string">'t'</span></code> - Text mode (default).</strong></li>
|
1396 | 1396 | <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> |
1398 | 1398 | <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>
|
1399 | 1399 | <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>
|
1400 | 1400 | <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>
|
1401 | 1401 | <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"><file>.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"><file>.seek(<span class="hljs-number">0</span>) <span class="hljs-comment"># Moves to the start of the file.</span> |
1403 | 1403 | <file>.seek(offset) <span class="hljs-comment"># Moves 'offset' chars/bytes from the start.</span>
|
1404 | 1404 | <file>.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>
|
1405 | 1405 | <bin_file>.seek(±offset, <anchor>) <span class="hljs-comment"># Anchor: 0 start, 1 current pos., 2 end.</span>
|
1406 |
| -</code></pre></div></div></div> |
| 1406 | +</code></pre></div> |
1407 | 1407 |
|
1408 | 1408 |
|
1409 | 1409 |
|
|
1724 | 1724 | </code></pre></div>
|
1725 | 1725 |
|
1726 | 1726 |
|
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"><mview> = memoryview(<bytes> / <bytearray> / <array>) |
1728 |
| -<mview>.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"><mview> = memoryview(<bytes/bytearray/array>) |
| 1732 | +<num> = <mview>[<index>] <span class="hljs-comment"># Can be int or float.</span> |
| 1733 | +<mview> = <mview>[<slice>] <span class="hljs-comment"># Mview with rearanged elements.</span> |
| 1734 | +<mview> = <mview>.cast(<span class="hljs-string">'<typecode>'</span>) <span class="hljs-comment"># Cast a memoryview to a new format.</span> |
| 1735 | +<mview>.release() <span class="hljs-comment"># Releases the buffer.</span> |
1729 | 1736 | </code></pre></div>
|
1730 | 1737 |
|
1731 | 1738 |
|
| 1739 | +<pre><code class="python language-python hljs"><bin_file>.write(<mview>) |
| 1740 | +<bytes> = bytes(<mview>) <span class="hljs-comment"># Or: <mview>.tobytes()</span> |
| 1741 | +<bytes> = <bytes>.join(<coll_of_mviews>) <span class="hljs-comment"># Joins mviews using bytes object as sep.</span> |
| 1742 | +<list> = list(<mview>) <span class="hljs-comment"># Returns numbers. Or: <mview>.tolist()</span> |
| 1743 | +<str> = str(<mview>, <span class="hljs-string">'utf-8'</span>) <span class="hljs-comment"># Or: <bytes>.decode('utf-8')</span> |
| 1744 | +<int> = int.from_bytes(<mview>, byteorder=<span class="hljs-string">'big|little'</span>, signed=<span class="hljs-keyword">False</span>) |
| 1745 | +<span class="hljs-string">'<hex>'</span> = <mview>.hex() |
| 1746 | +</code></pre> |
1732 | 1747 | <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
|
1733 | 1748 | <deque> = deque(<collection>, maxlen=<span class="hljs-keyword">None</span>)
|
1734 | 1749 | </code></pre></div>
|
|
1925 | 1940 | reader(adder(printer())) <span class="hljs-comment"># 100, 101, ..., 109</span>
|
1926 | 1941 | </code></pre></div>
|
1927 | 1942 |
|
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> |
1930 | 1945 | <span class="hljs-keyword">from</span> tqdm <span class="hljs-keyword">import</span> tqdm
|
1931 | 1946 | <span class="hljs-keyword">from</span> time <span class="hljs-keyword">import</span> sleep
|
1932 | 1947 | <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 | 1961 |
|
1947 | 1962 | <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>
|
1948 | 1963 | <span class="hljs-keyword">import</span> csv, tabulate
|
1949 |
| -<span class="hljs-keyword">with</span> open(<filename>, 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: |
1950 | 1965 | rows = csv.reader(file)
|
1951 | 1966 | header = [a.title() <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> next(rows)]
|
1952 | 1967 | table = tabulate.tabulate(rows, header)
|
|
0 commit comments