From 05cd30ecf20121a174e7b273ad0f0850679c6f55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Sat, 14 Sep 2019 20:28:54 +0200 Subject: [PATCH] Memoryview --- README.md | 112 ++++++++++++++++++++++++++++++----------------------- index.html | 76 +++++++++++++++++++++--------------- 2 files changed, 108 insertions(+), 80 deletions(-) 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 - = json.dumps(, ensure_ascii=True, indent=None) - = json.loads() -``` - -### 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 - = pickle.dumps() - = pickle.loads() -``` - -### 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=, user=, password=, database=) ``` +JSON +---- +```python +import json + = json.dumps(, ensure_ascii=True, indent=None) + = json.loads() +``` + +### 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 + = pickle.dumps() + = pickle.loads() +``` + +### 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( / / ) -.release() + = memoryview( / / ) +.release() # Releases the buffer. +``` + +```python + = [] # Returns int in range from 0 to 255. + = [] # Returns bytes even if it has only one element. +.write() +``` + +```python + = .join() # Joins elements using bytes object as separator. + = bytes() # Or: .tobytes() +'' = .hex() + = list() # Returns numbers. + = str(, 'utf-8') # Or: .decode('utf-8') + = int.from_bytes(, byteorder='big|little', signed=False) +'' = .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) # Copies the entir writer.writerows(rows) -

#JSON

import json
-<str>    = json.dumps(<object>, ensure_ascii=True, indent=None)
-<object> = json.loads(<str>)
-
- -

Read Object from JSON File

def read_json_file(filename):
-    with open(filename, encoding='utf-8') as file:
-        return json.load(file)
-
- -

Write Object to JSON File

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

import pickle
-<bytes>  = pickle.dumps(<object>)
-<object> = pickle.loads(<bytes>)
-
- -

Read Object from File

def read_pickle_file(filename):
-    with open(filename, 'rb') as file:
-        return pickle.load(file)
-
- -

Write Object to File

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.

import sqlite3
 db = sqlite3.connect('<path>')                  # Also ':memory:'.
 ...
@@ -1651,6 +1621,36 @@ db = connector.connect(host=<str>, user=<str>, password=<str>,
 
+

#JSON

import json
+<str>    = json.dumps(<object>, ensure_ascii=True, indent=None)
+<object> = json.loads(<str>)
+
+ +

Read Object from JSON File

def read_json_file(filename):
+    with open(filename, encoding='utf-8') as file:
+        return json.load(file)
+
+ +

Write Object to JSON File

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

import pickle
+<bytes>  = pickle.dumps(<object>)
+<object> = pickle.loads(<bytes>)
+
+ +

Read Object from File

def read_pickle_file(filename):
+    with open(filename, 'rb') as file:
+        return pickle.load(file)
+
+ +

Write Object to File

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'.

<bytes> = b'<str>'                       # Only accepts ASCII characters and \x00 - \xff.
 <int>   = <bytes>[<index>]               # Returns int in range from 0 to 255.
 <bytes> = <bytes>[<slice>]               # Returns bytes even if it has only one element.
@@ -1724,11 +1724,23 @@ db = connector.connect(host=<str>, user=<str>, password=<str>,
 
-

#Memory View

Used for accessing the internal data of an object that supports the buffer protocol.

<memoryview> = memoryview(<bytes> / <bytearray> / <array>)
-<memoryview>.release()
+

#Memory View

Used for accessing the internal data of an object that supports the buffer protocol.

<mview> = memoryview(<bytes> / <bytearray> / <array>)
+<mview>.release()                         # Releases the buffer.
 
+
<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>)
+
+
<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()
+

#Deque

A thread-safe list with efficient appends and pops from either side. Pronounced "deck".

from collections import deque
 <deque> = deque(<collection>, maxlen=None)