From 0e9cbd302692623fc21b3c3f67d8adc24afdc806 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Sat, 29 Jun 2019 22:42:53 +0200 Subject: [PATCH] Bytes --- README.md | 17 +++++++++-------- index.html | 17 +++++++++-------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 1999681..dd3ab86 100644 --- a/README.md +++ b/README.md @@ -303,7 +303,7 @@ String = .split() # Splits on one or more whitespace characters. = .split(sep=None, maxsplit=-1) # Splits on 'sep' str at most 'maxsplit' times. = .splitlines(keepends=False) # Splits on line breaks. Keeps them if 'keepends'. - = .join() # Joins elements using string as separator. + = .join() # Joins elements using string as separator. ``` ```python @@ -1475,23 +1475,24 @@ Bytes **Bytes object is an immutable sequence of single bytes. Mutable version is called 'bytearray'.** ```python - = b'' - = [] - = [] - = list() - = b''.join() + = b'' # Only accepts ASCII characters and \x00 - \xff. + = [] # Returns int in range from 0 to 255. + = [] # Returns bytes even if it has only one element. + = .join() # Joins elements using bytes object as separator. ``` ### Encode ```python - = .encode(encoding='utf-8') + = .encode('utf-8') # Or: bytes(, 'utf-8') + = bytes() # Ints must be in range from 0 to 255. = .to_bytes(, byteorder='big|little', signed=False) = bytes.fromhex('') ``` ### Decode ```python - = .decode(encoding='utf-8') + = .decode('utf-8') # Or: str(, 'utf-8') + = list() # Returns ints in range from 0 to 255. = int.from_bytes(, byteorder='big|little', signed=False) '' = .hex() ``` diff --git a/index.html b/index.html index f82e628..4e784d4 100644 --- a/index.html +++ b/index.html @@ -405,7 +405,7 @@ to_exclusive = <range>.stop
<list> = <str>.split()                       # Splits on one or more whitespace characters.
 <list> = <str>.split(sep=None, maxsplit=-1)  # Splits on 'sep' str at most 'maxsplit' times.
 <list> = <str>.splitlines(keepends=False)    # Splits on line breaks. Keeps them if 'keepends'.
-<str>  = <str>.join(<collection>)            # Joins elements using string as separator.
+<str>  = <str>.join(<coll_of_strings>)       # Joins elements using string as separator.
 
<str>  = <str>.replace(old, new [, count])   # Replaces 'old' with 'new' at most 'count' times.
 <bool> = <str>.startswith(<sub_str>)         # Pass tuple of strings for multiple options.
@@ -1289,19 +1289,20 @@ db.commit()
 

#Bytes

Bytes object is an immutable sequence of single bytes. Mutable version is called 'bytearray'.

-
<bytes> = b'<str>'
-<int>   = <bytes>[<index>]
-<bytes> = <bytes>[<slice>]
-<ints>  = list(<bytes>)
-<bytes> = b''.join(<coll_of_bytes>)
+
<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.
+<bytes> = <bytes>.join(<coll_of_bytes>)  # Joins elements using bytes object as separator. 
 

Encode

-
<bytes> = <str>.encode(encoding='utf-8')
+
<bytes> = <str>.encode('utf-8')          # Or: bytes(<str>, 'utf-8')
+<bytes> = bytes(<coll_of_ints>)          # Ints must be in range from 0 to 255.
 <bytes> = <int>.to_bytes(<length>, byteorder='big|little', signed=False)
 <bytes> = bytes.fromhex('<hex>')
 

Decode

-
<str>   = <bytes>.decode(encoding='utf-8')
+
<str>   = <bytes>.decode('utf-8')        # Or: str(<bytes>, 'utf-8')
+<list>  = list(<bytes>)                  # Returns ints in range from 0 to 255.
 <int>   = int.from_bytes(<bytes>, byteorder='big|little', signed=False)
 '<hex>' = <bytes>.hex()