diff --git a/README.md b/README.md index de65e87..8327a79 100644 --- a/README.md +++ b/README.md @@ -200,23 +200,23 @@ Iterator ### Itertools ```python -from itertools import count, repeat, cycle, chain, islice +import itertools as it ``` ```python - = count(start=0, step=1) # Returns updated value endlessly. Accepts floats. - = repeat( [, times]) # Returns element endlessly or 'times' times. - = cycle() # Repeats the sequence endlessly. + = it.count(start=0, step=1) # Returns updated value endlessly. Accepts floats. + = it.repeat( [, times]) # Returns element endlessly or 'times' times. + = it.cycle() # Repeats the sequence endlessly. ``` ```python - = chain(, [, ...]) # Empties collections in order (figuratively). - = chain.from_iterable() # Empties collections inside a collection in order. + = it.chain(, [, ...]) # Empties collections in order (figuratively). + = it.chain.from_iterable() # Empties collections inside a collection in order. ``` ```python - = islice(, to_exclusive) # Only returns first 'to_exclusive' elements. - = islice(, from_inclusive, …) # `to_exclusive, +step_size`. Indices can be None. + = it.islice(, to_exclusive) # Only returns first 'to_exclusive' elements. + = it.islice(, from_inc, …) # `to_exclusive, +step_size`. Indices can be None. ``` @@ -486,11 +486,11 @@ Format Numbers ------- ```python - = int() # Or: math.floor() - = float() # Or: - = complex(real=0, imag=0) # Or: ± j - = fractions.Fraction(0, 1) # Or: Fraction(numerator=0, denominator=1) - = decimal.Decimal() # Or: Decimal((sign, digits, exponent)) + = int() # Or: math.floor() + = float() # Or: + = complex(real=0, imag=0) # Or: ± j + = fractions.Fraction(0, 1) # Or: Fraction(numerator=0, denominator=1) + = decimal.Decimal() # Or: Decimal((sign, digits, exponent)) ``` * **`'int()'` and `'float()'` raise ValueError on malformed strings.** * **Decimal numbers are stored exactly, unlike most floats where `'1.1 + 2.2 != 3.3'`.** @@ -499,47 +499,46 @@ Numbers ### Basic Functions ```python - = pow(, ) # Or: ** - = abs() # = abs() - = round( [, ±ndigits]) # `round(126, -1) == 130` + = pow(, ) # Or: ** + = abs() # = abs() + = round( [, ±ndigits]) # `round(126, -1) == 130` ``` ### Math ```python -from math import e, pi, inf, nan, isinf, isnan -from math import sin, cos, tan, asin, acos, atan, degrees, radians -from math import log, log10, log2 +from math import e, pi, inf, nan, isinf, isnan # ` == nan` is always False. +from math import sin, cos, tan, asin, acos, atan # Also: degrees, radians. +from math import log, log10, log2 # Log can accept base as second arg. ``` ### Statistics ```python -from statistics import mean, median, variance, stdev, quantiles, groupby +from statistics import mean, median, variance # Also: stdev, quantiles, groupby. ``` ### Random ```python -from random import random, randint, choice, shuffle, gauss, seed - - = random() # A float inside [0, 1). - = randint(from_inc, to_inc) # An int inside [from_inc, to_inc]. - = choice() # Keeps the sequence intact. +from random import random, randint, choice # Also shuffle, gauss, triangular, seed. + = random() # A float inside [0, 1). + = randint(from_inc, to_inc) # An int inside [from_inc, to_inc]. + = choice() # Keeps the sequence intact. ``` ### Bin, Hex ```python - = ±0b # Or: ±0x - = int('±', 2) # Or: int('±', 16) - = int('±0b', 0) # Or: int('±0x', 0) - = bin() # Returns '[-]0b'. + = ±0b # Or: ±0x + = int('±', 2) # Or: int('±', 16) + = int('±0b', 0) # Or: int('±0x', 0) + = bin() # Returns '[-]0b'. ``` ### Bitwise Operators ```python - = & # And (0b1100 & 0b1010 == 0b1000). - = | # Or (0b1100 | 0b1010 == 0b1110). - = ^ # Xor (0b1100 ^ 0b1010 == 0b0110). - = << n_bits # Left shift. Use >> for right. - = ~ # Not. Also - - 1. + = & # And (0b1100 & 0b1010 == 0b1000). + = | # Or (0b1100 | 0b1010 == 0b1110). + = ^ # Xor (0b1100 ^ 0b1010 == 0b0110). + = << n_bits # Left shift. Use >> for right. + = ~ # Not. Also - - 1. ``` @@ -549,39 +548,40 @@ Combinatorics * **If you want to print the iterator, you need to pass it to the list() function first!** ```python -from itertools import product, combinations, combinations_with_replacement, permutations +import itertools as it ``` ```python ->>> product([0, 1], repeat=3) -[(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), ..., (1, 1, 1)] +>>> it.product([0, 1], repeat=3) +[(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), + (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)] ``` ```python ->>> product('abc', 'abc') # a b c -[('a', 'a'), ('a', 'b'), ('a', 'c'), # a x x x - ('b', 'a'), ('b', 'b'), ('b', 'c'), # b x x x - ('c', 'a'), ('c', 'b'), ('c', 'c')] # c x x x +>>> it.product('abc', 'abc') # a b c +[('a', 'a'), ('a', 'b'), ('a', 'c'), # a x x x + ('b', 'a'), ('b', 'b'), ('b', 'c'), # b x x x + ('c', 'a'), ('c', 'b'), ('c', 'c')] # c x x x ``` ```python ->>> combinations('abc', 2) # a b c -[('a', 'b'), ('a', 'c'), # a . x x - ('b', 'c')] # b . . x +>>> it.combinations('abc', 2) # a b c +[('a', 'b'), ('a', 'c'), # a . x x + ('b', 'c')] # b . . x ``` ```python ->>> combinations_with_replacement('abc', 2) # a b c -[('a', 'a'), ('a', 'b'), ('a', 'c'), # a x x x - ('b', 'b'), ('b', 'c'), # b . x x - ('c', 'c')] # c . . x +>>> it.combinations_with_replacement('abc', 2) # a b c +[('a', 'a'), ('a', 'b'), ('a', 'c'), # a x x x + ('b', 'b'), ('b', 'c'), # b . x x + ('c', 'c')] # c . . x ``` ```python ->>> permutations('abc', 2) # a b c -[('a', 'b'), ('a', 'c'), # a . x x - ('b', 'a'), ('b', 'c'), # b x . x - ('c', 'a'), ('c', 'b')] # c x x . +>>> it.permutations('abc', 2) # a b c +[('a', 'b'), ('a', 'c'), # a . x x + ('b', 'a'), ('b', 'c'), # b x . x + ('c', 'a'), ('c', 'b')] # c x x . ``` @@ -1701,34 +1701,34 @@ import os, shutil, subprocess ``` ```python -os.chdir() # Changes the current working directory. -os.mkdir(, mode=0o777) # Creates a directory. Mode is in octal. -os.makedirs(, mode=0o777) # Creates all path's dirs. Also: `exist_ok=False`. +os.chdir() # Changes the current working directory. +os.mkdir(, mode=0o777) # Creates a directory. Permissions are in octal. +os.makedirs(, mode=0o777) # Creates all path's dirs. Also: `exist_ok=False`. ``` ```python -shutil.copy(from, to) # Copies the file. 'to' can exist or be a dir. -shutil.copytree(from, to) # Copies the directory. 'to' must not exist. +shutil.copy(from, to) # Copies the file. 'to' can exist or be a dir. +shutil.copytree(from, to) # Copies the directory. 'to' must not exist. ``` ```python -os.rename(from, to) # Renames/moves the file or directory. -os.replace(from, to) # Same, but overwrites 'to' if it exists. +os.rename(from, to) # Renames/moves the file or directory. +os.replace(from, to) # Same, but overwrites 'to' if it exists. ``` ```python -os.remove() # Deletes the file. -os.rmdir() # Deletes the empty directory. -shutil.rmtree() # Deletes the directory. +os.remove() # Deletes the file. +os.rmdir() # Deletes the empty directory. +shutil.rmtree() # Deletes the directory. ``` * **Paths can be either strings, Paths or DirEntry objects.** * **Functions report OS related errors by raising either OSError or one of its [subclasses](#exceptions-1).** ### Shell Commands ```python - = os.popen('') # Executes command in sh/cmd and returns its stdout pipe. - = .read(size=-1) # Reads 'size' chars or until EOF. Also readline/s(). - = .close() # Closes the pipe. Returns None on success, int on error. + = os.popen('') # Executes command in sh/cmd. Returns its stdout pipe. + = .read(size=-1) # Reads 'size' chars or until EOF. Also readline/s(). + = .close() # Closes the pipe. Returns None on success. ``` #### Sends '1 + 1' to the basic calculator and captures its output: @@ -1754,8 +1754,8 @@ JSON ```python import json - = json.dumps() # Converts object to JSON string. - = json.loads() # Converts JSON string to object. + = json.dumps() # Converts object to JSON string. + = json.loads() # Converts JSON string to object. ``` ### Read Object from JSON File @@ -1779,8 +1779,8 @@ Pickle ```python import pickle - = pickle.dumps() # Converts object to bytes object. - = pickle.loads() # Converts bytes object to object. + = pickle.dumps() # Converts object to bytes object. + = pickle.loads() # Converts bytes object to object. ``` ### Read Object from File diff --git a/index.html b/index.html index f82571f..16b9665 100644 --- a/index.html +++ b/index.html @@ -54,7 +54,7 @@
- +
@@ -220,18 +220,18 @@ Point(x=1, y=2 <list> = list(<iter>) # Returns a list of iterator's remaining elements. -

Itertools

from itertools import count, repeat, cycle, chain, islice
+

Itertools

import itertools as it
 
-
<iter> = count(start=0, step=1)             # Returns updated value endlessly. Accepts floats.
-<iter> = repeat(<el> [, times])             # Returns element endlessly or 'times' times.
-<iter> = cycle(<collection>)                # Repeats the sequence endlessly.
+
<iter> = it.count(start=0, step=1)          # Returns updated value endlessly. Accepts floats.
+<iter> = it.repeat(<el> [, times])          # Returns element endlessly or 'times' times.
+<iter> = it.cycle(<collection>)             # Repeats the sequence endlessly.
 
-
<iter> = chain(<coll_1>, <coll_2> [, ...])  # Empties collections in order (figuratively).
-<iter> = chain.from_iterable(<coll>)        # Empties collections inside a collection in order.
+
<iter> = it.chain(<coll>, <coll> [, ...])   # Empties collections in order (figuratively).
+<iter> = it.chain.from_iterable(<coll>)     # Empties collections inside a collection in order.
 
-
<iter> = islice(<coll>, to_exclusive)       # Only returns first 'to_exclusive' elements.
-<iter> = islice(<coll>, from_inclusive, …)  # `to_exclusive, +step_size`. Indices can be None.
+
<iter> = it.islice(<coll>, to_exclusive)    # Only returns first 'to_exclusive' elements.
+<iter> = it.islice(<coll>, from_inc, …)     # `to_exclusive, +step_size`. Indices can be None.
 

#Generator

  • Any function that contains a yield statement returns a generator.
  • @@ -444,11 +444,11 @@ Point(x=1, y=2 {90:X} # '5A'
-

#Numbers

<int>      = int(<float/str/bool>)       # Or: math.floor(<float>)
-<float>    = float(<int/str/bool>)       # Or: <real>e±<int>
-<complex>  = complex(real=0, imag=0)     # Or: <real> ± <real>j
-<Fraction> = fractions.Fraction(0, 1)    # Or: Fraction(numerator=0, denominator=1)
-<Decimal>  = decimal.Decimal(<str/int>)  # Or: Decimal((sign, digits, exponent))
+

#Numbers

<int>      = int(<float/str/bool>)                # Or: math.floor(<float>)
+<float>    = float(<int/str/bool>)                # Or: <real>e±<int>
+<complex>  = complex(real=0, imag=0)              # Or: <real> ± <real>j
+<Fraction> = fractions.Fraction(0, 1)             # Or: Fraction(numerator=0, denominator=1)
+<Decimal>  = decimal.Decimal(<str/int>)           # Or: Decimal((sign, digits, exponent))
 
    @@ -457,67 +457,67 @@ Point(x=1, y=2
  • Floats can be compared with: 'math.isclose(<float>, <float>)'.
  • Precision of decimal operations is set with: 'decimal.getcontext().prec = <int>'.
-

Basic Functions

<num> = pow(<num>, <num>)                # Or: <num> ** <num>
-<num> = abs(<num>)                       # <float> = abs(<complex>)
-<num> = round(<num> [, ±ndigits])        # `round(126, -1) == 130`
+

Basic Functions

<num> = pow(<num>, <num>)                         # Or: <num> ** <num>
+<num> = abs(<num>)                                # <float> = abs(<complex>)
+<num> = round(<num> [, ±ndigits])                 # `round(126, -1) == 130`
 
-

Math

from math import e, pi, inf, nan, isinf, isnan
-from math import sin, cos, tan, asin, acos, atan, degrees, radians
-from math import log, log10, log2
+

Math

from math import e, pi, inf, nan, isinf, isnan    # `<el> == nan` is always False.
+from math import sin, cos, tan, asin, acos, atan  # Also: degrees, radians.
+from math import log, log10, log2                 # Log can accept base as second arg.
 
-

Statistics

from statistics import mean, median, variance, stdev, quantiles, groupby
+

Statistics

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

Random

from random import random, randint, choice, shuffle, gauss, seed
-
-<float> = random()                       # A float inside [0, 1).
-<int>   = randint(from_inc, to_inc)      # An int inside [from_inc, to_inc].
-<el>    = choice(<sequence>)             # Keeps the sequence intact.
+

Random

from random import random, randint, choice        # Also shuffle, gauss, triangular, seed.
+<float> = random()                                # A float inside [0, 1).
+<int>   = randint(from_inc, to_inc)               # An int inside [from_inc, to_inc].
+<el>    = choice(<sequence>)                      # Keeps the sequence intact.
 
-

Bin, Hex

<int> = ±0b<bin>                         # Or: ±0x<hex>
-<int> = int('±<bin>', 2)                 # Or: int('±<hex>', 16)
-<int> = int('±0b<bin>', 0)               # Or: int('±0x<hex>', 0)
-<str> = bin(<int>)                       # Returns '[-]0b<bin>'.
+

Bin, Hex

<int> = ±0b<bin>                                  # Or: ±0x<hex>
+<int> = int('±<bin>', 2)                          # Or: int('±<hex>', 16)
+<int> = int('±0b<bin>', 0)                        # Or: int('±0x<hex>', 0)
+<str> = bin(<int>)                                # Returns '[-]0b<bin>'.
 
-

Bitwise Operators

<int> = <int> & <int>                    # And (0b1100 & 0b1010 == 0b1000).
-<int> = <int> | <int>                    # Or  (0b1100 | 0b1010 == 0b1110).
-<int> = <int> ^ <int>                    # Xor (0b1100 ^ 0b1010 == 0b0110).
-<int> = <int> << n_bits                  # Left shift. Use >> for right.
-<int> = ~<int>                           # Not. Also -<int> - 1.
+

Bitwise Operators

<int> = <int> & <int>                             # And (0b1100 & 0b1010 == 0b1000).
+<int> = <int> | <int>                             # Or  (0b1100 | 0b1010 == 0b1110).
+<int> = <int> ^ <int>                             # Xor (0b1100 ^ 0b1010 == 0b0110).
+<int> = <int> << n_bits                           # Left shift. Use >> for right.
+<int> = ~<int>                                    # Not. Also -<int> - 1.
 

#Combinatorics

  • Every function returns an iterator.
  • If you want to print the iterator, you need to pass it to the list() function first!
  • -
from itertools import product, combinations, combinations_with_replacement, permutations
+
import itertools as it
 
-
>>> product([0, 1], repeat=3)
-[(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), ..., (1, 1, 1)]
+
>>> it.product([0, 1], repeat=3)
+[(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1),
+ (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)]
 
-
>>> product('abc', 'abc')                    #   a  b  c
-[('a', 'a'), ('a', 'b'), ('a', 'c'),         # a x  x  x
- ('b', 'a'), ('b', 'b'), ('b', 'c'),         # b x  x  x
- ('c', 'a'), ('c', 'b'), ('c', 'c')]         # c x  x  x
+
>>> it.product('abc', 'abc')                      #   a  b  c
+[('a', 'a'), ('a', 'b'), ('a', 'c'),              # a x  x  x
+ ('b', 'a'), ('b', 'b'), ('b', 'c'),              # b x  x  x
+ ('c', 'a'), ('c', 'b'), ('c', 'c')]              # c x  x  x
 
-
>>> combinations('abc', 2)                   #   a  b  c
-[('a', 'b'), ('a', 'c'),                     # a .  x  x
- ('b', 'c')]                                 # b .  .  x
+
>>> it.combinations('abc', 2)                     #   a  b  c
+[('a', 'b'), ('a', 'c'),                          # a .  x  x
+ ('b', 'c')]                                      # b .  .  x
 
-
>>> combinations_with_replacement('abc', 2)  #   a  b  c
-[('a', 'a'), ('a', 'b'), ('a', 'c'),         # a x  x  x
- ('b', 'b'), ('b', 'c'),                     # b .  x  x
- ('c', 'c')]                                 # c .  .  x
+
>>> it.combinations_with_replacement('abc', 2)    #   a  b  c
+[('a', 'a'), ('a', 'b'), ('a', 'c'),              # a x  x  x
+ ('b', 'b'), ('b', 'c'),                          # b .  x  x
+ ('c', 'c')]                                      # c .  .  x
 
-
>>> permutations('abc', 2)                   #   a  b  c
-[('a', 'b'), ('a', 'c'),                     # a .  x  x
- ('b', 'a'), ('b', 'c'),                     # b x  .  x
- ('c', 'a'), ('c', 'b')]                     # c x  x  .
+
>>> it.permutations('abc', 2)                     #   a  b  c
+[('a', 'b'), ('a', 'c'),                          # a .  x  x
+ ('b', 'a'), ('b', 'c'),                          # b x  .  x
+ ('c', 'a'), ('c', 'b')]                          # c x  x  .
 

#Datetime

  • Module 'datetime' provides 'date' <D>, 'time' <T>, 'datetime' <DT> and 'timedelta' <TD> classes. All are immutable and hashable.
  • @@ -1434,27 +1434,27 @@ value = args.<name>

    #OS Commands

    import os, shutil, subprocess
     
    -
    os.chdir(<path>)                 # Changes the current working directory.
    -os.mkdir(<path>, mode=0o777)     # Creates a directory. Mode is in octal.
    -os.makedirs(<path>, mode=0o777)  # Creates all path's dirs. Also: `exist_ok=False`.
    +
    os.chdir(<path>)                    # Changes the current working directory.
    +os.mkdir(<path>, mode=0o777)        # Creates a directory. Permissions are in octal.
    +os.makedirs(<path>, mode=0o777)     # Creates all path's dirs. Also: `exist_ok=False`.
     
    -
    shutil.copy(from, to)            # Copies the file. 'to' can exist or be a dir.
    -shutil.copytree(from, to)        # Copies the directory. 'to' must not exist.
    +
    shutil.copy(from, to)               # Copies the file. 'to' can exist or be a dir.
    +shutil.copytree(from, to)           # Copies the directory. 'to' must not exist.
     
    -
    os.rename(from, to)              # Renames/moves the file or directory.
    -os.replace(from, to)             # Same, but overwrites 'to' if it exists.
    +
    os.rename(from, to)                 # Renames/moves the file or directory.
    +os.replace(from, to)                # Same, but overwrites 'to' if it exists.
     
    -
    os.remove(<path>)                # Deletes the file.
    -os.rmdir(<path>)                 # Deletes the empty directory.
    -shutil.rmtree(<path>)            # Deletes the directory.
    +
    os.remove(<path>)                   # Deletes the file.
    +os.rmdir(<path>)                    # Deletes the empty directory.
    +shutil.rmtree(<path>)               # Deletes the directory.
     
    • Paths can be either strings, Paths or DirEntry objects.
    • Functions report OS related errors by raising either OSError or one of its subclasses.
    -

    Shell Commands

    <pipe> = os.popen('<command>')   # Executes command in sh/cmd and returns its stdout pipe.
    -<str>  = <pipe>.read(size=-1)    # Reads 'size' chars or until EOF. Also readline/s().
    -<int>  = <pipe>.close()          # Closes the pipe. Returns None on success, int on error.
    +

    Shell Commands

    <pipe> = os.popen('<command>')      # Executes command in sh/cmd. Returns its stdout pipe.
    +<str>  = <pipe>.read(size=-1)       # Reads 'size' chars or until EOF. Also readline/s().
    +<int>  = <pipe>.close()             # Closes the pipe. Returns None on success.
     

    Sends '1 + 1' to the basic calculator and captures its output:

    >>> subprocess.run('bc', input='1 + 1\n', capture_output=True, text=True)
    @@ -1470,8 +1470,8 @@ CompletedProcess(args=['bc', #JSON

    Text file format for storing collections of strings and numbers.

    import json
    -<str>    = json.dumps(<object>)    # Converts object to JSON string.
    -<object> = json.loads(<str>)       # Converts JSON string to object.
    +<str>    = json.dumps(<object>)     # Converts object to JSON string.
    +<object> = json.loads(<str>)        # Converts JSON string to object.
     
    @@ -1486,8 +1486,8 @@ CompletedProcess(args=['bc', #Pickle

    Binary file format for storing Python objects.

    import pickle
    -<bytes>  = pickle.dumps(<object>)  # Converts object to bytes object.
    -<object> = pickle.loads(<bytes>)   # Converts bytes object to object.
    +<bytes>  = pickle.dumps(<object>)   # Converts object to bytes object.
    +<object> = pickle.loads(<bytes>)    # Converts bytes object to object.
     
    @@ -2906,7 +2906,7 @@ $ pyinstaller script.py --add-data '<path>:.' diff --git a/parse.js b/parse.js index 5d551f3..0a9c811 100755 --- a/parse.js +++ b/parse.js @@ -86,12 +86,12 @@ const DATACLASS = '<tuple> = (\'<attr_name>\', <type> [, <default_value>])'; const SHUTIL_COPY = - 'shutil.copy(from, to) # Copies the file. \'to\' can exist or be a dir.\n' + - 'shutil.copytree(from, to) # Copies the directory. \'to\' must not exist.\n'; + 'shutil.copy(from, to) # Copies the file. \'to\' can exist or be a dir.\n' + + 'shutil.copytree(from, to) # Copies the directory. \'to\' must not exist.\n'; const OS_RENAME = - 'os.rename(from, to) # Renames/moves the file or directory.\n' + - 'os.replace(from, to) # Same, but overwrites \'to\' if it exists.\n'; + 'os.rename(from, to) # Renames/moves the file or directory.\n' + + 'os.replace(from, to) # Same, but overwrites \'to\' if it exists.\n'; const STRUCT_FORMAT = '\'<n>s\'';