Browse Source

Memoryview

pull/44/head
Jure Šorn 5 years ago
parent
commit
05cd30ecf2
2 changed files with 108 additions and 80 deletions
  1. 112
      README.md
  2. 76
      index.html

112
README.md

@ -1739,52 +1739,6 @@ def write_to_csv_file(filename, rows):
```
JSON
----
```python
import json
<str> = json.dumps(<object>, ensure_ascii=True, indent=None)
<object> = json.loads(<str>)
```
### Read Object from JSON File
```python
def read_json_file(filename):
with open(filename, encoding='utf-8') as file:
return json.load(file)
```
### Write Object to JSON File
```python
def write_to_json_file(filename, an_object):
with open(filename, 'w', encoding='utf-8') as file:
json.dump(an_object, file, ensure_ascii=False, indent=2)
```
Pickle
------
```python
import pickle
<bytes> = pickle.dumps(<object>)
<object> = pickle.loads(<bytes>)
```
### Read Object from File
```python
def read_pickle_file(filename):
with open(filename, 'rb') as file:
return pickle.load(file)
```
### Write Object to File
```python
def write_to_pickle_file(filename, an_object):
with open(filename, 'wb') as file:
pickle.dump(an_object, file)
```
SQLite
------
**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>)
```
JSON
----
```python
import json
<str> = json.dumps(<object>, ensure_ascii=True, indent=None)
<object> = json.loads(<str>)
```
### Read Object from JSON File
```python
def read_json_file(filename):
with open(filename, encoding='utf-8') as file:
return json.load(file)
```
### Write Object to JSON File
```python
def write_to_json_file(filename, an_object):
with open(filename, 'w', encoding='utf-8') as file:
json.dump(an_object, file, ensure_ascii=False, indent=2)
```
Pickle
------
```python
import pickle
<bytes> = pickle.dumps(<object>)
<object> = pickle.loads(<bytes>)
```
### Read Object from File
```python
def read_pickle_file(filename):
with open(filename, 'rb') as file:
return pickle.load(file)
```
### Write Object to File
```python
def write_to_pickle_file(filename, an_object):
with open(filename, 'wb') as file:
pickle.dump(an_object, file)
```
Bytes
-----
**Bytes object is an immutable sequence of single bytes. Mutable version is called 'bytearray'.**
@ -1946,8 +1946,24 @@ Memory View
**Used for accessing the internal data of an object that supports the buffer protocol.**
```python
<memoryview> = memoryview(<bytes> / <bytearray> / <array>)
<memoryview>.release()
<mview> = memoryview(<bytes> / <bytearray> / <array>)
<mview>.release() # Releases the buffer.
```
```python
<num> = <mview>[<index>] # Returns int in range from 0 to 255.
<mview> = <mview>[<slice>] # Returns bytes even if it has only one element.
<file>.write(<mview>)
```
```python
<bytes> = <bytes>.join(<coll_of_mviews>) # Joins elements using bytes object as separator.
<bytes> = bytes(<mview>) # Or: <mview>.tobytes()
'<hex>' = <mview>.hex()
<list> = list(<mview>) # Returns numbers.
<str> = str(<mview>, 'utf-8') # Or: <bytes>.decode('utf-8')
<int> = int.from_bytes(<mview>, byteorder='big|little', signed=False)
'<hex>' = <bytes>.hex()
```

76
index.html

@ -1566,36 +1566,6 @@ shutil.copytree(from, to) <span class="hljs-comment"># Copies the entir
writer.writerows(rows)
</code></pre></div>
<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
&lt;str&gt; = json.dumps(&lt;object&gt;, ensure_ascii=<span class="hljs-keyword">True</span>, indent=<span class="hljs-keyword">None</span>)
&lt;object&gt; = json.loads(&lt;str&gt;)
</code></pre></div>
<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>
<span class="hljs-keyword">with</span> open(filename, encoding=<span class="hljs-string">'utf-8'</span>) <span class="hljs-keyword">as</span> file:
<span class="hljs-keyword">return</span> json.load(file)
</code></pre></div>
<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>
<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:
json.dump(an_object, file, ensure_ascii=<span class="hljs-keyword">False</span>, indent=<span class="hljs-number">2</span>)
</code></pre></div>
<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
&lt;bytes&gt; = pickle.dumps(&lt;object&gt;)
&lt;object&gt; = pickle.loads(&lt;bytes&gt;)
</code></pre></div>
<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>
<span class="hljs-keyword">with</span> open(filename, <span class="hljs-string">'rb'</span>) <span class="hljs-keyword">as</span> file:
<span class="hljs-keyword">return</span> pickle.load(file)
</code></pre></div>
<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>
<span class="hljs-keyword">with</span> open(filename, <span class="hljs-string">'wb'</span>) <span class="hljs-keyword">as</span> file:
pickle.dump(an_object, file)
</code></pre></div>
<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
db = sqlite3.connect(<span class="hljs-string">'&lt;path&gt;'</span>) <span class="hljs-comment"># Also ':memory:'.</span>
...
@ -1651,6 +1621,36 @@ db = connector.connect(host=&lt;str&gt;, user=&lt;str&gt;, password=&lt;str&gt;,
</code></pre></div>
<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
&lt;str&gt; = json.dumps(&lt;object&gt;, ensure_ascii=<span class="hljs-keyword">True</span>, indent=<span class="hljs-keyword">None</span>)
&lt;object&gt; = json.loads(&lt;str&gt;)
</code></pre></div>
<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>
<span class="hljs-keyword">with</span> open(filename, encoding=<span class="hljs-string">'utf-8'</span>) <span class="hljs-keyword">as</span> file:
<span class="hljs-keyword">return</span> json.load(file)
</code></pre></div>
<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>
<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:
json.dump(an_object, file, ensure_ascii=<span class="hljs-keyword">False</span>, indent=<span class="hljs-number">2</span>)
</code></pre></div>
<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
&lt;bytes&gt; = pickle.dumps(&lt;object&gt;)
&lt;object&gt; = pickle.loads(&lt;bytes&gt;)
</code></pre></div>
<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>
<span class="hljs-keyword">with</span> open(filename, <span class="hljs-string">'rb'</span>) <span class="hljs-keyword">as</span> file:
<span class="hljs-keyword">return</span> pickle.load(file)
</code></pre></div>
<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>
<span class="hljs-keyword">with</span> open(filename, <span class="hljs-string">'wb'</span>) <span class="hljs-keyword">as</span> file:
pickle.dump(an_object, file)
</code></pre></div>
<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>
&lt;int&gt; = &lt;bytes&gt;[&lt;index&gt;] <span class="hljs-comment"># Returns int in range from 0 to 255.</span>
&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 @@ db = connector.connect(host=&lt;str&gt;, user=&lt;str&gt;, password=&lt;str&gt;,
</code></pre></div>
<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;)
&lt;memoryview&gt;.release()
<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;)
&lt;mview&gt;.release() <span class="hljs-comment"># Releases the buffer.</span>
</code></pre></div>
<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>
&lt;mview&gt; = &lt;mview&gt;[&lt;slice&gt;] <span class="hljs-comment"># Returns bytes even if it has only one element.</span>
&lt;file&gt;.write(&lt;mview&gt;)
</code></pre>
<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>
&lt;bytes&gt; = bytes(&lt;mview&gt;) <span class="hljs-comment"># Or: &lt;mview&gt;.tobytes() </span>
<span class="hljs-string">'&lt;hex&gt;'</span> = &lt;mview&gt;.hex()
&lt;list&gt; = list(&lt;mview&gt;) <span class="hljs-comment"># Returns numbers.</span>
&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>
&lt;int&gt; = int.from_bytes(&lt;mview&gt;, byteorder=<span class="hljs-string">'big|little'</span>, signed=<span class="hljs-keyword">False</span>)
<span class="hljs-string">'&lt;hex&gt;'</span> = &lt;bytes&gt;.hex()
</code></pre>
<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
&lt;deque&gt; = deque(&lt;collection&gt;, maxlen=<span class="hljs-keyword">None</span>)
</code></pre></div>

|||||||
100:0
Loading…
Cancel
Save