Skip to content

Commit 05cd30e

Browse files
committed
Memoryview
1 parent cffe52c commit 05cd30e

File tree

2 files changed

+108
-80
lines changed

2 files changed

+108
-80
lines changed

README.md

Lines changed: 64 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,52 +1739,6 @@ def write_to_csv_file(filename, rows):
17391739
```
17401740

17411741

1742-
JSON
1743-
----
1744-
```python
1745-
import json
1746-
<str> = json.dumps(<object>, ensure_ascii=True, indent=None)
1747-
<object> = json.loads(<str>)
1748-
```
1749-
1750-
### Read Object from JSON File
1751-
```python
1752-
def read_json_file(filename):
1753-
with open(filename, encoding='utf-8') as file:
1754-
return json.load(file)
1755-
```
1756-
1757-
### Write Object to JSON File
1758-
```python
1759-
def write_to_json_file(filename, an_object):
1760-
with open(filename, 'w', encoding='utf-8') as file:
1761-
json.dump(an_object, file, ensure_ascii=False, indent=2)
1762-
```
1763-
1764-
1765-
Pickle
1766-
------
1767-
```python
1768-
import pickle
1769-
<bytes> = pickle.dumps(<object>)
1770-
<object> = pickle.loads(<bytes>)
1771-
```
1772-
1773-
### Read Object from File
1774-
```python
1775-
def read_pickle_file(filename):
1776-
with open(filename, 'rb') as file:
1777-
return pickle.load(file)
1778-
```
1779-
1780-
### Write Object to File
1781-
```python
1782-
def write_to_pickle_file(filename, an_object):
1783-
with open(filename, 'wb') as file:
1784-
pickle.dump(an_object, file)
1785-
```
1786-
1787-
17881742
SQLite
17891743
------
17901744
**Server-less database engine that stores each database into separate file.**
@@ -1848,6 +1802,52 @@ db = connector.connect(host=<str>, user=<str>, password=<str>, database=<str>)
18481802
```
18491803

18501804

1805+
JSON
1806+
----
1807+
```python
1808+
import json
1809+
<str> = json.dumps(<object>, ensure_ascii=True, indent=None)
1810+
<object> = json.loads(<str>)
1811+
```
1812+
1813+
### Read Object from JSON File
1814+
```python
1815+
def read_json_file(filename):
1816+
with open(filename, encoding='utf-8') as file:
1817+
return json.load(file)
1818+
```
1819+
1820+
### Write Object to JSON File
1821+
```python
1822+
def write_to_json_file(filename, an_object):
1823+
with open(filename, 'w', encoding='utf-8') as file:
1824+
json.dump(an_object, file, ensure_ascii=False, indent=2)
1825+
```
1826+
1827+
1828+
Pickle
1829+
------
1830+
```python
1831+
import pickle
1832+
<bytes> = pickle.dumps(<object>)
1833+
<object> = pickle.loads(<bytes>)
1834+
```
1835+
1836+
### Read Object from File
1837+
```python
1838+
def read_pickle_file(filename):
1839+
with open(filename, 'rb') as file:
1840+
return pickle.load(file)
1841+
```
1842+
1843+
### Write Object to File
1844+
```python
1845+
def write_to_pickle_file(filename, an_object):
1846+
with open(filename, 'wb') as file:
1847+
pickle.dump(an_object, file)
1848+
```
1849+
1850+
18511851
Bytes
18521852
-----
18531853
**Bytes object is an immutable sequence of single bytes. Mutable version is called 'bytearray'.**
@@ -1946,8 +1946,24 @@ Memory View
19461946
**Used for accessing the internal data of an object that supports the buffer protocol.**
19471947

19481948
```python
1949-
<memoryview> = memoryview(<bytes> / <bytearray> / <array>)
1950-
<memoryview>.release()
1949+
<mview> = memoryview(<bytes> / <bytearray> / <array>)
1950+
<mview>.release() # Releases the buffer.
1951+
```
1952+
1953+
```python
1954+
<num> = <mview>[<index>] # Returns int in range from 0 to 255.
1955+
<mview> = <mview>[<slice>] # Returns bytes even if it has only one element.
1956+
<file>.write(<mview>)
1957+
```
1958+
1959+
```python
1960+
<bytes> = <bytes>.join(<coll_of_mviews>) # Joins elements using bytes object as separator.
1961+
<bytes> = bytes(<mview>) # Or: <mview>.tobytes()
1962+
'<hex>' = <mview>.hex()
1963+
<list> = list(<mview>) # Returns numbers.
1964+
<str> = str(<mview>, 'utf-8') # Or: <bytes>.decode('utf-8')
1965+
<int> = int.from_bytes(<mview>, byteorder='big|little', signed=False)
1966+
'<hex>' = <bytes>.hex()
19511967
```
19521968

19531969

index.html

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,36 +1566,6 @@
15661566
writer.writerows(rows)
15671567
</code></pre></div>
15681568

1569-
<div><h2 id="json"><a href="#json" name="json">#</a>JSON</h2><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> json
1570-
&lt;str&gt; = json.dumps(&lt;object&gt;, ensure_ascii=<span class="hljs-keyword">True</span>, indent=<span class="hljs-keyword">None</span>)
1571-
&lt;object&gt; = json.loads(&lt;str&gt;)
1572-
</code></pre></div>
1573-
1574-
<div><h3 id="readobjectfromjsonfile">Read Object from JSON File</h3><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">read_json_file</span><span class="hljs-params">(filename)</span>:</span>
1575-
<span class="hljs-keyword">with</span> open(filename, encoding=<span class="hljs-string">'utf-8'</span>) <span class="hljs-keyword">as</span> file:
1576-
<span class="hljs-keyword">return</span> json.load(file)
1577-
</code></pre></div>
1578-
1579-
<div><h3 id="writeobjecttojsonfile">Write Object to JSON File</h3><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">write_to_json_file</span><span class="hljs-params">(filename, an_object)</span>:</span>
1580-
<span class="hljs-keyword">with</span> open(filename, <span class="hljs-string">'w'</span>, encoding=<span class="hljs-string">'utf-8'</span>) <span class="hljs-keyword">as</span> file:
1581-
json.dump(an_object, file, ensure_ascii=<span class="hljs-keyword">False</span>, indent=<span class="hljs-number">2</span>)
1582-
</code></pre></div>
1583-
1584-
<div><h2 id="pickle"><a href="#pickle" name="pickle">#</a>Pickle</h2><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> pickle
1585-
&lt;bytes&gt; = pickle.dumps(&lt;object&gt;)
1586-
&lt;object&gt; = pickle.loads(&lt;bytes&gt;)
1587-
</code></pre></div>
1588-
1589-
<div><h3 id="readobjectfromfile">Read Object 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_pickle_file</span><span class="hljs-params">(filename)</span>:</span>
1590-
<span class="hljs-keyword">with</span> open(filename, <span class="hljs-string">'rb'</span>) <span class="hljs-keyword">as</span> file:
1591-
<span class="hljs-keyword">return</span> pickle.load(file)
1592-
</code></pre></div>
1593-
1594-
<div><h3 id="writeobjecttofile">Write Object to File</h3><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">write_to_pickle_file</span><span class="hljs-params">(filename, an_object)</span>:</span>
1595-
<span class="hljs-keyword">with</span> open(filename, <span class="hljs-string">'wb'</span>) <span class="hljs-keyword">as</span> file:
1596-
pickle.dump(an_object, file)
1597-
</code></pre></div>
1598-
15991569
<div><h2 id="sqlite"><a href="#sqlite" name="sqlite">#</a>SQLite</h2><p><strong>Server-less database engine that stores each database into separate file.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> sqlite3
16001570
db = sqlite3.connect(<span class="hljs-string">'&lt;path&gt;'</span>) <span class="hljs-comment"># Also ':memory:'.</span>
16011571
...
@@ -1651,6 +1621,36 @@
16511621
</code></pre></div>
16521622

16531623

1624+
<div><h2 id="json"><a href="#json" name="json">#</a>JSON</h2><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> json
1625+
&lt;str&gt; = json.dumps(&lt;object&gt;, ensure_ascii=<span class="hljs-keyword">True</span>, indent=<span class="hljs-keyword">None</span>)
1626+
&lt;object&gt; = json.loads(&lt;str&gt;)
1627+
</code></pre></div>
1628+
1629+
<div><h3 id="readobjectfromjsonfile">Read Object from JSON File</h3><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">read_json_file</span><span class="hljs-params">(filename)</span>:</span>
1630+
<span class="hljs-keyword">with</span> open(filename, encoding=<span class="hljs-string">'utf-8'</span>) <span class="hljs-keyword">as</span> file:
1631+
<span class="hljs-keyword">return</span> json.load(file)
1632+
</code></pre></div>
1633+
1634+
<div><h3 id="writeobjecttojsonfile">Write Object to JSON File</h3><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">write_to_json_file</span><span class="hljs-params">(filename, an_object)</span>:</span>
1635+
<span class="hljs-keyword">with</span> open(filename, <span class="hljs-string">'w'</span>, encoding=<span class="hljs-string">'utf-8'</span>) <span class="hljs-keyword">as</span> file:
1636+
json.dump(an_object, file, ensure_ascii=<span class="hljs-keyword">False</span>, indent=<span class="hljs-number">2</span>)
1637+
</code></pre></div>
1638+
1639+
<div><h2 id="pickle"><a href="#pickle" name="pickle">#</a>Pickle</h2><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> pickle
1640+
&lt;bytes&gt; = pickle.dumps(&lt;object&gt;)
1641+
&lt;object&gt; = pickle.loads(&lt;bytes&gt;)
1642+
</code></pre></div>
1643+
1644+
<div><h3 id="readobjectfromfile">Read Object 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_pickle_file</span><span class="hljs-params">(filename)</span>:</span>
1645+
<span class="hljs-keyword">with</span> open(filename, <span class="hljs-string">'rb'</span>) <span class="hljs-keyword">as</span> file:
1646+
<span class="hljs-keyword">return</span> pickle.load(file)
1647+
</code></pre></div>
1648+
1649+
<div><h3 id="writeobjecttofile">Write Object to File</h3><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">write_to_pickle_file</span><span class="hljs-params">(filename, an_object)</span>:</span>
1650+
<span class="hljs-keyword">with</span> open(filename, <span class="hljs-string">'wb'</span>) <span class="hljs-keyword">as</span> file:
1651+
pickle.dump(an_object, file)
1652+
</code></pre></div>
1653+
16541654
<div><h2 id="bytes"><a href="#bytes" name="bytes">#</a>Bytes</h2><p><strong>Bytes object is an immutable sequence of single bytes. Mutable version is called 'bytearray'.</strong></p><pre><code class="python language-python hljs">&lt;bytes&gt; = <span class="hljs-string">b'&lt;str&gt;'</span> <span class="hljs-comment"># Only accepts ASCII characters and \x00 - \xff.</span>
16551655
&lt;int&gt; = &lt;bytes&gt;[&lt;index&gt;] <span class="hljs-comment"># Returns int in range from 0 to 255.</span>
16561656
&lt;bytes&gt; = &lt;bytes&gt;[&lt;slice&gt;] <span class="hljs-comment"># Returns bytes even if it has only one element.</span>
@@ -1724,11 +1724,23 @@
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;memoryview&gt; = memoryview(&lt;bytes&gt; / &lt;bytearray&gt; / &lt;array&gt;)
1728-
&lt;memoryview&gt;.release()
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>
17291729
</code></pre></div>
17301730

17311731

1732+
<pre><code class="python language-python hljs">&lt;num&gt; = &lt;mview&gt;[&lt;index&gt;] <span class="hljs-comment"># Returns int in range from 0 to 255.</span>
1733+
&lt;mview&gt; = &lt;mview&gt;[&lt;slice&gt;] <span class="hljs-comment"># Returns bytes even if it has only one element.</span>
1734+
&lt;file&gt;.write(&lt;mview&gt;)
1735+
</code></pre>
1736+
<pre><code class="python language-python hljs">&lt;bytes&gt; = &lt;bytes&gt;.join(&lt;coll_of_mviews&gt;) <span class="hljs-comment"># Joins elements using bytes object as separator.</span>
1737+
&lt;bytes&gt; = bytes(&lt;mview&gt;) <span class="hljs-comment"># Or: &lt;mview&gt;.tobytes() </span>
1738+
<span class="hljs-string">'&lt;hex&gt;'</span> = &lt;mview&gt;.hex()
1739+
&lt;list&gt; = list(&lt;mview&gt;) <span class="hljs-comment"># Returns numbers.</span>
1740+
&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>
1741+
&lt;int&gt; = int.from_bytes(&lt;mview&gt;, byteorder=<span class="hljs-string">'big|little'</span>, signed=<span class="hljs-keyword">False</span>)
1742+
<span class="hljs-string">'&lt;hex&gt;'</span> = &lt;bytes&gt;.hex()
1743+
</code></pre>
17321744
<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
17331745
&lt;deque&gt; = deque(&lt;collection&gt;, maxlen=<span class="hljs-keyword">None</span>)
17341746
</code></pre></div>

0 commit comments

Comments
 (0)