diff --git a/README.md b/README.md index 756b62b..82173e8 100644 --- a/README.md +++ b/README.md @@ -320,7 +320,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) # On [\n\r\f\v\x1c-\x1e\x85\u2028\u2029] and \r\n. - = .join() # Joins elements using string as a separator. + = .join() # Joins elements by using string as a separator. ``` ```python @@ -524,8 +524,7 @@ from math import log, log10, log2 # Log accepts base as second arg. ### Statistics ```python -from statistics import mean, median, mode # Mode returns the most common value. -from statistics import variance, stdev # Also: pvariance, pstdev, quantiles. +from statistics import mean, median, mode # Also: variance, stdev, quantiles. ``` ### Random @@ -606,7 +605,7 @@ import zoneinfo, dateutil.tz * **Times and datetimes that have defined timezone are called aware and ones that don't, naive. If object is naive, it is presumed to be in the system's timezone!** * **`'fold=1'` means the second pass in case of time jumping back for one hour.** * **Timedelta normalizes arguments to ±days, seconds (< 86 400) and microseconds (< 1M). Its str() method returns `'[±D, ]H:MM:SS[.…]'` and total_seconds() a float of all seconds.** -* **Use `'.weekday()'` to get the day of the week as an int, with Monday being 0.** +* **Use `'.weekday()'` to get the day of the week as an integer, with Monday being 0.** ### Now ```python @@ -1076,7 +1075,7 @@ class Person: ``` ### Slots -**Mechanism that restricts objects to attributes listed in 'slots', reduces their memory footprint.** +**Mechanism that restricts objects to attributes listed in 'slots'.** ```python class MyClassWithSlots: @@ -1097,8 +1096,7 @@ Duck Types **A duck type is an implicit type that prescribes a set of special methods. Any object that has those methods defined is considered a member of that duck type.** ### Comparable -* **If eq() method is not overridden, it returns `'id(self) == id(other)'`, which is the same as `'self is other'`.** -* **That means all user-defined objects compare not equal by default.** +* **If eq() method is not overridden, it returns `'id(self) == id(other)'`, which is the same as `'self is other'`. That means all user-defined objects compare not equal by default, because id() returns object's unique identification number (its memory address).** * **Only the left side object has eq() method called, unless it returns NotImplemented, in which case the right object is consulted. False is returned if both return NotImplemented.** * **Ne() automatically works on any object that has eq() defined.** @@ -1113,9 +1111,8 @@ class MyComparable: ``` ### Hashable -* **Hashable object needs both hash() and eq() methods and its hash value should never change.** -* **Hashable objects that compare equal must have the same hash value, meaning default hash() that returns `'id(self)'` will not do.** -* **That is why Python automatically makes classes unhashable if you only implement eq().** +* **Hashable object needs both hash() and eq() methods and its hash value must not change.** +* **Hashable objects that compare equal must have the same hash value, meaning default hash() that returns `'id(self)'` will not do. That is why Python automatically makes classes unhashable if you only implement eq().** ```python class MyHashable: @@ -1133,7 +1130,7 @@ class MyHashable: ``` ### Sortable -* **With 'total_ordering' decorator, you only need to provide eq() and one of lt(), gt(), le() or ge() special methods and the rest will be automatically generated.** +* **With 'total_ordering' decorator, you only need to provide eq() and one of lt(), gt(), le() or ge() special methods (used by <, >, <=, >=) and the rest will be automatically generated.** * **Functions sorted() and min() only require lt() method, while max() only requires gt(). However, it is best to define them all so that confusion doesn't arise in other contexts.** * **When two lists, strings or dataclasses are compared, their values get compared in order until a pair of unequal values is found. The comparison of this two values is then returned. The shorter sequence is considered smaller in case of all values being equal.** * **To sort collection of strings in proper alphabetical order pass `'key=locale.strxfrm'` to sorted() after running `'locale.setlocale(locale.LC_COLLATE, "en_US.UTF-8")'`.** @@ -1205,7 +1202,7 @@ class Counter: ### Context Manager * **With statements only work on objects that have enter() and exit() special methods.** * **Enter() should lock the resources and optionally return an object.** -* **Exit() should release the resources.** +* **Exit() should release the resources (for example close a file).** * **Any exception that happens inside the with block is passed to the exit() method.** * **The exit() method can suppress the exception by returning a true value.** ```python @@ -1530,7 +1527,7 @@ Input ``` * **Reads a line from the user input or pipe if present (trailing newline gets stripped).** * **If argument is passed, it gets printed to the standard output before input is read.** -* **EOFError is raised if user hits EOF (ctrl-d/ctrl-z⏎) or if input stream is exhausted.** +* **EOFError is raised if user hits EOF (ctrl-d/ctrl-z⏎) or if stream is already exhausted.** Command Line Arguments @@ -1955,14 +1952,14 @@ Bytes ```python = b'' # Only accepts ASCII characters and \x00-\xff. - = [index] # Returns an int in range from 0 to 255. + = [index] # Returns an integer in range from 0 to 255. = [] # Returns bytes even if it has only one element. - = .join() # Joins elements using bytes as a separator. + = .join() # Joins elements by using bytes as a separator. ``` ### Encode ```python - = bytes() # Ints must be in range from 0 to 255. + = bytes() # Integers must be in range from 0 to 255. = bytes(, 'utf-8') # Encodes the string. Also .encode(). = bytes.fromhex('') # Hex pairs can be separated by whitespaces. = .to_bytes(n_bytes, …) # `byteorder='big/little', signed=False`. @@ -1970,7 +1967,7 @@ Bytes ### Decode ```python - = list() # Returns ints in range from 0 to 255. + = list() # Returns integers in range from 0 to 255. = str(, 'utf-8') # Returns a string. Also .decode(). = .hex() # Returns hex pairs. Accepts `sep=`. = int.from_bytes(, …) # `byteorder='big/little', signed=False`. @@ -2059,7 +2056,7 @@ Memory View ```python = memoryview() # Immutable if bytes is passed, else mutable. - = [index] # Returns int or float. Bytes if format is 'c'. + = [index] # Returns int/float. Bytes if format is 'c'. = [] # Returns memoryview with rearranged elements. = .cast('') # Only works between B/b/c and other types. .release() # Releases memory buffer of the base object. @@ -2126,7 +2123,7 @@ first_element = op.methodcaller('pop', 0)() Match Statement --------------- -**Executes the first block with matching pattern. Added in Python 3.10.** +**Executes the first block with matching pattern.** ```python match : @@ -2147,7 +2144,7 @@ match : = {: , ...} # Matches dictionary with matching items. = (=, ...) # Matches object with matching attributes. ``` -* **Sequence pattern can also be written as a tuple.** +* **Sequence pattern can also be written as a tuple, i.e. `'(, [...])'`.** * **Use `'*'` and `'**'` in sequence/mapping patterns to bind remaining items.** * **Sequence pattern must match all items of the collection, while mapping pattern does not.** * **Patterns can be surrounded with brackets to override precedence (`'|'` > `'as'` > `','`).** diff --git a/index.html b/index.html index 02786fc..7818c96 100644 --- a/index.html +++ b/index.html @@ -56,7 +56,7 @@
- +
@@ -312,7 +312,7 @@ Point(x=1, y=2
<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)    # On [\n\r\f\v\x1c-\x1e\x85\u2028\u2029] and \r\n.
-<str>  = <str>.join(<coll_of_strings>)       # Joins elements using string as a separator.
+<str>  = <str>.join(<coll_of_strings>)       # Joins elements by using string as a separator.
 
<bool> = <sub_str> in <str>                  # Checks if string contains the substring.
 <bool> = <str>.startswith(<sub_str>)         # Pass tuple of strings for multiple options.
@@ -480,8 +480,7 @@ Point(x=1, y=2
 from math import log, log10, log2            # Log accepts base as second arg.
 
-

Statistics

from statistics import mean, median, mode    # Mode returns the most common value.
-from statistics import variance, stdev       # Also: pvariance, pstdev, quantiles.
+

Statistics

from statistics import mean, median, mode    # Also: variance, stdev, quantiles.
 

Random

from random import random, randint, uniform  # Also: gauss, choice, shuffle, seed.
@@ -539,7 +538,7 @@ shuffle(<list>)                              #
 
  • Times and datetimes that have defined timezone are called aware and ones that don't, naive. If object is naive, it is presumed to be in the system's timezone!
  • 'fold=1' means the second pass in case of time jumping back for one hour.
  • Timedelta normalizes arguments to ±days, seconds (< 86 400) and microseconds (< 1M). Its str() method returns '[±D, ]H:MM:SS[.…]' and total_seconds() a float of all seconds.
  • -
  • Use '<D/DT>.weekday()' to get the day of the week as an int, with Monday being 0.
  • +
  • Use '<D/DT>.weekday()' to get the day of the week as an integer, with Monday being 0.
  • Now

    <D/DTn> = D/DT.today()                      # Current local date or naive DT. Also DT.now().
     <DTa>   = DT.now(<tzinfo>)                  # Aware DT from current time in passed timezone.
    @@ -920,7 +919,7 @@ P = make_dataclass('P', [(>>> person.name
     'Guido van Rossum'
     
    -

    Slots

    Mechanism that restricts objects to attributes listed in 'slots', reduces their memory footprint.

    class MyClassWithSlots:
    +

    Slots

    Mechanism that restricts objects to attributes listed in 'slots'.

    class MyClassWithSlots:
         __slots__ = ['a']
         def __init__(self):
             self.a = 1
    @@ -932,8 +931,7 @@ P = make_dataclass('P', [(#Duck Types

    A duck type is an implicit type that prescribes a set of special methods. Any object that has those methods defined is considered a member of that duck type.

    Comparable

      -
    • If eq() method is not overridden, it returns 'id(self) == id(other)', which is the same as 'self is other'.
    • -
    • That means all user-defined objects compare not equal by default.
    • +
    • If eq() method is not overridden, it returns 'id(self) == id(other)', which is the same as 'self is other'. That means all user-defined objects compare not equal by default, because id() returns object's unique identification number (its memory address).
    • Only the left side object has eq() method called, unless it returns NotImplemented, in which case the right object is consulted. False is returned if both return NotImplemented.
    • Ne() automatically works on any object that has eq() defined.
    class MyComparable:
    @@ -949,9 +947,8 @@ P = make_dataclass('P', [(Hashable
      -
    • Hashable object needs both hash() and eq() methods and its hash value should never change.
    • -
    • Hashable objects that compare equal must have the same hash value, meaning default hash() that returns 'id(self)' will not do.
    • -
    • That is why Python automatically makes classes unhashable if you only implement eq().
    • +
    • Hashable object needs both hash() and eq() methods and its hash value must not change.
    • +
    • Hashable objects that compare equal must have the same hash value, meaning default hash() that returns 'id(self)' will not do. That is why Python automatically makes classes unhashable if you only implement eq().
    class MyHashable:
         def __init__(self, a):
             self._a = a
    @@ -968,7 +965,7 @@ P = make_dataclass('P', [(Sortable
      -
    • With 'total_ordering' decorator, you only need to provide eq() and one of lt(), gt(), le() or ge() special methods and the rest will be automatically generated.
    • +
    • With 'total_ordering' decorator, you only need to provide eq() and one of lt(), gt(), le() or ge() special methods (used by <, >, <=, >=) and the rest will be automatically generated.
    • Functions sorted() and min() only require lt() method, while max() only requires gt(). However, it is best to define them all so that confusion doesn't arise in other contexts.
    • When two lists, strings or dataclasses are compared, their values get compared in order until a pair of unequal values is found. The comparison of this two values is then returned. The shorter sequence is considered smaller in case of all values being equal.
    • To sort collection of strings in proper alphabetical order pass 'key=locale.strxfrm' to sorted() after running 'locale.setlocale(locale.LC_COLLATE, "en_US.UTF-8")'.
    • @@ -1036,7 +1033,7 @@ P = make_dataclass('P', [(Context Manager
      • With statements only work on objects that have enter() and exit() special methods.
      • Enter() should lock the resources and optionally return an object.
      • -
      • Exit() should release the resources.
      • +
      • Exit() should release the resources (for example close a file).
      • Any exception that happens inside the with block is passed to the exit() method.
      • The exit() method can suppress the exception by returning a true value.
      class MyOpen:
      @@ -1315,7 +1312,7 @@ pprint(<collection>, width=80, depth=
       
    • Reads a line from the user input or pipe if present (trailing newline gets stripped).
    • If argument is passed, it gets printed to the standard output before input is read.
    • -
    • EOFError is raised if user hits EOF (ctrl-d/ctrl-z⏎) or if input stream is exhausted.
    • +
    • EOFError is raised if user hits EOF (ctrl-d/ctrl-z⏎) or if stream is already exhausted.

    #Command Line Arguments

    import sys
     scripts_path = sys.argv[0]
    @@ -1632,19 +1629,19 @@ CompletedProcess(args=['bc', #Bytes

    A 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 an int in range from 0 to 255.
    +<int>   = <bytes>[index]                 # Returns an integer 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 as a separator.
    +<bytes> = <bytes>.join(<coll_of_bytes>)  # Joins elements by using bytes as a separator.
     
    -

    Encode

    <bytes> = bytes(<coll_of_ints>)          # Ints must be in range from 0 to 255.
    +

    Encode

    <bytes> = bytes(<coll_of_ints>)          # Integers must be in range from 0 to 255.
     <bytes> = bytes(<str>, 'utf-8')          # Encodes the string. Also <str>.encode().
     <bytes> = bytes.fromhex('<hex>')         # Hex pairs can be separated by whitespaces.
     <bytes> = <int>.to_bytes(n_bytes, …)     # `byteorder='big/little', signed=False`.
     
    -

    Decode

    <list>  = list(<bytes>)                  # Returns ints in range from 0 to 255.
    +

    Decode

    <list>  = list(<bytes>)                  # Returns integers in range from 0 to 255.
     <str>   = str(<bytes>, 'utf-8')          # Returns a string. Also <bytes>.decode().
     <str>   = <bytes>.hex()                  # Returns hex pairs. Accepts `sep=<str>`.
     <int>   = int.from_bytes(<bytes>, …)     # `byteorder='big/little', signed=False`.
    @@ -1713,7 +1710,7 @@ CompletedProcess(args=['bc', # Writes array's memory to the binary file.
     

    #Memory View

    A sequence object that points to the memory of another bytes-like object. Each element can reference a single or multiple consecutive bytes, depending on format. Order and number of elements can be changed with slicing.

    <mview> = memoryview(<bytes/bytearray/array>)  # Immutable if bytes is passed, else mutable.
    -<obj>   = <mview>[index]                       # Returns int or float. Bytes if format is 'c'.
    +<obj>   = <mview>[index]                       # Returns int/float. Bytes if format is 'c'.
     <mview> = <mview>[<slice>]                     # Returns memoryview with rearranged elements.
     <mview> = <mview>.cast('<typecode>')           # Only works between B/b/c and other types.
     <mview>.release()                              # Releases memory buffer of the base object.
    @@ -1761,7 +1758,7 @@ first_element    = op.methodcaller('pop', Most operators call the object's special method that is named after them (second object is passed as an argument), while logical operators call their own code that relies on bool().
     
  • Comparisons can be chained: 'x < y < z' gets converted to '(x < y) and (y < z)'.
  • -

    #Match Statement

    Executes the first block with matching pattern. Added in Python 3.10.

    match <object/expression>:
    +

    #Match Statement

    Executes the first block with matching pattern.

    match <object/expression>:
         case <pattern> [if <condition>]:
             <code>
         ...
    @@ -1780,7 +1777,7 @@ first_element    = op.methodcaller('pop', 
      -
    • Sequence pattern can also be written as a tuple.
    • +
    • Sequence pattern can also be written as a tuple, i.e. '(<patt_1>, [...])'.
    • Use '*<name>' and '**<name>' in sequence/mapping patterns to bind remaining items.
    • Sequence pattern must match all items of the collection, while mapping pattern does not.
    • Patterns can be surrounded with brackets to override precedence ('|' > 'as' > ',').
    • @@ -2945,7 +2942,7 @@ $ deactivate # Deactivates the active