From 7a2574d4a627fb01d2fdd2526f9d10fcf6c6c8af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Tue, 17 Sep 2019 23:00:27 +0200 Subject: [PATCH] MemoryView --- README.md | 25 ++++++++++++++++++++----- index.html | 31 +++++++++++++++++++++++-------- parse.js | 11 ++++++++++- 3 files changed, 53 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 09d0daf..efec523 100644 --- a/README.md +++ b/README.md @@ -1943,11 +1943,26 @@ from array import array Memory View ----------- -**Used for accessing the internal data of an object that supports the buffer protocol.** +* **Memory View is a seqence that points to the memory of bytes, bytearray or array objects.** +* **Each element can reference a single or multiple consecutive bytes.** +* **Referenced elements can be narrowed down with slicing.** ```python - = memoryview( / / ) -.release() # Releases the buffer. + = memoryview() + = [] # Can be int or float. + = [] # Mview with rearanged elements. + = .cast('') # Cast a memoryview to a new format. +.release() # Releases the buffer. +``` + +```python +.write() + = bytes() # Or: .tobytes() + = .join() # Joins mviews using bytes object as sep. + = list() # Returns numbers. Or: .tolist() + = str(, 'utf-8') # Or: .decode('utf-8') + = int.from_bytes(, byteorder='big|little', signed=False) +'' = .hex() ``` @@ -2217,7 +2232,7 @@ def printer(): reader(adder(printer())) # 100, 101, ..., 109 ``` -

+
Libraries ========= @@ -2253,7 +2268,7 @@ Table ```python # $ pip3 install tabulate import csv, tabulate -with open(, encoding='utf-8', newline='') as file: +with open('test.csv', encoding='utf-8', newline='') as file: rows = csv.reader(file) header = [a.title() for a in next(rows)] table = tabulate.tabulate(rows, header) diff --git a/index.html b/index.html index dfddda6..4ee2476 100644 --- a/index.html +++ b/index.html @@ -1394,16 +1394,16 @@ value = args.<name>
  • 'a+' - Read and write from the end.
  • 't' - Text mode (default).
  • 'b' - Binary mode.
  • -

    Exceptions

      +

    Exceptions

    • 'FileNotFoundError' can be risen when reading with 'r' or 'r+'.
    • 'FileExistsError' can be risen when writing with 'x'.
    • 'IsADirectoryError' and 'PermissionError' can be risen by any.
    • 'OSError' is the parent class of all listed exceptions.
    • -

    File

    <file>.seek(0)                      # Moves to the start of the file.
    +

    File

    <file>.seek(0)                      # Moves to the start of the file.
     <file>.seek(offset)                 # Moves 'offset' chars/bytes from the start.
     <file>.seek(0, 2)                   # Moves to the end of the file.
     <bin_file>.seek(±offset, <anchor>)  # Anchor: 0 start, 1 current pos., 2 end.
    -
    + @@ -1724,11 +1724,26 @@ 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.

    <mview> = memoryview(<bytes> / <bytearray> / <array>)
    -<mview>.release()                            # Releases the buffer.
    +

    #Memory View

      +
    • Memory View is a seqence that points to the memory of bytes, bytearray or array objects.
    • +
    • Each element can reference a single or multiple consecutive bytes.
    • +
    • Referenced elements can be narrowed down with slicing.
    • +
    <mview> = memoryview(<bytes/bytearray/array>)
    +<num>   = <mview>[<index>]                     # Can be int or float.
    +<mview> = <mview>[<slice>]                     # Mview with rearanged elements.
    +<mview> = <mview>.cast('<typecode>')           # Cast a memoryview to a new format.
    +<mview>.release()                              # Releases the buffer.
     
    +
    <bin_file>.write(<mview>)
    +<bytes> = bytes(<mview>)                       # Or: <mview>.tobytes()
    +<bytes> = <bytes>.join(<coll_of_mviews>)       # Joins mviews using bytes object as sep.
    +<list>  = list(<mview>)                        # Returns numbers. Or: <mview>.tolist()
    +<str>   = str(<mview>, 'utf-8')                # Or: <bytes>.decode('utf-8')
    +<int>   = int.from_bytes(<mview>, byteorder='big|little', signed=False)
    +'<hex>' = <mview>.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)
     
    @@ -1925,8 +1940,8 @@ ValueError: malformed node or string reader(adder(printer())) # 100, 101, ..., 109
    -



    -

    Libraries

    #Progress Bar

    # $ pip3 install tqdm
    +


    +

    Libraries

    #Progress Bar

    # $ pip3 install tqdm
     from tqdm import tqdm
     from time import sleep
     for el in tqdm([1, 2, 3]):
    @@ -1946,7 +1961,7 @@ pyplot.clf()                           # Clears figur
     
     

    #Table

    Prints a CSV file as an ASCII table:

    # $ pip3 install tabulate
     import csv, tabulate
    -with open(<filename>, encoding='utf-8', newline='') as file:
    +with open('test.csv', encoding='utf-8', newline='') as file:
         rows   = csv.reader(file)
         header = [a.title() for a in next(rows)]
         table  = tabulate.tabulate(rows, header)
    diff --git a/parse.js b/parse.js
    index 0f2b6fb..d58fdda 100755
    --- a/parse.js
    +++ b/parse.js
    @@ -303,7 +303,8 @@ function highlightCode() {
       fixClasses();
       fixHighlights();
       preventPageBreaks();
    -  insertPageBreak();
    +  fixPageBreaks();
    +  // insertPageBreak();
     }
     
     function setApaches(elements) {
    @@ -338,6 +339,14 @@ function preventPageBreaks() {
       });
     }
     
    +function fixPageBreaks() {
    +  const fileDiv = $('#file').parent()
    +  const modesDiv = $('#file').parent().parent().parent()
    +  modesDiv.after(fileDiv)
    +  const exceptDiv = $('#exceptions-1').parent()
    +  modesDiv.after(exceptDiv)
    +}
    +
     function insertPageBreak() {
       $('
    ').insertBefore($('#libraries').parent()) }