diff --git a/parse.js b/parse.js index 1c660ed..8d38505 100755 --- a/parse.js +++ b/parse.js @@ -1,1025 +1 @@ -#!/usr/bin/env node -// Usage: node parse.js -// -// Script that creates index.html out of web/template.html and README.md. -// -// It is written in JS because this code used to be executed on the client side. -// To install the Node.js and npm run: -// $ sudo apt install nodejs npm # On macOS use `brew install ...` instead. -// -// To install dependencies globally, run: -// $ npm install -g jsdom jquery showdown highlightjs@9.12.0 -// -// If running on macOS and modules can't be found after installation add: -// export NODE_PATH=/usr/local/lib/node_modules -// to the ~/.bash_profile or ~/.bashrc file and run '$ bash'. -// -// To avoid problems with permissions and path variables, install modules -// into project's directory using: -// $ npm install jsdom jquery showdown highlightjs@9.12.0 -// -// It is also advisable to add a Bash script into .git/hooks directory, that will -// run this script before every commit. It should be named 'pre-commit' and it -// should contain the following line: `./parse.js`. - - -const fs = require('fs'); -const jsdom = require('jsdom'); -const showdown = require('showdown'); -const hljs = require('highlightjs'); - - -const TOC = - '
ToC = {\n' +
-  '    \'1. Collections\': [List, Dictionary, Set, Tuple, Range, Enumerate, Iterator, Generator],\n' +
-  '    \'2. Types\':       [Type, String, Regular_Exp, Format, Numbers, Combinatorics, Datetime],\n' +
-  '    \'3. Syntax\':      [Args, Inline, Import, Decorator, Class, Duck_Types, Enum, Exception],\n' +
-  '    \'4. System\':      [Exit, Print, Input, Command_Line_Arguments, Open, Path, OS_Commands],\n' +
-  '    \'5. Data\':        [JSON, Pickle, CSV, SQLite, Bytes, Struct, Array, Memory_View, Deque],\n' +
-  '    \'6. Advanced\':    [Operator, Match_Stmt, Logging, Introspection, Threading, Coroutines],\n' +
-  '    \'7. Libraries\':   [Progress_Bar, Plot, Table, Console_App, GUI, Scraping, Web, Profile],\n' +
-  '    \'8. Multimedia\':  [NumPy, Image, Animation, Audio, Synthesizer, Pygame, Pandas, Plotly]\n' +
-  '}\n' +
-  '
\n'; - -const BIN_HEX = - '<int> = ±0b<bin> # Or: ±0x<hex>\n' + - '<int> = int(\'±<bin>\', 2) # Or: int(\'±<hex>\', 16)\n' + - '<int> = int(\'±0b<bin>\', 0) # Or: int(\'±0x<hex>\', 0)\n' + - '<str> = bin(<int>) # Returns \'[-]0b<bin>\'. Also hex().\n'; - -const CACHE = - 'from functools import cache\n' + - '\n' + - '@cache\n' + - 'def fib(n):\n' + - ' return n if n < 2 else fib(n-2) + fib(n-1)'; - -const PARAMETRIZED_DECORATOR = - 'from functools import wraps\n' + - '\n' + - 'def debug(print_result=False):\n' + - ' def decorator(func):\n' + - ' @wraps(func)\n' + - ' def out(*args, **kwargs):\n' + - ' result = func(*args, **kwargs)\n' + - ' print(func.__name__, result if print_result else \'\')\n' + - ' return result\n' + - ' return out\n' + - ' return decorator\n' + - '\n' + - '@debug(print_result=True)\n' + - 'def add(x, y):\n' + - ' return x + y\n'; - -const REPR_USE_CASES = - 'print/str/repr([<obj>])\n' + - 'print/str/repr({<obj>: <obj>})\n' + - 'f\'{<obj>!r}\'\n' + - 'Z = dataclasses.make_dataclass(\'Z\', [\'a\']); print/str/repr(Z(<obj>))\n' + - '>>> <obj>\n'; - -const CONSTRUCTOR_OVERLOADING = - 'class <name>:\n' + - ' def __init__(self, a=None):\n' + - ' self.a = a\n'; - -const SHUTIL_COPY = - 'shutil.copy(from, to) # Copies the file. \'to\' can exist or be a dir.\n' + - 'shutil.copy2(from, to) # Also copies creation and modification time.\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 file \'to\' even on Windows.\n' + - 'shutil.move(from, to) # Rename() that moves into \'to\' if it\'s a dir.\n'; - -const STRUCT_FORMAT = - '\'<n>s\''; - -const MATCH = - 'match <object/expression>:\n' + - ' case <pattern> [if <condition>]:\n' + - ' <code>\n' + - ' ...\n'; - -const MATCH_EXAMPLE = - '>>> from pathlib import Path\n' + - '>>> match Path(\'/home/gto/python-cheatsheet/README.md\'):\n' + - '... case Path(\n' + - '... parts=[\'/\', \'home\', user, *_]\n' + - '... ) as p if p.name.lower().startswith(\'readme\') and p.is_file():\n' + - '... print(f\'{p.name} is a readme file that belongs to user {user}.\')\n' + - 'README.md is a readme file that belongs to user gto.\n'; - -const COROUTINES = - 'import asyncio, collections, curses, curses.textpad, enum, random\n' + - '\n' + - 'P = collections.namedtuple(\'P\', \'x y\') # Position\n' + - 'D = enum.Enum(\'D\', \'n e s w\') # Direction\n' + - 'W, H = 15, 7 # Width, Height\n' + - '\n' + - 'def main(screen):\n' + - ' curses.curs_set(0) # Makes cursor invisible.\n' + - ' screen.nodelay(True) # Makes getch() non-blocking.\n' + - ' asyncio.run(main_coroutine(screen)) # Starts running asyncio code.\n' + - '\n' + - 'async def main_coroutine(screen):\n' + - ' moves = asyncio.Queue()\n' + - ' state = {\'*\': P(0, 0)} | {id_: P(W//2, H//2) for id_ in range(10)}\n' + - ' ai = [random_controller(id_, moves) for id_ in range(10)]\n' + - ' mvc = [human_controller(screen, moves), model(moves, state), view(state, screen)]\n' + - ' tasks = [asyncio.create_task(coro) for coro in ai + mvc]\n' + - ' await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)\n' + - '\n' + - 'async def random_controller(id_, moves):\n' + - ' while True:\n' + - ' d = random.choice(list(D))\n' + - ' moves.put_nowait((id_, d))\n' + - ' await asyncio.sleep(random.triangular(0.01, 0.65))\n' + - '\n' + - 'async def human_controller(screen, moves):\n' + - ' while True:\n' + - ' key_mappings = {258: D.s, 259: D.n, 260: D.w, 261: D.e}\n' + - ' if d := key_mappings.get(screen.getch()):\n' + - ' moves.put_nowait((\'*\', d))\n' + - ' await asyncio.sleep(0.005)\n' + - '\n' + - 'async def model(moves, state):\n' + - ' while state[\'*\'] not in (state[id_] for id_ in range(10)):\n' + - ' id_, d = await moves.get()\n' + - ' deltas = {D.n: P(0, -1), D.e: P(1, 0), D.s: P(0, 1), D.w: P(-1, 0)}\n' + - ' state[id_] = P((state[id_].x + deltas[d].x) % W, (state[id_].y + deltas[d].y) % H)\n' + - '\n' + - 'async def view(state, screen):\n' + - ' offset = P(curses.COLS//2 - W//2, curses.LINES//2 - H//2)\n' + - ' while True:\n' + - ' screen.erase()\n' + - ' curses.textpad.rectangle(screen, offset.y-1, offset.x-1, offset.y+H, offset.x+W)\n' + - ' for id_, p in state.items():\n' + - ' screen.addstr(offset.y + (p.y - state[\'*\'].y + H//2) % H,\n' + - ' offset.x + (p.x - state[\'*\'].x + W//2) % W, str(id_))\n' + - ' screen.refresh()\n' + - ' await asyncio.sleep(0.005)\n' + - '\n' + - 'if __name__ == \'__main__\':\n' + - ' curses.wrapper(main)\n'; - -const CURSES = - '# $ pip3 install windows-curses\n' + - 'import curses, os\n' + - 'from curses import A_REVERSE, KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT, KEY_ENTER\n' + - '\n' + - 'def main(screen):\n' + - ' ch, first, selected, paths = 0, 0, 0, os.listdir()\n' + - ' while ch != ord(\'q\'):\n' + - ' height, width = screen.getmaxyx()\n' + - ' screen.erase()\n' + - ' for y, filename in enumerate(paths[first : first+height]):\n' + - ' color = A_REVERSE if filename == paths[selected] else 0\n' + - ' screen.addnstr(y, 0, filename, width-1, color)\n' + - ' ch = screen.getch()\n' + - ' selected -= (ch == KEY_UP) and (selected > 0)\n' + - ' selected += (ch == KEY_DOWN) and (selected < len(paths)-1)\n' + - ' first = min(first, selected)\n' + - ' first = max(first, selected - (height-1))\n' + - ' if ch in [KEY_LEFT, KEY_RIGHT, KEY_ENTER, ord(\'\\n\'), ord(\'\\r\')]:\n' + - ' new_dir = \'..\' if ch == KEY_LEFT else paths[selected]\n' + - ' if os.path.isdir(new_dir):\n' + - ' os.chdir(new_dir)\n' + - ' first, selected, paths = 0, 0, os.listdir()\n' + - '\n' + - 'if __name__ == \'__main__\':\n' + - ' curses.wrapper(main)\n'; - -const PROGRESS_BAR = - '# $ pip3 install tqdm\n' + - '>>> import tqdm, time\n' + - '>>> for el in tqdm.tqdm([1, 2, 3], desc=\'Processing\'):\n' + - '... time.sleep(1)\n' + - 'Processing: 100%|████████████████████| 3/3 [00:03<00:00, 1.00s/it]\n'; - -const LOGGING_EXAMPLE = - '>>> logger = log.getLogger(\'my_module\')\n' + - '>>> handler = log.FileHandler(\'test.log\', encoding=\'utf-8\')\n' + - '>>> handler.setFormatter(log.Formatter(\'%(asctime)s %(levelname)s:%(name)s:%(message)s\'))\n' + - '>>> logger.addHandler(handler)\n' + - '>>> logger.setLevel(\'DEBUG\')\n' + - '>>> log.basicConfig()\n' + - '>>> log.root.handlers[0].setLevel(\'WARNING\')\n' + - '>>> logger.critical(\'Running out of disk space.\')\n' + - 'CRITICAL:my_module:Running out of disk space.\n' + - '>>> print(open(\'test.log\').read())\n' + - '2023-02-07 23:21:01,430 CRITICAL:my_module:Running out of disk space.\n'; - -const AUDIO_1 = - 'def write_to_wav_file(filename, samples_f, p=None, nchannels=1, sampwidth=2, framerate=44100):\n' + - ' def get_bytes(a_float):\n' + - ' a_float = max(-1, min(1 - 2e-16, a_float))\n' + - ' a_float += (p.sampwidth == 1)\n' + - ' a_float *= pow(2, (p.sampwidth * 8) - 1)\n' + - ' return int(a_float).to_bytes(p.sampwidth, \'little\', signed=(p.sampwidth != 1))\n' + - ' if p is None:\n' + - ' p = wave._wave_params(nchannels, sampwidth, framerate, 0, \'NONE\', \'not compressed\')\n' + - ' with wave.open(filename, \'wb\') as file:\n' + - ' file.setparams(p)\n' + - ' file.writeframes(b\'\'.join(get_bytes(f) for f in samples_f))\n'; - -const AUDIO_2 = - 'from math import pi, sin\n' + - 'samples_f = (sin(i * 2 * pi * 440 / 44100) for i in range(100_000))\n' + - 'write_to_wav_file(\'test.wav\', samples_f)\n'; - -const MARIO = - 'import collections, dataclasses, enum, io, itertools as it, pygame as pg, urllib.request\n' + - 'from random import randint\n' + - '\n' + - 'P = collections.namedtuple(\'P\', \'x y\') # Position\n' + - 'D = enum.Enum(\'D\', \'n e s w\') # Direction\n' + - 'W, H, MAX_S = 50, 50, P(5, 10) # Width, Height, Max speed\n' + - '\n' + - 'def main():\n' + - ' def get_screen():\n' + - ' pg.init()\n' + - ' return pg.display.set_mode((W*16, H*16))\n' + - ' def get_images():\n' + - ' url = \'https://gto76.github.io/python-cheatsheet/web/mario_bros.png\'\n' + - ' img = pg.image.load(io.BytesIO(urllib.request.urlopen(url).read()))\n' + - ' return [img.subsurface(get_rect(x, 0)) for x in range(img.get_width() // 16)]\n' + - ' def get_mario():\n' + - ' Mario = dataclasses.make_dataclass(\'Mario\', \'rect spd facing_left frame_cycle\'.split())\n' + - ' return Mario(get_rect(1, 1), P(0, 0), False, it.cycle(range(3)))\n' + - ' def get_tiles():\n' + - ' border = [(x, y) for x in range(W) for y in range(H) if x in [0, W-1] or y in [0, H-1]]\n' + - ' platforms = [(randint(1, W-2), randint(2, H-2)) for _ in range(W*H // 10)]\n' + - ' return [get_rect(x, y) for x, y in border + platforms]\n' + - ' def get_rect(x, y):\n' + - ' return pg.Rect(x*16, y*16, 16, 16)\n' + - ' run(get_screen(), get_images(), get_mario(), get_tiles())\n' + - '\n' + - 'def run(screen, images, mario, tiles):\n' + - ' clock = pg.time.Clock()\n' + - ' pressed = set()\n' + - ' while not pg.event.get(pg.QUIT):\n' + - ' clock.tick(28)\n' + - ' pressed |= {e.key for e in pg.event.get(pg.KEYDOWN)}\n' + - ' pressed -= {e.key for e in pg.event.get(pg.KEYUP)}\n' + - ' update_speed(mario, tiles, pressed)\n' + - ' update_position(mario, tiles)\n' + - ' draw(screen, images, mario, tiles)\n' + - '\n' + - 'def update_speed(mario, tiles, pressed):\n' + - ' x, y = mario.spd\n' + - ' x += 2 * ((pg.K_RIGHT in pressed) - (pg.K_LEFT in pressed))\n' + - ' x += (x < 0) - (x > 0)\n' + - ' y += 1 if D.s not in get_boundaries(mario.rect, tiles) else (pg.K_UP in pressed) * -10\n' + - ' mario.spd = P(x=max(-MAX_S.x, min(MAX_S.x, x)), y=max(-MAX_S.y, min(MAX_S.y, y)))\n' + - '\n' + - 'def update_position(mario, tiles):\n' + - ' x, y = mario.rect.topleft\n' + - ' n_steps = max(abs(s) for s in mario.spd)\n' + - ' for _ in range(n_steps):\n' + - ' mario.spd = stop_on_collision(mario.spd, get_boundaries(mario.rect, tiles))\n' + - ' x, y = x + (mario.spd.x / n_steps), y + (mario.spd.y / n_steps)\n' + - ' mario.rect.topleft = x, y\n' + - '\n' + - 'def get_boundaries(rect, tiles):\n' + - ' deltas = {D.n: P(0, -1), D.e: P(1, 0), D.s: P(0, 1), D.w: P(-1, 0)}\n' + - ' return {d for d, delta in deltas.items() if rect.move(delta).collidelist(tiles) != -1}\n' + - '\n' + - 'def stop_on_collision(spd, bounds):\n' + - ' return P(x=0 if (D.w in bounds and spd.x < 0) or (D.e in bounds and spd.x > 0) else spd.x,\n' + - ' y=0 if (D.n in bounds and spd.y < 0) or (D.s in bounds and spd.y > 0) else spd.y)\n' + - '\n' + - 'def draw(screen, images, mario, tiles):\n' + - ' screen.fill((85, 168, 255))\n' + - ' mario.facing_left = mario.spd.x < 0 if mario.spd.x else mario.facing_left\n' + - ' is_airborne = D.s not in get_boundaries(mario.rect, tiles)\n' + - ' image_index = 4 if is_airborne else (next(mario.frame_cycle) if mario.spd.x else 6)\n' + - ' screen.blit(images[image_index + (mario.facing_left * 9)], mario.rect)\n' + - ' for t in tiles:\n' + - ' is_border = t.x in [0, (W-1)*16] or t.y in [0, (H-1)*16]\n' + - ' screen.blit(images[18 if is_border else 19], t)\n' + - ' pg.display.flip()\n' + - '\n' + - 'if __name__ == \'__main__\':\n' + - ' main()\n'; - -const GROUPBY = - '>>> df = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 6]], list(\'abc\'), list(\'xyz\'))\n' + - '>>> gb = df.groupby(\'z\'); gb.apply(print)\n' + - ' x y z\n' + - 'a 1 2 3\n' + - ' x y z\n' + - 'b 4 5 6\n' + - 'c 7 8 6\n' + - '>>> gb.sum()\n' + - ' x y\n' + - 'z\n' + - '3 1 2\n' + - '6 11 13'; - - -const CYTHON_1 = - 'cdef <ctype/type> <var_name> [= <obj>]\n' + - 'cdef <ctype>[n_elements] <var_name> [= <coll_of_nums>]\n' + - 'cdef <ctype/type/void> <func_name>(<ctype/type> <arg_name>): ...\n'; - -const CYTHON_2 = - 'cdef class <class_name>:\n' + - ' cdef public <ctype/type> <attr_name>\n' + - ' def __init__(self, <ctype/type> <arg_name>):\n' + - ' self.<attr_name> = <arg_name>\n'; - -const INDEX = - '
  • Ctrl+F / ⌘F is usually sufficient.
  • \n' + - '
  • Searching \'#<title>\' will limit the search to the titles.
  • \n' + - '
  • Click on the title\'s \'#\' to get a link to its section.
  • \n'; - - -const DIAGRAM_1_A = - '+------------------+------------+------------+------------+\n' + - '| | Iterable | Collection | Sequence |\n' + - '+------------------+------------+------------+------------+\n'; - -const DIAGRAM_1_B = - '┏━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┓\n' + - '┃ │ Iterable │ Collection │ Sequence ┃\n' + - '┠──────────────────┼────────────┼────────────┼────────────┨\n' + - '┃ list, range, str │ ✓ │ ✓ │ ✓ ┃\n' + - '┃ dict, set │ ✓ │ ✓ │ ┃\n' + - '┃ iter │ ✓ │ │ ┃\n' + - '┗━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┛\n'; - -const DIAGRAM_2_A = - '+--------------------+----------+----------+----------+----------+----------+\n' + - '| | Number | Complex | Real | Rational | Integral |\n' + - '+--------------------+----------+----------+----------+----------+----------+\n'; - -const DIAGRAM_2_B = - '┏━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┓\n' + - '┃ │ Number │ Complex │ Real │ Rational │ Integral ┃\n' + - '┠────────────────────┼──────────┼──────────┼──────────┼──────────┼──────────┨\n' + - '┃ int │ ✓ │ ✓ │ ✓ │ ✓ │ ✓ ┃\n' + - '┃ fractions.Fraction │ ✓ │ ✓ │ ✓ │ ✓ │ ┃\n' + - '┃ float │ ✓ │ ✓ │ ✓ │ │ ┃\n' + - '┃ complex │ ✓ │ ✓ │ │ │ ┃\n' + - '┃ decimal.Decimal │ ✓ │ │ │ │ ┃\n' + - '┗━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┛\n'; - -const DIAGRAM_3_A = - '+---------------+----------+----------+----------+----------+----------+\n'; - -const DIAGRAM_3_B = - '┏━━━━━━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┓\n' + - '┃ │ [ !#$%…] │ [a-zA-Z] │ [¼½¾] │ [²³¹] │ [0-9] ┃\n' + - '┠───────────────┼──────────┼──────────┼──────────┼──────────┼──────────┨\n' + - '┃ isprintable() │ ✓ │ ✓ │ ✓ │ ✓ │ ✓ ┃\n' + - '┃ isalnum() │ │ ✓ │ ✓ │ ✓ │ ✓ ┃\n' + - '┃ isnumeric() │ │ │ ✓ │ ✓ │ ✓ ┃\n' + - '┃ isdigit() │ │ │ │ ✓ │ ✓ ┃\n' + - '┃ isdecimal() │ │ │ │ │ ✓ ┃\n' + - '┗━━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┛\n'; - -const DIAGRAM_4_A = - "+--------------+----------------+----------------+----------------+----------------+\n" + - "| | {} | {:f} | {:e} | {:%} |\n" + - "+--------------+----------------+----------------+----------------+----------------+\n"; - -const DIAGRAM_4_B = - "┏━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━┓\n" + - "┃ │ {<float>} │ {<float>:f} │ {<float>:e} │ {<float>:%} ┃\n" + - "┠──────────────┼────────────────┼────────────────┼────────────────┼────────────────┨\n" + - "┃ 0.000056789 │ '5.6789e-05' │ '0.000057' │ '5.678900e-05' │ '0.005679%' ┃\n" + - "┃ 0.00056789 │ '0.00056789' │ '0.000568' │ '5.678900e-04' │ '0.056789%' ┃\n" + - "┃ 0.0056789 │ '0.0056789' │ '0.005679' │ '5.678900e-03' │ '0.567890%' ┃\n" + - "┃ 0.056789 │ '0.056789' │ '0.056789' │ '5.678900e-02' │ '5.678900%' ┃\n" + - "┃ 0.56789 │ '0.56789' │ '0.567890' │ '5.678900e-01' │ '56.789000%' ┃\n" + - "┃ 5.6789 │ '5.6789' │ '5.678900' │ '5.678900e+00' │ '567.890000%' ┃\n" + - "┃ 56.789 │ '56.789' │ '56.789000' │ '5.678900e+01' │ '5678.900000%' ┃\n" + - "┗━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━┛\n" + - "\n" + - "┏━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━┓\n" + - "┃ │ {<float>:.2} │ {<float>:.2f} │ {<float>:.2e} │ {<float>:.2%} ┃\n" + - "┠──────────────┼────────────────┼────────────────┼────────────────┼────────────────┨\n" + - "┃ 0.000056789 │ '5.7e-05' │ '0.00' │ '5.68e-05' │ '0.01%' ┃\n" + - "┃ 0.00056789 │ '0.00057' │ '0.00' │ '5.68e-04' │ '0.06%' ┃\n" + - "┃ 0.0056789 │ '0.0057' │ '0.01' │ '5.68e-03' │ '0.57%' ┃\n" + - "┃ 0.056789 │ '0.057' │ '0.06' │ '5.68e-02' │ '5.68%' ┃\n" + - "┃ 0.56789 │ '0.57' │ '0.57' │ '5.68e-01' │ '56.79%' ┃\n" + - "┃ 5.6789 │ '5.7' │ '5.68' │ '5.68e+00' │ '567.89%' ┃\n" + - "┃ 56.789 │ '5.7e+01' │ '56.79' │ '5.68e+01' │ '5678.90%' ┃\n" + - "┗━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━┛\n"; - -const DIAGRAM_5_A = - "+--------------+----------------+----------------+----------------+----------------+\n" + - "| | {:.2} | {:.2f} | {:.2e} | {:.2%} |\n" + - "+--------------+----------------+----------------+----------------+----------------+\n"; - -const DIAGRAM_6_A = - '+------------+------------+------------+------------+--------------+\n' + - '| | Iterable | Collection | Sequence | abc.Sequence |\n' + - '+------------+------------+------------+------------+--------------+\n'; - -const DIAGRAM_6_B = - '┏━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━━━┓\n' + - '┃ │ Iterable │ Collection │ Sequence │ abc.Sequence ┃\n' + - '┠────────────┼────────────┼────────────┼────────────┼──────────────┨\n' + - '┃ iter() │ ! │ ! │ ✓ │ ✓ ┃\n' + - '┃ contains() │ ✓ │ ✓ │ ✓ │ ✓ ┃\n' + - '┃ len() │ │ ! │ ! │ ! ┃\n' + - '┃ getitem() │ │ │ ! │ ! ┃\n' + - '┃ reversed() │ │ │ ✓ │ ✓ ┃\n' + - '┃ index() │ │ │ │ ✓ ┃\n' + - '┃ count() │ │ │ │ ✓ ┃\n' + - '┗━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━━━┛\n'; - -const DIAGRAM_7_A = - 'BaseException\n' + - ' +-- SystemExit'; - -const DIAGRAM_7_B = - "BaseException\n" + - " ├── SystemExit # Raised by the sys.exit() function.\n" + - " ├── KeyboardInterrupt # Raised when the user hits the interrupt key (ctrl-c).\n" + - " └── Exception # User-defined exceptions should be derived from this class.\n" + - " ├── ArithmeticError # Base class for arithmetic errors such as ZeroDivisionError.\n" + - " ├── AssertionError # Raised by `assert <exp>` if expression returns false value.\n" + - " ├── AttributeError # Raised when object doesn't have requested attribute/method.\n" + - " ├── EOFError # Raised by input() when it hits an end-of-file condition.\n" + - " ├── LookupError # Base class for errors when a collection can't find an item.\n" + - " │ ├── IndexError # Raised when a sequence index is out of range.\n" + - " │ └── KeyError # Raised when a dictionary key or set element is missing.\n" + - " ├── MemoryError # Out of memory. May be too late to start deleting variables.\n" + - " ├── NameError # Raised when nonexistent name (variable/func/class) is used.\n" + - " │ └── UnboundLocalError # Raised when local name is used before it's being defined.\n" + - " ├── OSError # Errors such as FileExistsError/TimeoutError (see #Open).\n" + - " │ └── ConnectionError # Errors such as BrokenPipeError/ConnectionAbortedError.\n" + - " ├── RuntimeError # Raised by errors that don't fall into other categories.\n" + - " │ ├── NotImplementedEr… # Can be raised by abstract methods or by unfinished code.\n" + - " │ └── RecursionError # Raised when the maximum recursion depth is exceeded.\n" + - " ├── StopIteration # Raised when an empty iterator is passed to next().\n" + - " ├── TypeError # When an argument of the wrong type is passed to function.\n" + - " └── ValueError # When argument has the right type but inappropriate value.\n"; - -const DIAGRAM_8_A = - '+-----------+------------+------------+------------+\n' + - '| | List | Set | Dict |\n' + - '+-----------+------------+------------+------------+\n'; - -const DIAGRAM_8_B = - '┏━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┓\n' + - '┃ │ List │ Set │ Dict ┃\n' + - '┠───────────┼────────────┼────────────┼────────────┨\n' + - '┃ getitem() │ IndexError │ │ KeyError ┃\n' + - '┃ pop() │ IndexError │ KeyError │ KeyError ┃\n' + - '┃ remove() │ ValueError │ KeyError │ ┃\n' + - '┃ index() │ ValueError │ │ ┃\n' + - '┗━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┛\n'; - -const DIAGRAM_9_A = - '+------------------+--------------+--------------+--------------+\n' + - '| | excel | excel-tab | unix |\n' + - '+------------------+--------------+--------------+--------------+\n'; - -const DIAGRAM_9_B = - "┏━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━┓\n" + - "┃ │ excel │ excel-tab │ unix ┃\n" + - "┠──────────────────┼──────────────┼──────────────┼──────────────┨\n" + - "┃ delimiter │ ',' │ '\\t' │ ',' ┃\n" + - "┃ lineterminator │ '\\r\\n' │ '\\r\\n' │ '\\n' ┃\n" + - "┃ quotechar │ '\"' │ '\"' │ '\"' ┃\n" + - "┃ escapechar │ None │ None │ None ┃\n" + - "┃ doublequote │ True │ True │ True ┃\n" + - "┃ quoting │ 0 │ 0 │ 1 ┃\n" + - "┃ skipinitialspace │ False │ False │ False ┃\n" + - "┗━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━┛\n"; - -const DIAGRAM_95_A = - '+-----------------+--------------+----------------------------------+\n' + - '| Dialect | pip3 install | Dependencies |\n' + - '+-----------------+--------------+----------------------------------+\n'; - -const DIAGRAM_95_B = - '┏━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n' + - '┃ Dialect │ pip3 install │ Dependencies ┃\n' + - '┠─────────────────┼──────────────┼──────────────────────────────────┨\n' + - '┃ mysql │ mysqlclient │ www.pypi.org/project/mysqlclient ┃\n' + - '┃ postgresql │ psycopg2 │ www.pypi.org/project/psycopg2 ┃\n' + - '┃ mssql │ pyodbc │ www.pypi.org/project/pyodbc ┃\n' + - '┃ oracle+oracledb │ oracledb │ www.pypi.org/project/oracledb ┃\n' + - '┗━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n'; - -const DIAGRAM_10_A = - '+-------------+-------------+\n' + - '| Classes | Metaclasses |\n' + - '+-------------+-------------|\n' + - '| MyClass <-- MyMetaClass |\n'; - -const DIAGRAM_10_B = - '┏━━━━━━━━━━━━━┯━━━━━━━━━━━━━┓\n' + - '┃ Classes │ Metaclasses ┃\n' + - '┠─────────────┼─────────────┨\n' + - '┃ MyClass ←──╴MyMetaClass ┃\n' + - '┃ │ ↑ ┃\n' + - '┃ object ←─────╴type ←╮ ┃\n' + - '┃ │ │ ╰──╯ ┃\n' + - '┃ str ←─────────╯ ┃\n' + - '┗━━━━━━━━━━━━━┷━━━━━━━━━━━━━┛\n'; - -const DIAGRAM_11_A = - '+-------------+-------------+\n' + - '| Classes | Metaclasses |\n' + - '+-------------+-------------|\n' + - '| MyClass | MyMetaClass |\n'; - -const DIAGRAM_11_B = - '┏━━━━━━━━━━━━━┯━━━━━━━━━━━━━┓\n' + - '┃ Classes │ Metaclasses ┃\n' + - '┠─────────────┼─────────────┨\n' + - '┃ MyClass │ MyMetaClass ┃\n' + - '┃ ↑ │ ↑ ┃\n' + - '┃ object╶─────→ type ┃\n' + - '┃ ↓ │ ┃\n' + - '┃ str │ ┃\n' + - '┗━━━━━━━━━━━━━┷━━━━━━━━━━━━━┛\n'; - -const DIAGRAM_115_A = - '+--------------+------------+-------------------------------+-------+------+\n' + - '| pip3 install | Target | How to run | Lines | Live |\n' + - '+--------------+------------+-------------------------------+-------+------+\n'; - -const DIAGRAM_115_B = - '┏━━━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━┯━━━━━━┓\n' + - '┃ pip3 install │ Target │ How to run │ Lines │ Live ┃\n' + - '┠──────────────┼────────────┼───────────────────────────────┼───────┼──────┨\n' + - '┃ pyinstrument │ CPU │ pyinstrument test.py │ × │ × ┃\n' + - '┃ py-spy │ CPU │ py-spy top -- python3 test.py │ × │ ✓ ┃\n' + - '┃ scalene │ CPU+Memory │ scalene test.py │ ✓ │ × ┃\n' + - '┃ memray │ Memory │ memray run --live test.py │ ✓ │ ✓ ┃\n' + - '┗━━━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━━┷━━━━━━┛\n'; - -const DIAGRAM_12_A = - '+-----------+-----------+------+-----------+\n' + - '| sampwidth | min | zero | max |\n' + - '+-----------+-----------+------+-----------+\n'; - -const DIAGRAM_12_B = - '┏━━━━━━━━━━━┯━━━━━━━━━━━┯━━━━━━┯━━━━━━━━━━━┓\n' + - '┃ sampwidth │ min │ zero │ max ┃\n' + - '┠───────────┼───────────┼──────┼───────────┨\n' + - '┃ 1 │ 0 │ 128 │ 255 ┃\n' + - '┃ 2 │ -32768 │ 0 │ 32767 ┃\n' + - '┃ 3 │ -8388608 │ 0 │ 8388607 ┃\n' + - '┗━━━━━━━━━━━┷━━━━━━━━━━━┷━━━━━━┷━━━━━━━━━━━┛\n'; - -const DIAGRAM_13_A = - '| s.apply(…) | 3 | sum 3 | s 3 |'; - -const DIAGRAM_13_B = - "┏━━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┓\n" + - "┃ │ 'sum' │ ['sum'] │ {'s': 'sum'} ┃\n" + - "┠──────────────┼─────────────┼─────────────┼───────────────┨\n" + - "┃ s.apply(…) │ 3 │ sum 3 │ s 3 ┃\n" + - "┃ s.agg(…) │ │ │ ┃\n" + - "┗━━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┛\n" + - "\n" + - "┏━━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┓\n" + - "┃ │ 'rank' │ ['rank'] │ {'r': 'rank'} ┃\n" + - "┠──────────────┼─────────────┼─────────────┼───────────────┨\n" + - "┃ s.apply(…) │ │ rank │ ┃\n" + - "┃ s.agg(…) │ x 1.0 │ x 1.0 │ r x 1.0 ┃\n" + - "┃ │ y 2.0 │ y 2.0 │ y 2.0 ┃\n" + - "┗━━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┛\n"; - -const DIAGRAM_13_BB = - "┏━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┓\n" + - "┃ │ 'mean' │ ['mean'] │ {'m': 'mean'} ┃\n" + - "┠────────────────┼─────────────┼─────────────┼───────────────┨\n" + - "┃ s.apply/agg(…) │ 1.5 │ mean 1.5 │ m 1.5 ┃\n" + - "┗━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┛\n" + - "\n" + - "┏━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┓\n" + - "┃ │ 'rank' │ ['rank'] │ {'r': 'rank'} ┃\n" + - "┠────────────────┼─────────────┼─────────────┼───────────────┨\n" + - "┃ s.apply/agg(…) │ │ rank │ ┃\n" + - "┃ │ x 1.0 │ x 1.0 │ r x 1.0 ┃\n" + - "┃ │ y 2.0 │ y 2.0 │ y 2.0 ┃\n" + - "┗━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┛\n"; - -const DIAGRAM_13_XXX = - "┏━━━━━━━━━━━━━━━━┯━━━━━━━━━━━┯━━━━━━━━━━━━━━━━┯━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━┓\n" + - "┃ │ 'sum' │ ['sum', 'max'] │ 'rank' │ ['rank', 'diff'] ┃\n" + - "┠────────────────┼───────────┼────────────────┼───────────┼──────────────────┨\n" + - "┃ s.apply/agg(…) │ 3 │ sum 3 │ x 1.0 │ rank diff ┃\n" + - "┃ │ │ max 2 │ y 2.0 │ x 1.0 NaN ┃\n" + - "┃ │ │ Name: a │ Name: a │ y 2.0 1.0 ┃\n" + - "┗━━━━━━━━━━━━━━━━┷━━━━━━━━━━━┷━━━━━━━━━━━━━━━━┷━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━┛\n"; - -const DIAGRAM_13_XX = - "┏━━━━━━━━━━━━━━━━┯━━━━━━━━━━━┯━━━━━━━━━━━┯━━━━━━━━━━━━━━━━┓\n" + - "┃ │ 'sum' │ 'rank' │ ['sum', 'max'] ┃\n" + - "┠────────────────┼───────────┼───────────┼────────────────┨\n" + - "┃ s.apply/agg(…) │ 3 │ x 1.0 │ sum 3 ┃\n" + - "┃ │ │ y 2.0 │ max 2 ┃\n" + - "┃ │ │ Name: a │ Name: a ┃\n" + - "┗━━━━━━━━━━━━━━━━┷━━━━━━━━━━━┷━━━━━━━━━━━┷━━━━━━━━━━━━━━━━┛\n"; - -const DIAGRAM_13_X = - "┏━━━━━━━━━━━━━━━━┯━━━━━━━━━━━┯━━━━━━━━━━━┯━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━┓\n" + - "┃ │ 'sum' │ 'rank' │ ['sum', 'max'] │ ['rank', 'diff'] ┃\n" + - "┠────────────────┼───────────┼───────────┼────────────────┼──────────────────┨\n" + - "┃ s.apply/agg(…) │ 3 │ x 1.0 │ sum 3 │ rank diff ┃\n" + - "┃ │ │ y 2.0 │ max 2 │ x 1.0 NaN ┃\n" + - "┃ │ │ Name: a │ Name: a │ y 2.0 1.0 ┃\n" + - "┗━━━━━━━━━━━━━━━━┷━━━━━━━━━━━┷━━━━━━━━━━━┷━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━┛\n"; - -const DIAGRAM_13_Y = - "┏━━━━━━━━━━━━━━━━┯━━━━━━━━━━━┯━━━━━━━━━━━┯━━━━━━━━━━━┯━━━━━━━━━━━┓\n" + - "┃ │ 'sum' │ 'rank' │ ['sum'] │ ['rank'] ┃\n" + - "┠────────────────┼───────────┼───────────┼───────────┼───────────┨\n" + - "┃ s.apply/agg(…) │ 3 │ x 1.0 │ sum 3 │ rank ┃\n" + - "┃ │ │ y 2.0 │ Name: a │ x 1.0 ┃\n" + - "┃ │ │ Name: a │ │ y 2.0 ┃\n" + - "┗━━━━━━━━━━━━━━━━┷━━━━━━━━━━━┷━━━━━━━━━━━┷━━━━━━━━━━━┷━━━━━━━━━━━┛\n"; - -const DIAGRAM_13_BBB = - "┏━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┓\n" + - "┃ │ 'sum' │ ['sum'] │ {'s': 'sum'} ┃\n" + - "┠────────────────┼─────────────┼─────────────┼───────────────┨\n" + - "┃ s.apply/agg(…) │ 3 │ sum 3 │ s 3 ┃\n" + - "┗━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┛\n" + - "\n" + - "┏━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┓\n" + - "┃ │ 'rank' │ ['rank'] │ {'r': 'rank'} ┃\n" + - "┠────────────────┼─────────────┼─────────────┼───────────────┨\n" + - "┃ s.apply/agg(…) │ │ rank │ ┃\n" + - "┃ │ x 1.0 │ x 1.0 │ r x 1.0 ┃\n" + - "┃ │ y 2.0 │ y 2.0 │ y 2.0 ┃\n" + - "┗━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┛\n"; - -const DIAGRAM_14_A = - "| | 'rank' | ['rank'] | {'r': 'rank'} |"; - -const DIAGRAM_15_A = - '+-----------------------+---------------+------------+------------+---------------------------+'; - -const DIAGRAM_15_B = - "┏━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n" + - "┃ │ 'outer' │ 'inner' │ 'left' │ Description ┃\n" + - "┠───────────────────────┼───────────────┼────────────┼────────────┼───────────────────────────┨\n" + - "┃ df.merge(df_2, │ x y z │ x y z │ x y z │ Merges on column if 'on' ┃\n" + - "┃ on='y', │ 0 1 2 . │ 3 4 5 │ 1 2 . │ or 'left_on/right_on' are ┃\n" + - "┃ how=…) │ 1 3 4 5 │ │ 3 4 5 │ set, else on shared cols. ┃\n" + - "┃ │ 2 . 6 7 │ │ │ Uses 'inner' by default. ┃\n" + - "┠───────────────────────┼───────────────┼────────────┼────────────┼───────────────────────────┨\n" + - "┃ df.join(df_2, │ x yl yr z │ │ x yl yr z │ Merges on row keys. ┃\n" + - "┃ lsuffix='l', │ a 1 2 . . │ x yl yr z │ 1 2 . . │ Uses 'left' by default. ┃\n" + - "┃ rsuffix='r', │ b 3 4 4 5 │ 3 4 4 5 │ 3 4 4 5 │ If Series is passed, it ┃\n" + - "┃ how=…) │ c . . 6 7 │ │ │ is treated as a column. ┃\n" + - "┠───────────────────────┼───────────────┼────────────┼────────────┼───────────────────────────┨\n" + - "┃ pd.concat([df, df_2], │ x y z │ y │ │ Adds rows at the bottom. ┃\n" + - "┃ axis=0, │ a 1 2 . │ 2 │ │ Uses 'outer' by default. ┃\n" + - "┃ join=…) │ b 3 4 . │ 4 │ │ A Series is treated as a ┃\n" + - "┃ │ b . 4 5 │ 4 │ │ column. To add a row use ┃\n" + - "┃ │ c . 6 7 │ 6 │ │ pd.concat([df, DF([s])]). ┃\n" + - "┠───────────────────────┼───────────────┼────────────┼────────────┼───────────────────────────┨\n" + - "┃ pd.concat([df, df_2], │ x y y z │ │ │ Adds columns at the ┃\n" + - "┃ axis=1, │ a 1 2 . . │ x y y z │ │ right end. Uses 'outer' ┃\n" + - "┃ join=…) │ b 3 4 4 5 │ 3 4 4 5 │ │ by default. A Series is ┃\n" + - "┃ │ c . . 6 7 │ │ │ treated as a column. ┃\n" + - "┗━━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n"; - - -const DIAGRAM_16_A = - '| df.apply(…) | x 4 | x y | x 4 |'; - -const DIAGRAM_16_B = - "┏━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┓\n" + - "┃ │ 'sum' │ ['sum'] │ {'x': 'sum'} ┃\n" + - "┠─────────────────┼───────────────┼───────────────┼───────────────┨\n" + - "┃ df.apply(…) │ x 4 │ x y │ x 4 ┃\n" + - "┃ df.agg(…) │ y 6 │ sum 4 6 │ ┃\n" + - "┗━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┛\n" + - "\n" + - "┏━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┓\n" + - "┃ │ 'rank' │ ['rank'] │ {'x': 'rank'} ┃\n" + - "┠─────────────────┼───────────────┼───────────────┼───────────────┨\n" + - "┃ df.apply(…) │ │ x y │ ┃\n" + - "┃ df.agg(…) │ x y │ rank rank │ x ┃\n" + - "┃ df.transform(…) │ a 1.0 1.0 │ a 1.0 1.0 │ a 1.0 ┃\n" + - "┃ │ b 2.0 2.0 │ b 2.0 2.0 │ b 2.0 ┃\n" + - "┗━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┛\n"; - -const DIAGRAM_17_A = - "| | 'rank' | ['rank'] | {'x': 'rank'} |"; - -const DIAGRAM_18_A = - '| gb.agg(…) | x y | | x y | |'; - -const DIAGRAM_18_B = - "┏━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┓\n" + - "┃ │ 'sum' │ 'rank' │ ['rank'] │ {'x': 'rank'} ┃\n" + - "┠─────────────────┼─────────────┼─────────────┼─────────────┼───────────────┨\n" + - "┃ gb.agg(…) │ x y │ │ x y │ ┃\n" + - "┃ │ z │ x y │ rank rank │ x ┃\n" + - "┃ │ 3 1 2 │ a 1 1 │ a 1 1 │ a 1 ┃\n" + - "┃ │ 6 11 13 │ b 1 1 │ b 1 1 │ b 1 ┃\n" + - "┃ │ │ c 2 2 │ c 2 2 │ c 2 ┃\n" + - "┠─────────────────┼─────────────┼─────────────┼─────────────┼───────────────┨\n" + - "┃ gb.transform(…) │ x y │ x y │ │ ┃\n" + - "┃ │ a 1 2 │ a 1 1 │ │ ┃\n" + - "┃ │ b 11 13 │ b 1 1 │ │ ┃\n" + - "┃ │ c 11 13 │ c 2 2 │ │ ┃\n" + - "┗━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┛\n"; - -const MENU = 'Download text file, Fork me on GitHub, Check out FAQ or Switch to dark theme.\n'; - -const DARK_THEME_SCRIPT = - ''; - - -function main() { - const html = getMd(); - initDom(html); - modifyPage(); - var template = readFile('web/template.html'); - template = updateDate(template); - const tokens = template.split('
    '); - const text = `${tokens[0]} ${document.body.innerHTML} ${tokens[1]}`; - writeToFile('index.html', text); -} - -function getMd() { - var readme = readFile('README.md'); - var readme = readme.replace("#semaphore-event-barrier", "#semaphoreeventbarrier"); - var readme = readme.replace("#semaphore-event-barrier", "#semaphoreeventbarrier"); - var readme = readme.replace("#dataframe-plot-encode-decode", "#dataframeplotencodedecode"); - const converter = new showdown.Converter(); - return converter.makeHtml(readme); -} - -function initDom(html) { - const { JSDOM } = jsdom; - const dom = new JSDOM(html); - const $ = (require('jquery'))(dom.window); - global.$ = $; - global.document = dom.window.document; -} - -function modifyPage() { - changeMenu(); - addDarkThemeScript(); - removeOrigToc(); - addToc(); - insertLinks(); - unindentBanner(); - updateDiagrams(); - highlightCode(); - fixPandasDiagram(); - removePlotImages(); - fixABCSequenceDiv(); - fixStructFormatDiv(); -} - -function changeMenu() { - $('sup').first().html(MENU) -} - -function addDarkThemeScript() { - $('#main').before(DARK_THEME_SCRIPT); -} - -function removeOrigToc() { - const headerContents = $('#contents'); - const contentsList = headerContents.next(); - headerContents.remove(); - contentsList.remove(); -} - -function addToc() { - const nodes = $.parseHTML(TOC); - $('#main').before(nodes); -} - -function insertLinks() { - $('h2').each(function() { - const aId = $(this).attr('id'); - const text = $(this).text(); - const line = `#${text}`; - $(this).html(line); - }); -} - -function unindentBanner() { - const montyImg = $('img').first(); - montyImg.parent().addClass('banner'); - montyImg.parent().css({"margin-bottom": "20px", "padding-bottom": "7px"}) - const downloadPraragrapth = $('p').first(); - downloadPraragrapth.addClass('banner'); -} - -function updateDiagrams() { - $(`code:contains(${DIAGRAM_1_A})`).html(DIAGRAM_1_B); - $(`code:contains(${DIAGRAM_2_A})`).html(DIAGRAM_2_B); - $(`code:contains(${DIAGRAM_4_A})`).html(DIAGRAM_4_B); - $(`code:contains(${DIAGRAM_5_A})`).parent().remove(); - $(`code:contains(${DIAGRAM_6_A})`).html(DIAGRAM_6_B); - $(`code:contains(${DIAGRAM_7_A})`).html(DIAGRAM_7_B); - $(`code:contains(${DIAGRAM_8_A})`).html(DIAGRAM_8_B); - $(`code:contains(${DIAGRAM_9_A})`).html(DIAGRAM_9_B); - $(`code:contains(${DIAGRAM_95_A})`).html(DIAGRAM_95_B); - $(`code:contains(${DIAGRAM_115_A})`).html(DIAGRAM_115_B); - $(`code:contains(${DIAGRAM_12_A})`).html(DIAGRAM_12_B).removeClass("text").removeClass("language-text").addClass("python"); - $(`code:contains(${DIAGRAM_13_A})`).html(DIAGRAM_13_B).removeClass("text").removeClass("language-text").addClass("python"); - $(`code:contains(${DIAGRAM_14_A})`).parent().remove(); - $(`code:contains(${DIAGRAM_15_A})`).html(DIAGRAM_15_B).removeClass("text").removeClass("language-text").addClass("python"); - $(`code:contains(${DIAGRAM_16_A})`).html(DIAGRAM_16_B).removeClass("text").removeClass("language-text").addClass("python"); - $(`code:contains(${DIAGRAM_17_A})`).parent().remove(); -} - -function highlightCode() { - changeCodeLanguages(); - $('code').each(function(index) { - hljs.highlightBlock(this); - }); - fixClasses(); - fixHighlights(); - preventPageBreaks(); - fixPageBreaksFile(); - fixPageBreaksStruct(); - insertPageBreaks(); -} - -function changeCodeLanguages() { - setApaches(['', '', '
    ', '', '', '']); - $('code').not('.python').not('.text').not('.bash').not('.apache').addClass('python'); - $('code:contains( = <2d>[row_index, col_index])').removeClass().addClass('bash'); - $('code:contains(<1d_array> = <2d>[row_indices, col_indices])').removeClass().addClass('bash'); - $('code:contains(<2d_bools> = <2d> > )').removeClass().addClass('bash'); - $('code.perl').removeClass().addClass('python'); -} - -function setApaches(elements) { - for (el of elements) { - $(`code:contains(${el})`).addClass('apache'); - } -} - -function fixClasses() { - // Changes class="hljs-keyword" to class="hljs-title" of 'class' keyword. - $('.hljs-class').filter(':contains(class \')').find(':first-child').removeClass('hljs-keyword').addClass('hljs-title') -} - -function fixHighlights() { - $(`code:contains( = ±0b)`).html(BIN_HEX); - $(`code:contains( + fib(n)`).html(CACHE); - $(`code:contains(@debug(print_result=True))`).html(PARAMETRIZED_DECORATOR); - $(`code:contains(print/str/repr([]))`).html(REPR_USE_CASES); - $(`code:contains((self, a=None):)`).html(CONSTRUCTOR_OVERLOADING); - $(`code:contains(shutil.copy)`).html(SHUTIL_COPY); - $(`code:contains(os.rename)`).html(OS_RENAME); - $(`code:contains(\'s\')`).html(STRUCT_FORMAT); - $(`code:contains(match :)`).html(MATCH); - $(`code:contains(>>> match Path)`).html(MATCH_EXAMPLE); - $(`code:contains(import asyncio, collections, curses, curses.textpad, enum, random)`).html(COROUTINES); - $(`code:contains(import curses, os)`).html(CURSES); - $(`code:contains(pip3 install tqdm)`).html(PROGRESS_BAR); - $(`code:contains(>>> log.basicConfig()`).html(LOGGING_EXAMPLE); - $(`code:contains(a_float = max()`).html(AUDIO_1); - $(`code:contains(samples_f = (sin(i *)`).html(AUDIO_2); - $(`code:contains(collections, dataclasses, enum, io, itertools)`).html(MARIO); - $(`code:contains(>>> gb = df.groupby)`).html(GROUPBY); - $(`code:contains(cdef [= ])`).html(CYTHON_1); - $(`code:contains(cdef class :)`).html(CYTHON_2); - $(`ul:contains(Ctrl+F / ⌘F is usually sufficient.)`).html(INDEX); -} - -function preventPageBreaks() { - $(':header').each(function(index) { - var el = $(this) - var untilPre = el.nextUntil('pre') - var untilH2 = el.nextUntil('h2') - if ((untilPre.length < untilH2.length) || el.prop('tagName') === 'H1') { - untilPre.add(el).next().add(el).wrapAll("
    "); - } else { - untilH2.add(el).wrapAll("
    "); - } - }); -} - -function fixPageBreaksFile() { - const modesDiv = $('#file').parent().parent().parent() - move(modesDiv, 'file') - move(modesDiv, 'exceptions-1') -} - -function fixPageBreaksStruct() { - const formatDiv = $('#floatingpointtypesstructalwaysusesstandardsizes').parent().parent().parent().parent() - move(formatDiv, 'floatingpointtypesstructalwaysusesstandardsizes') - move(formatDiv, 'integertypesuseacapitalletterforunsignedtypeminimumandstandardsizesareinbrackets') - move(formatDiv, 'forstandardsizesstartformatstringwith') -} - -function move(anchor_el, el_id) { - const el = $('#'+el_id).parent() - anchor_el.after(el) -} - -function insertPageBreaks() { - insertPageBreakBefore('#decorator') - // insertPageBreakBefore('#print') -} - -function insertPageBreakBefore(an_id) { - $('
    ').insertBefore($(an_id).parent()) -} - -function fixPandasDiagram() { - const diagram_15 = '┏━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━┓'; - $(`code:contains(${diagram_15})`).find(".hljs-keyword:contains(and)").after("and"); - $(`code:contains(${diagram_15})`).find(".hljs-keyword:contains(as)").after("as"); - $(`code:contains(${diagram_15})`).find(".hljs-keyword:contains(is)").after("is"); - $(`code:contains(${diagram_15})`).find(".hljs-keyword:contains(if)").after("if"); - $(`code:contains(${diagram_15})`).find(".hljs-keyword:contains(or)").after("or"); - $(`code:contains(${diagram_15})`).find(".hljs-keyword:contains(else)").after("else"); - $(`code:contains(${diagram_15})`).find(".hljs-keyword").remove(); - $(`code:contains(${diagram_15})`).find(".hljs-string:contains(\'left_on/right_on\')").after("\'left_on/right_on\'"); - $(`code:contains(${diagram_15})`).find(".hljs-string:contains(\'left_on/right_on\')").remove(); - $(`code:contains(${diagram_15})`).find(".hljs-string:contains('on')").after("'on'"); - $(`code:contains(${diagram_15})`).find(".hljs-string:contains('on')").remove(); -} - -function removePlotImages() { - $('img[alt="Covid Deaths"]').remove(); - $('img[alt="Covid Cases"]').remove(); -} - -function fixABCSequenceDiv() { - $('#abcsequence').parent().insertBefore($('#tableofrequiredandautomaticallyavailablespecialmethods').parent()) -} - -function fixStructFormatDiv() { - const div = $('#format-2').parent() - $('#format-2').insertBefore(div) - $('#forstandardtypesizesandmanualalignmentpaddingstartformatstringwith').parent().insertBefore(div) -} - -function updateDate(template) { - const date = new Date(); - const date_str = date.toLocaleString('en-us', {month: 'long', day: 'numeric', year: 'numeric'}); - template = template.replace('May 20, 2021', date_str); - template = template.replace('May 20, 2021', date_str); - return template; -} - - -// UTIL - -function readFile(filename) { - try { - return fs.readFileSync(filename, 'utf8'); - } catch(e) { - console.error('Error:', e.stack); - } -} - -function writeToFile(filename, text) { - try { - return fs.writeFileSync(filename, text, 'utf8'); - } catch(e) { - console.error('Error:', e.stack); - } -} - -main(); +456