diff --git a/README.md b/README.md index 16160aa..4f0f9c8 100644 --- a/README.md +++ b/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() ``` diff --git a/index.html b/index.html index d59350c..d7cf999 100644 --- a/index.html +++ b/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 -<str> = json.dumps(<object>, ensure_ascii=<span class="hljs-keyword">True</span>, indent=<span class="hljs-keyword">None</span>) -<object> = json.loads(<str>) -</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 -<bytes> = pickle.dumps(<object>) -<object> = pickle.loads(<bytes>) -</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">'<path>'</span>) <span class="hljs-comment"># Also ':memory:'.</span> ... @@ -1651,6 +1621,36 @@ db = connector.connect(host=<str>, user=<str>, password=<str>, </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 +<str> = json.dumps(<object>, ensure_ascii=<span class="hljs-keyword">True</span>, indent=<span class="hljs-keyword">None</span>) +<object> = json.loads(<str>) +</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 +<bytes> = pickle.dumps(<object>) +<object> = pickle.loads(<bytes>) +</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"><bytes> = <span class="hljs-string">b'<str>'</span> <span class="hljs-comment"># Only accepts ASCII characters and \x00 - \xff.</span> <int> = <bytes>[<index>] <span class="hljs-comment"># Returns int in range from 0 to 255.</span> <bytes> = <bytes>[<slice>] <span class="hljs-comment"># Returns bytes even if it has only one element.</span> @@ -1724,11 +1724,23 @@ db = connector.connect(host=<str>, user=<str>, password=<str>, </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"><memoryview> = memoryview(<bytes> / <bytearray> / <array>) -<memoryview>.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"><mview> = memoryview(<bytes> / <bytearray> / <array>) +<mview>.release() <span class="hljs-comment"># Releases the buffer.</span> </code></pre></div> +<pre><code class="python language-python hljs"><num> = <mview>[<index>] <span class="hljs-comment"># Returns int in range from 0 to 255.</span> +<mview> = <mview>[<slice>] <span class="hljs-comment"># Returns bytes even if it has only one element.</span> +<file>.write(<mview>) +</code></pre> +<pre><code class="python language-python hljs"><bytes> = <bytes>.join(<coll_of_mviews>) <span class="hljs-comment"># Joins elements using bytes object as separator.</span> +<bytes> = bytes(<mview>) <span class="hljs-comment"># Or: <mview>.tobytes() </span> +<span class="hljs-string">'<hex>'</span> = <mview>.hex() +<list> = list(<mview>) <span class="hljs-comment"># Returns numbers.</span> +<str> = str(<mview>, <span class="hljs-string">'utf-8'</span>) <span class="hljs-comment"># Or: <bytes>.decode('utf-8')</span> +<int> = int.from_bytes(<mview>, byteorder=<span class="hljs-string">'big|little'</span>, signed=<span class="hljs-keyword">False</span>) +<span class="hljs-string">'<hex>'</span> = <bytes>.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 <deque> = deque(<collection>, maxlen=<span class="hljs-keyword">None</span>) </code></pre></div>