You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

871 lines
68 KiB

4 years ago
5 years ago
3 years ago
5 years ago
4 years ago
2 years ago
5 years ago
5 years ago
2 years ago
5 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
2 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
3 years ago
3 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
4 years ago
2 years ago
2 years ago
4 years ago
2 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
5 years ago
2 years ago
6 years ago
2 years ago
2 years ago
2 years ago
6 years ago
6 years ago
5 years ago
5 years ago
6 years ago
5 years ago
2 years ago
5 years ago
2 years ago
2 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
2 years ago
  1. #!/usr/bin/env node
  2. // Usage: node parse.js
  3. //
  4. // Script that creates index.html out of web/template.html and README.md.
  5. //
  6. // It is written in JS because this code used to be executed on the client side.
  7. // To install the Node.js and npm run:
  8. // $ sudo apt install nodejs npm # On macOS use `brew install ...` instead.
  9. //
  10. // To install dependencies globally, run:
  11. // $ npm install -g jsdom jquery showdown highlightjs@9.12.0
  12. //
  13. // If running on macOS and modules can't be found after installation add:
  14. // export NODE_PATH=/usr/local/lib/node_modules
  15. // to the ~/.bash_profile or ~/.bashrc file and run '$ bash'.
  16. //
  17. // To avoid problems with permissions and path variables, install modules
  18. // into project's directory using:
  19. // $ npm install jsdom jquery showdown highlightjs@9.12.0
  20. //
  21. // It is also advisable to add a Bash script into .git/hooks directory, that will
  22. // run this script before every commit. It should be named 'pre-commit' and it
  23. // should contain the following line: `./parse.js`.
  24. const fs = require('fs');
  25. const jsdom = require('jsdom');
  26. const showdown = require('showdown');
  27. const hljs = require('highlightjs');
  28. const TOC =
  29. '<br>' +
  30. '<h2 id="toc">Contents</h2>\n' +
  31. '<pre><code class="hljs bash" style="line-height: 1.3em;"><strong>ToC</strong> = {\n' +
  32. ' <strong><span class="hljs-string">\'1. Collections\'</span></strong>: [<a href="#list">List</a>, <a href="#dictionary">Dictionary</a>, <a href="#set">Set</a>, <a href="#tuple">Tuple</a>, <a href="#range">Range</a>, <a href="#enumerate">Enumerate</a>, <a href="#iterator">Iterator</a>, <a href="#generator">Generator</a>],\n' +
  33. ' <strong><span class="hljs-string">\'2. Types\'</span></strong>: [<a href="#type">Type</a>, <a href="#string">String</a>, <a href="#regex">Regular_Exp</a>, <a href="#format">Format</a>, <a href="#numbers">Numbers</a>, <a href="#combinatorics">Combinatorics</a>, <a href="#datetime">Datetime</a>],\n' +
  34. ' <strong><span class="hljs-string">\'3. Syntax\'</span></strong>: [<a href="#arguments">Args</a>, <a href="#inline">Inline</a>, <a href="#imports">Import</a>, <a href="#decorator">Decorator</a>, <a href="#class">Class</a>, <a href="#ducktypes">Duck_Types</a>, <a href="#enum">Enum</a>, <a href="#exceptions">Exception</a>],\n' +
  35. ' <strong><span class="hljs-string">\'4. System\'</span></strong>: [<a href="#exit">Exit</a>, <a href="#print">Print</a>, <a href="#input">Input</a>, <a href="#commandlinearguments">Command_Line_Arguments</a>, <a href="#open">Open</a>, <a href="#paths">Path</a>, <a href="#oscommands">OS_Commands</a>],\n' +
  36. ' <strong><span class="hljs-string">\'5. Data\'</span></strong>: [<a href="#json">JSON</a>, <a href="#pickle">Pickle</a>, <a href="#csv">CSV</a>, <a href="#sqlite">SQLite</a>, <a href="#bytes">Bytes</a>, <a href="#struct">Struct</a>, <a href="#array">Array</a>, <a href="#memoryview">Memory_View</a>, <a href="#deque">Deque</a>],\n' +
  37. ' <strong><span class="hljs-string">\'6. Advanced\'</span></strong>: [<a href="#threading">Threading</a>, <a href="#operator">Operator</a>, <a href="#introspection">Introspection</a>, <a href="#metaprogramming">Metaprograming</a>, <a href="#eval">Eval</a>, <a href="#coroutines">Coroutine</a>],\n' +
  38. ' <strong><span class="hljs-string">\'7. Libraries\'</span></strong>: [<a href="#progressbar">Progress_Bar</a>, <a href="#plot">Plot</a>, <a href="#table">Table</a>, <a href="#curses">Curses</a>, <a href="#logging">Logging</a>, <a href="#scraping">Scraping</a>, <a href="#web">Web</a>, <a href="#profiling">Profile</a>,\n' +
  39. ' <a href="#numpy">NumPy</a>, <a href="#image">Image</a>, <a href="#audio">Audio</a>, <a href="#pygame">Games</a>, <a href="#pandas">Data</a>]\n' +
  40. '}\n' +
  41. '</code></pre>\n';
  42. const LRU_CACHE =
  43. '<span class="hljs-keyword">from</span> functools <span class="hljs-keyword">import</span> lru_cache\n' +
  44. '\n' +
  45. '<span class="hljs-meta">@lru_cache(maxsize=None)</span>\n' +
  46. '<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">fib</span><span class="hljs-params">(n)</span>:</span>\n' +
  47. ' <span class="hljs-keyword">return</span> n <span class="hljs-keyword">if</span> n &lt; <span class="hljs-number">2</span> <span class="hljs-keyword">else</span> fib(n-<span class="hljs-number">2</span>) + fib(n-<span class="hljs-number">1</span>)\n';
  48. const PARAMETRIZED_DECORATOR =
  49. '<span class="hljs-keyword">from</span> functools <span class="hljs-keyword">import</span> wraps\n' +
  50. '\n' +
  51. '<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">debug</span><span class="hljs-params">(print_result=<span class="hljs-keyword">False</span>)</span>:</span>\n' +
  52. ' <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">decorator</span><span class="hljs-params">(func)</span>:</span>\n' +
  53. '<span class="hljs-meta"> @wraps(func)</span>\n' +
  54. ' <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">out</span><span class="hljs-params">(*args, **kwargs)</span>:</span>\n' +
  55. ' result = func(*args, **kwargs)\n' +
  56. ' print(func.__name__, result <span class="hljs-keyword">if</span> print_result <span class="hljs-keyword">else</span> <span class="hljs-string">\'\'</span>)\n' +
  57. ' <span class="hljs-keyword">return</span> result\n' +
  58. ' <span class="hljs-keyword">return</span> out\n' +
  59. ' <span class="hljs-keyword">return</span> decorator\n' +
  60. '\n' +
  61. '<span class="hljs-meta">@debug(print_result=True)</span>\n' +
  62. '<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">add</span><span class="hljs-params">(x, y)</span>:</span>\n' +
  63. ' <span class="hljs-keyword">return</span> x + y\n';
  64. const REPR_USE_CASES =
  65. 'print/str/repr([&lt;el&gt;])\n' +
  66. 'print/str/repr({&lt;el&gt;: &lt;el&gt;})\n' +
  67. '<span class="hljs-string">f\'<span class="hljs-subst">{&lt;el&gt;!r}</span>\'</span>\n' +
  68. 'Z = dataclasses.make_dataclass(<span class="hljs-string">\'Z\'</span>, [<span class="hljs-string">\'a\'</span>]); print/str/repr(Z(&lt;el&gt;))\n' +
  69. '<span class="hljs-meta">&gt;&gt;&gt; </span>&lt;el&gt;\n';
  70. const CONSTRUCTOR_OVERLOADING =
  71. '<span class="hljs-class"><span class="hljs-keyword">class</span> &lt;<span class="hljs-title">name</span>&gt;:</span>\n' +
  72. ' <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, a=<span class="hljs-keyword">None</span>)</span>:</span>\n' +
  73. ' self.a = a\n';
  74. const DATACLASS =
  75. '<span class="hljs-keyword">from</span> dataclasses <span class="hljs-keyword">import</span> make_dataclass\n' +
  76. '&lt;class&gt; = make_dataclass(<span class="hljs-string">\'&lt;class_name&gt;\'</span>, &lt;coll_of_attribute_names&gt;)\n' +
  77. '&lt;class&gt; = make_dataclass(<span class="hljs-string">\'&lt;class_name&gt;\'</span>, &lt;coll_of_tuples&gt;)\n' +
  78. '&lt;tuple&gt; = (<span class="hljs-string">\'&lt;attr_name&gt;\'</span>, &lt;type&gt; [, &lt;default_value&gt;])';
  79. const SHUTIL_COPY =
  80. 'shutil.copy(from, to) <span class="hljs-comment"># Copies the file. \'to\' can exist or be a dir.</span>\n' +
  81. 'shutil.copytree(from, to) <span class="hljs-comment"># Copies the directory. \'to\' must not exist.</span>\n';
  82. const OS_RENAME =
  83. 'os.rename(from, to) <span class="hljs-comment"># Renames/moves the file or directory.</span>\n' +
  84. 'os.replace(from, to) <span class="hljs-comment"># Same, but overwrites \'to\' if it exists.</span>\n';
  85. const STRUCT_FORMAT =
  86. '<span class="hljs-section">\'&lt;n&gt;s\'</span><span class="hljs-attribute"></span>';
  87. const TYPE =
  88. '&lt;class&gt; = type(<span class="hljs-string">\'&lt;class_name&gt;\'</span>, &lt;tuple_of_parents&gt;, &lt;dict_of_class_attributes&gt;)';
  89. const EVAL =
  90. '<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> ast <span class="hljs-keyword">import</span> literal_eval\n' +
  91. '<span class="hljs-meta">&gt;&gt;&gt; </span>literal_eval(<span class="hljs-string">\'[1, 2, 3]\'</span>)\n' +
  92. '[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]\n' +
  93. '<span class="hljs-meta">&gt;&gt;&gt; </span>literal_eval(<span class="hljs-string">\'1 + 2\'</span>)\n' +
  94. 'ValueError: malformed node or string\n';
  95. const COROUTINES =
  96. '<span class="hljs-keyword">import</span> asyncio, collections, curses, curses.textpad, enum, random\n' +
  97. '\n' +
  98. 'P = collections.namedtuple(<span class="hljs-string">\'P\'</span>, <span class="hljs-string">\'x y\'</span>) <span class="hljs-comment"># Position</span>\n' +
  99. 'D = enum.Enum(<span class="hljs-string">\'D\'</span>, <span class="hljs-string">\'n e s w\'</span>) <span class="hljs-comment"># Direction</span>\n' +
  100. 'W, H = <span class="hljs-number">15</span>, <span class="hljs-number">7</span> <span class="hljs-comment"># Width, Height</span>\n' +
  101. '\n' +
  102. '<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span><span class="hljs-params">(screen)</span>:</span>\n' +
  103. ' curses.curs_set(<span class="hljs-number">0</span>) <span class="hljs-comment"># Makes cursor invisible.</span>\n' +
  104. ' screen.nodelay(<span class="hljs-keyword">True</span>) <span class="hljs-comment"># Makes getch() non-blocking.</span>\n' +
  105. ' asyncio.run(main_coroutine(screen)) <span class="hljs-comment"># Starts running asyncio code.</span>\n' +
  106. '\n' +
  107. '<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main_coroutine</span><span class="hljs-params">(screen)</span>:</span>\n' +
  108. ' moves = asyncio.Queue()\n' +
  109. ' state = {<span class="hljs-string">\'*\'</span>: P(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>), **{id_: P(W//<span class="hljs-number">2</span>, H//<span class="hljs-number">2</span>) <span class="hljs-keyword">for</span> id_ <span class="hljs-keyword">in</span> range(<span class="hljs-number">10</span>)}}\n' +
  110. ' ai = [random_controller(id_, moves) <span class="hljs-keyword">for</span> id_ <span class="hljs-keyword">in</span> range(<span class="hljs-number">10</span>)]\n' +
  111. ' mvc = [human_controller(screen, moves), model(moves, state), view(state, screen)]\n' +
  112. ' tasks = [asyncio.create_task(cor) <span class="hljs-keyword">for</span> cor <span class="hljs-keyword">in</span> ai + mvc]\n' +
  113. ' <span class="hljs-keyword">await</span> asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)\n' +
  114. '\n' +
  115. '<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">random_controller</span><span class="hljs-params">(id_, moves)</span>:</span>\n' +
  116. ' <span class="hljs-keyword">while</span> <span class="hljs-keyword">True</span>:\n' +
  117. ' d = random.choice(list(D))\n' +
  118. ' moves.put_nowait((id_, d))\n' +
  119. ' <span class="hljs-keyword">await</span> asyncio.sleep(random.triangular(<span class="hljs-number">0.01</span>, <span class="hljs-number">0.65</span>))\n' +
  120. '\n' +
  121. '<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">human_controller</span><span class="hljs-params">(screen, moves)</span>:</span>\n' +
  122. ' <span class="hljs-keyword">while</span> <span class="hljs-keyword">True</span>:\n' +
  123. ' ch = screen.getch()\n' +
  124. ' key_mappings = {<span class="hljs-number">258</span>: D.s, <span class="hljs-number">259</span>: D.n, <span class="hljs-number">260</span>: D.w, <span class="hljs-number">261</span>: D.e}\n' +
  125. ' <span class="hljs-keyword">if</span> ch <span class="hljs-keyword">in</span> key_mappings:\n' +
  126. ' moves.put_nowait((<span class="hljs-string">\'*\'</span>, key_mappings[ch]))\n' +
  127. ' <span class="hljs-keyword">await</span> asyncio.sleep(<span class="hljs-number">0.005</span>)\n' +
  128. '\n' +
  129. '<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">model</span><span class="hljs-params">(moves, state)</span>:</span>\n' +
  130. ' <span class="hljs-keyword">while</span> state[<span class="hljs-string">\'*\'</span>] <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> (state[id_] <span class="hljs-keyword">for</span> id_ <span class="hljs-keyword">in</span> range(<span class="hljs-number">10</span>)):\n' +
  131. ' id_, d = <span class="hljs-keyword">await</span> moves.get()\n' +
  132. ' x, y = state[id_]\n' +
  133. ' deltas = {D.n: P(<span class="hljs-number">0</span>, <span class="hljs-number">-1</span>), D.e: P(<span class="hljs-number">1</span>, <span class="hljs-number">0</span>), D.s: P(<span class="hljs-number">0</span>, <span class="hljs-number">1</span>), D.w: P(<span class="hljs-number">-1</span>, <span class="hljs-number">0</span>)}\n' +
  134. ' state[id_] = P((x + deltas[d].x) % W, (y + deltas[d].y) % H)\n' +
  135. '\n' +
  136. '<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">view</span><span class="hljs-params">(state, screen)</span>:</span>\n' +
  137. ' offset = P(curses.COLS//<span class="hljs-number">2</span> - W//<span class="hljs-number">2</span>, curses.LINES//<span class="hljs-number">2</span> - H//<span class="hljs-number">2</span>)\n' +
  138. ' <span class="hljs-keyword">while</span> <span class="hljs-keyword">True</span>:\n' +
  139. ' screen.erase()\n' +
  140. ' curses.textpad.rectangle(screen, offset.y-<span class="hljs-number">1</span>, offset.x-<span class="hljs-number">1</span>, offset.y+H, offset.x+W)\n' +
  141. ' <span class="hljs-keyword">for</span> id_, p <span class="hljs-keyword">in</span> state.items():\n' +
  142. ' screen.addstr(offset.y + (p.y - state[<span class="hljs-string">\'*\'</span>].y + H//<span class="hljs-number">2</span>) % H,\n' +
  143. ' offset.x + (p.x - state[<span class="hljs-string">\'*\'</span>].x + W//<span class="hljs-number">2</span>) % W, str(id_))\n' +
  144. ' <span class="hljs-keyword">await</span> asyncio.sleep(<span class="hljs-number">0.005</span>)\n' +
  145. '\n' +
  146. '<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">\'__main__\'</span>:\n' +
  147. ' curses.wrapper(main)\n';
  148. const PROGRESS_BAR =
  149. '<span class="hljs-comment"># $ pip3 install tqdm</span>\n' +
  150. '<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> tqdm <span class="hljs-keyword">import</span> tqdm\n' +
  151. '<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> time <span class="hljs-keyword">import</span> sleep\n' +
  152. '<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">for</span> el <span class="hljs-keyword">in</span> tqdm([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>], desc=<span class="hljs-string">\'Processing\'</span>):\n' +
  153. '<span class="hljs-meta">... </span> sleep(<span class="hljs-number">1</span>)\n' +
  154. 'Processing: 100%|████████████████████| 3/3 [00:03&lt;00:00, 1.00s/it]\n';
  155. const LOGGING_EXAMPLE =
  156. '<span class="hljs-meta">&gt;&gt;&gt; </span>logging.basicConfig(level=<span class="hljs-string">\'WARNING\'</span>)\n' +
  157. '<span class="hljs-meta">&gt;&gt;&gt; </span>logger = logging.getLogger(<span class="hljs-string">\'my_module\'</span>)\n' +
  158. '<span class="hljs-meta">&gt;&gt;&gt; </span>handler = logging.FileHandler(<span class="hljs-string">\'test.log\'</span>)\n' +
  159. '<span class="hljs-meta">&gt;&gt;&gt; </span>formatter = logging.Formatter(<span class="hljs-string">\'%(asctime)s %(levelname)s:%(name)s:%(message)s\'</span>)\n' +
  160. '<span class="hljs-meta">&gt;&gt;&gt; </span>handler.setFormatter(formatter)\n' +
  161. '<span class="hljs-meta">&gt;&gt;&gt; </span>logger.addHandler(handler)\n' +
  162. '<span class="hljs-meta">&gt;&gt;&gt; </span>logger.critical(<span class="hljs-string">\'Running out of disk space.\'</span>)\n' +
  163. 'CRITICAL:my_module:Running out of disk space.\n' +
  164. '<span class="hljs-meta">&gt;&gt;&gt; </span>open(<span class="hljs-string">\'test.log\'</span>).read()\n' +
  165. '2023-02-07 23:21:01,430 CRITICAL:my_module:Running out of disk space.\n';
  166. const AUDIO =
  167. '<span class="hljs-keyword">from</span> math <span class="hljs-keyword">import</span> pi, sin\n' +
  168. 'samples_f = (sin(i * <span class="hljs-number">2</span> * pi * <span class="hljs-number">440</span> / <span class="hljs-number">44100</span>) <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">100_000</span>))\n' +
  169. 'write_to_wav_file(<span class="hljs-string">\'test.wav\'</span>, samples_f)\n';
  170. const MARIO =
  171. '<span class="hljs-keyword">import</span> collections, dataclasses, enum, io, itertools <span class="hljs-keyword">as</span> it, pygame <span class="hljs-keyword">as</span> pg, urllib.request\n' +
  172. '<span class="hljs-keyword">from</span> random <span class="hljs-keyword">import</span> randint\n' +
  173. '\n' +
  174. 'P = collections.namedtuple(<span class="hljs-string">\'P\'</span>, <span class="hljs-string">\'x y\'</span>) <span class="hljs-comment"># Position</span>\n' +
  175. 'D = enum.Enum(<span class="hljs-string">\'D\'</span>, <span class="hljs-string">\'n e s w\'</span>) <span class="hljs-comment"># Direction</span>\n' +
  176. 'W, H, MAX_S = <span class="hljs-number">50</span>, <span class="hljs-number">50</span>, P(<span class="hljs-number">5</span>, <span class="hljs-number">10</span>) <span class="hljs-comment"># Width, Height, Max speed</span>\n' +
  177. '\n' +
  178. '<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>:</span>\n' +
  179. ' <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_screen</span><span class="hljs-params">()</span>:</span>\n' +
  180. ' pg.init()\n' +
  181. ' <span class="hljs-keyword">return</span> pg.display.set_mode((W*<span class="hljs-number">16</span>, H*<span class="hljs-number">16</span>))\n' +
  182. ' <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_images</span><span class="hljs-params">()</span>:</span>\n' +
  183. ' url = <span class="hljs-string">\'https://gto76.github.io/python-cheatsheet/web/mario_bros.png\'</span>\n' +
  184. ' img = pg.image.load(io.BytesIO(urllib.request.urlopen(url).read()))\n' +
  185. ' <span class="hljs-keyword">return</span> [img.subsurface(get_rect(x, <span class="hljs-number">0</span>)) <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> range(img.get_width() // <span class="hljs-number">16</span>)]\n' +
  186. ' <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_mario</span><span class="hljs-params">()</span>:</span>\n' +
  187. ' Mario = dataclasses.make_dataclass(<span class="hljs-string">\'Mario\'</span>, <span class="hljs-string">\'rect spd facing_left frame_cycle\'</span>.split())\n' +
  188. ' <span class="hljs-keyword">return</span> Mario(get_rect(<span class="hljs-number">1</span>, <span class="hljs-number">1</span>), P(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>), <span class="hljs-keyword">False</span>, it.cycle(range(<span class="hljs-number">3</span>)))\n' +
  189. ' <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_tiles</span><span class="hljs-params">()</span>:</span>\n' +
  190. ' border = [(x, y) <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> range(W) <span class="hljs-keyword">for</span> y <span class="hljs-keyword">in</span> range(H) <span class="hljs-keyword">if</span> x <span class="hljs-keyword">in</span> [<span class="hljs-number">0</span>, W-<span class="hljs-number">1</span>] <span class="hljs-keyword">or</span> y <span class="hljs-keyword">in</span> [<span class="hljs-number">0</span>, H-<span class="hljs-number">1</span>]]\n' +
  191. ' platforms = [(randint(<span class="hljs-number">1</span>, W-<span class="hljs-number">2</span>), randint(<span class="hljs-number">2</span>, H-<span class="hljs-number">2</span>)) <span class="hljs-keyword">for</span> _ <span class="hljs-keyword">in</span> range(W*H // <span class="hljs-number">10</span>)]\n' +
  192. ' <span class="hljs-keyword">return</span> [get_rect(x, y) <span class="hljs-keyword">for</span> x, y <span class="hljs-keyword">in</span> border + platforms]\n' +
  193. ' <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_rect</span><span class="hljs-params">(x, y)</span>:</span>\n' +
  194. ' <span class="hljs-keyword">return</span> pg.Rect(x*<span class="hljs-number">16</span>, y*<span class="hljs-number">16</span>, <span class="hljs-number">16</span>, <span class="hljs-number">16</span>)\n' +
  195. ' run(get_screen(), get_images(), get_mario(), get_tiles())\n' +
  196. '\n' +
  197. '<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">run</span><span class="hljs-params">(screen, images, mario, tiles)</span>:</span>\n' +
  198. ' clock = pg.time.Clock()\n' +
  199. ' pressed = set()\n' +
  200. ' <span class="hljs-keyword">while</span> <span class="hljs-keyword">not</span> pg.event.get(pg.QUIT) <span class="hljs-keyword">and</span> clock.tick(<span class="hljs-number">28</span>):\n' +
  201. ' keys = {pg.K_UP: D.n, pg.K_RIGHT: D.e, pg.K_DOWN: D.s, pg.K_LEFT: D.w}\n' +
  202. ' pressed |= {keys.get(e.key) <span class="hljs-keyword">for</span> e <span class="hljs-keyword">in</span> pg.event.get(pg.KEYDOWN)}\n' +
  203. ' pressed -= {keys.get(e.key) <span class="hljs-keyword">for</span> e <span class="hljs-keyword">in</span> pg.event.get(pg.KEYUP)}\n' +
  204. ' update_speed(mario, tiles, pressed)\n' +
  205. ' update_position(mario, tiles)\n' +
  206. ' draw(screen, images, mario, tiles, pressed)\n' +
  207. '\n' +
  208. '<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">update_speed</span><span class="hljs-params">(mario, tiles, pressed)</span>:</span>\n' +
  209. ' x, y = mario.spd\n' +
  210. ' x += <span class="hljs-number">2</span> * ((D.e <span class="hljs-keyword">in</span> pressed) - (D.w <span class="hljs-keyword">in</span> pressed))\n' +
  211. ' x += (x &lt; <span class="hljs-number">0</span>) - (x &gt; <span class="hljs-number">0</span>)\n' +
  212. ' y += <span class="hljs-number">1</span> <span class="hljs-keyword">if</span> D.s <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> get_boundaries(mario.rect, tiles) <span class="hljs-keyword">else</span> (D.n <span class="hljs-keyword">in</span> pressed) * <span class="hljs-number">-10</span>\n' +
  213. ' mario.spd = P(x=max(-MAX_S.x, min(MAX_S.x, x)), y=max(-MAX_S.y, min(MAX_S.y, y)))\n' +
  214. '\n' +
  215. '<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">update_position</span><span class="hljs-params">(mario, tiles)</span>:</span>\n' +
  216. ' x, y = mario.rect.topleft\n' +
  217. ' n_steps = max(abs(s) <span class="hljs-keyword">for</span> s <span class="hljs-keyword">in</span> mario.spd)\n' +
  218. ' <span class="hljs-keyword">for</span> _ <span class="hljs-keyword">in</span> range(n_steps):\n' +
  219. ' mario.spd = stop_on_collision(mario.spd, get_boundaries(mario.rect, tiles))\n' +
  220. ' mario.rect.topleft = x, y = x + (mario.spd.x / n_steps), y + (mario.spd.y / n_steps)\n' +
  221. '\n' +
  222. '<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_boundaries</span><span class="hljs-params">(rect, tiles)</span>:</span>\n' +
  223. ' deltas = {D.n: P(<span class="hljs-number">0</span>, <span class="hljs-number">-1</span>), D.e: P(<span class="hljs-number">1</span>, <span class="hljs-number">0</span>), D.s: P(<span class="hljs-number">0</span>, <span class="hljs-number">1</span>), D.w: P(<span class="hljs-number">-1</span>, <span class="hljs-number">0</span>)}\n' +
  224. ' <span class="hljs-keyword">return</span> {d <span class="hljs-keyword">for</span> d, delta <span class="hljs-keyword">in</span> deltas.items() <span class="hljs-keyword">if</span> rect.move(delta).collidelist(tiles) != <span class="hljs-number">-1</span>}\n' +
  225. '\n' +
  226. '<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">stop_on_collision</span><span class="hljs-params">(spd, bounds)</span>:</span>\n' +
  227. ' <span class="hljs-keyword">return</span> P(x=<span class="hljs-number">0</span> <span class="hljs-keyword">if</span> (D.w <span class="hljs-keyword">in</span> bounds <span class="hljs-keyword">and</span> spd.x &lt; <span class="hljs-number">0</span>) <span class="hljs-keyword">or</span> (D.e <span class="hljs-keyword">in</span> bounds <span class="hljs-keyword">and</span> spd.x &gt; <span class="hljs-number">0</span>) <span class="hljs-keyword">else</span> spd.x,\n' +
  228. ' y=<span class="hljs-number">0</span> <span class="hljs-keyword">if</span> (D.n <span class="hljs-keyword">in</span> bounds <span class="hljs-keyword">and</span> spd.y &lt; <span class="hljs-number">0</span>) <span class="hljs-keyword">or</span> (D.s <span class="hljs-keyword">in</span> bounds <span class="hljs-keyword">and</span> spd.y &gt; <span class="hljs-number">0</span>) <span class="hljs-keyword">else</span> spd.y)\n' +
  229. '\n' +
  230. '<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">draw</span><span class="hljs-params">(screen, images, mario, tiles, pressed)</span>:</span>\n' +
  231. ' <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_marios_image_index</span><span class="hljs-params">()</span>:</span>\n' +
  232. ' <span class="hljs-keyword">if</span> D.s <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> get_boundaries(mario.rect, tiles):\n' +
  233. ' <span class="hljs-keyword">return</span> <span class="hljs-number">4</span>\n' +
  234. ' <span class="hljs-keyword">return</span> next(mario.frame_cycle) <span class="hljs-keyword">if</span> {D.w, D.e} &amp; pressed <span class="hljs-keyword">else</span> <span class="hljs-number">6</span>\n' +
  235. ' screen.fill((<span class="hljs-number">85</span>, <span class="hljs-number">168</span>, <span class="hljs-number">255</span>))\n' +
  236. ' mario.facing_left = (D.w <span class="hljs-keyword">in</span> pressed) <span class="hljs-keyword">if</span> {D.w, D.e} &amp; pressed <span class="hljs-keyword">else</span> mario.facing_left\n' +
  237. ' screen.blit(images[get_marios_image_index() + mario.facing_left * <span class="hljs-number">9</span>], mario.rect)\n' +
  238. ' <span class="hljs-keyword">for</span> t <span class="hljs-keyword">in</span> tiles:\n' +
  239. ' screen.blit(images[<span class="hljs-number">18</span> <span class="hljs-keyword">if</span> t.x <span class="hljs-keyword">in</span> [<span class="hljs-number">0</span>, (W-<span class="hljs-number">1</span>)*<span class="hljs-number">16</span>] <span class="hljs-keyword">or</span> t.y <span class="hljs-keyword">in</span> [<span class="hljs-number">0</span>, (H-<span class="hljs-number">1</span>)*<span class="hljs-number">16</span>] <span class="hljs-keyword">else</span> <span class="hljs-number">19</span>], t)\n' +
  240. ' pg.display.flip()\n' +
  241. '\n' +
  242. '<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">\'__main__\'</span>:\n' +
  243. ' main()\n';
  244. const PYINSTALLER =
  245. '$ pip3 install pyinstaller\n' +
  246. '$ pyinstaller script.py <span class="hljs-comment"># Compiles into \'./dist/script\' directory.</span>\n' +
  247. '$ pyinstaller script.py --onefile <span class="hljs-comment"># Compiles into \'./dist/script\' console app.</span>\n' +
  248. '$ pyinstaller script.py --windowed <span class="hljs-comment"># Compiles into \'./dist/script\' windowed app.</span>\n' +
  249. '$ pyinstaller script.py --add-data \'&lt;path&gt;:.\' <span class="hljs-comment"># Adds file to the root of the executable.</span>\n';
  250. const INDEX =
  251. '<li><strong>Only available in the <a href="https://transactions.sendowl.com/products/78175486/4422834F/view">PDF</a>.</strong></li>\n' +
  252. '<li><strong>Ctrl+F / ⌘F is usually sufficient.</strong></li>\n' +
  253. '<li><strong>Searching <code class="python hljs"><span class="hljs-string">\'#&lt;title&gt;\'</span></code> will limit the search to the titles.</strong></li>\n';
  254. const DIAGRAM_1_A =
  255. '+------------------+------------+------------+------------+\n' +
  256. '| | Iterable | Collection | Sequence |\n' +
  257. '+------------------+------------+------------+------------+\n';
  258. const DIAGRAM_1_B =
  259. '┏━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┓\n' +
  260. '┃ │ Iterable │ Collection │ Sequence ┃\n' +
  261. '┠──────────────────┼────────────┼────────────┼────────────┨\n' +
  262. '┃ list, range, str │ ✓ │ ✓ │ ✓ ┃\n' +
  263. '┃ dict, set │ ✓ │ ✓ │ ┃\n' +
  264. '┃ iter │ ✓ │ │ ┃\n' +
  265. '┗━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┛\n';
  266. const DIAGRAM_2_A =
  267. '+--------------------+----------+----------+----------+----------+----------+\n' +
  268. '| | Number | Complex | Real | Rational | Integral |\n' +
  269. '+--------------------+----------+----------+----------+----------+----------+\n';
  270. const DIAGRAM_2_B =
  271. '┏━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┓\n' +
  272. '┃ │ Number │ Complex │ Real │ Rational │ Integral ┃\n' +
  273. '┠────────────────────┼──────────┼──────────┼──────────┼──────────┼──────────┨\n' +
  274. '┃ int │ ✓ │ ✓ │ ✓ │ ✓ │ ✓ ┃\n' +
  275. '┃ fractions.Fraction │ ✓ │ ✓ │ ✓ │ ✓ │ ┃\n' +
  276. '┃ float │ ✓ │ ✓ │ ✓ │ │ ┃\n' +
  277. '┃ complex │ ✓ │ ✓ │ │ │ ┃\n' +
  278. '┃ decimal.Decimal │ ✓ │ │ │ │ ┃\n' +
  279. '┗━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┛\n';
  280. const DIAGRAM_3_A =
  281. '+---------------+----------+----------+----------+----------+----------+\n';
  282. const DIAGRAM_3_B =
  283. '┏━━━━━━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┓\n' +
  284. '┃ │ [ !#$%…] │ [a-zA-Z] │ [¼½¾] │ [²³¹] │ [0-9] ┃\n' +
  285. '┠───────────────┼──────────┼──────────┼──────────┼──────────┼──────────┨\n' +
  286. '┃ isprintable() │ ✓ │ ✓ │ ✓ │ ✓ │ ✓ ┃\n' +
  287. '┃ isalnum() │ │ ✓ │ ✓ │ ✓ │ ✓ ┃\n' +
  288. '┃ isnumeric() │ │ │ ✓ │ ✓ │ ✓ ┃\n' +
  289. '┃ isdigit() │ │ │ │ ✓ │ ✓ ┃\n' +
  290. '┃ isdecimal() │ │ │ │ │ ✓ ┃\n' +
  291. '┗━━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┛\n';
  292. const DIAGRAM_4_A =
  293. "+--------------+----------------+----------------+----------------+----------------+\n" +
  294. "| | {<float>} | {<float>:f} | {<float>:e} | {<float>:%} |\n" +
  295. "+--------------+----------------+----------------+----------------+----------------+\n";
  296. const DIAGRAM_4_B =
  297. "┏━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━┓\n" +
  298. "┃ │ {&lt;float&gt;} │ {&lt;float&gt;:f} │ {&lt;float&gt;:e} │ {&lt;float&gt;:%} ┃\n" +
  299. "┠──────────────┼────────────────┼────────────────┼────────────────┼────────────────┨\n" +
  300. "┃ 0.000056789 │ '5.6789e-05' │ '0.000057' │ '5.678900e-05' │ '0.005679%' ┃\n" +
  301. "┃ 0.00056789 │ '0.00056789' │ '0.000568' │ '5.678900e-04' │ '0.056789%' ┃\n" +
  302. "┃ 0.0056789 │ '0.0056789' │ '0.005679' │ '5.678900e-03' │ '0.567890%' ┃\n" +
  303. "┃ 0.056789 │ '0.056789' │ '0.056789' │ '5.678900e-02' │ '5.678900%' ┃\n" +
  304. "┃ 0.56789 │ '0.56789' │ '0.567890' │ '5.678900e-01' │ '56.789000%' ┃\n" +
  305. "┃ 5.6789 │ '5.6789' │ '5.678900' │ '5.678900e+00' │ '567.890000%' ┃\n" +
  306. "┃ 56.789 │ '56.789' │ '56.789000' │ '5.678900e+01' │ '5678.900000%' ┃\n" +
  307. "┗━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━┛\n" +
  308. "\n" +
  309. "┏━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━┓\n" +
  310. "┃ │ {&lt;float&gt;:.2} │ {&lt;float&gt;:.2f} │ {&lt;float&gt;:.2e} │ {&lt;float&gt;:.2%} ┃\n" +
  311. "┠──────────────┼────────────────┼────────────────┼────────────────┼────────────────┨\n" +
  312. "┃ 0.000056789 │ '5.7e-05' │ '0.00' │ '5.68e-05' │ '0.01%' ┃\n" +
  313. "┃ 0.00056789 │ '0.00057' │ '0.00' │ '5.68e-04' │ '0.06%' ┃\n" +
  314. "┃ 0.0056789 │ '0.0057' │ '0.01' │ '5.68e-03' │ '0.57%' ┃\n" +
  315. "┃ 0.056789 │ '0.057' │ '0.06' │ '5.68e-02' │ '5.68%' ┃\n" +
  316. "┃ 0.56789 │ '0.57' │ '0.57' │ '5.68e-01' │ '56.79%' ┃\n" +
  317. "┃ 5.6789 │ '5.7' │ '5.68' │ '5.68e+00' │ '567.89%' ┃\n" +
  318. "┃ 56.789 │ '5.7e+01' │ '56.79' │ '5.68e+01' │ '5678.90%' ┃\n" +
  319. "┗━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━┛\n";
  320. const DIAGRAM_5_A =
  321. "+--------------+----------------+----------------+----------------+----------------+\n" +
  322. "| | {<float>:.2} | {<float>:.2f} | {<float>:.2e} | {<float>:.2%} |\n" +
  323. "+--------------+----------------+----------------+----------------+----------------+\n";
  324. const DIAGRAM_6_A =
  325. '+------------+------------+------------+------------+--------------+\n' +
  326. '| | Iterable | Collection | Sequence | abc.Sequence |\n' +
  327. '+------------+------------+------------+------------+--------------+\n';
  328. const DIAGRAM_6_B =
  329. '┏━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━━━┓\n' +
  330. '┃ │ Iterable │ Collection │ Sequence │ abc.Sequence ┃\n' +
  331. '┠────────────┼────────────┼────────────┼────────────┼──────────────┨\n' +
  332. '┃ iter() │ ! │ ! │ ✓ │ ✓ ┃\n' +
  333. '┃ contains() │ ✓ │ ✓ │ ✓ │ ✓ ┃\n' +
  334. '┃ len() │ │ ! │ ! │ ! ┃\n' +
  335. '┃ getitem() │ │ │ ! │ ! ┃\n' +
  336. '┃ reversed() │ │ │ ✓ │ ✓ ┃\n' +
  337. '┃ index() │ │ │ │ ✓ ┃\n' +
  338. '┃ count() │ │ │ │ ✓ ┃\n' +
  339. '┗━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━━━┛\n';
  340. const DIAGRAM_7_A =
  341. 'BaseException\n' +
  342. ' +-- SystemExit';
  343. const DIAGRAM_7_B =
  344. "BaseException\n" +
  345. " ├── SystemExit <span class='hljs-comment'># Raised by the sys.exit() function.</span>\n" +
  346. " ├── KeyboardInterrupt <span class='hljs-comment'># Raised when the user hits the interrupt key (ctrl-c).</span>\n" +
  347. " └── Exception <span class='hljs-comment'># User-defined exceptions should be derived from this class.</span>\n" +
  348. " ├── ArithmeticError <span class='hljs-comment'># Base class for arithmetic errors.</span>\n" +
  349. " │ └── ZeroDivisionError <span class='hljs-comment'># Raised when dividing by zero.</span>\n" +
  350. " ├── AssertionError <span class='hljs-comment'># Raised by `assert &lt;exp&gt;` if expression returns false value.</span>\n" +
  351. " ├── AttributeError <span class='hljs-comment'># Raised when object doesn't have requested attribute/method.</span>\n" +
  352. " ├── EOFError <span class='hljs-comment'># Raised by input() when it hits an end-of-file condition.</span>\n" +
  353. " ├── LookupError <span class='hljs-comment'># Base class for errors when a collection can't find an item.</span>\n" +
  354. " │ ├── IndexError <span class='hljs-comment'># Raised when a sequence index is out of range.</span>\n" +
  355. " │ └── KeyError <span class='hljs-comment'># Raised when a dictionary key or set element is missing.</span>\n" +
  356. " ├── MemoryError <span class='hljs-comment'># Out of memory. Could be too late to start deleting vars.</span>\n" +
  357. " ├── NameError <span class='hljs-comment'># Raised when nonexistent name (variable/func/class) is used.</span>\n" +
  358. " │ └── UnboundLocalError <span class='hljs-comment'># Raised when local name is used before it's being defined.</span>\n" +
  359. " ├── OSError <span class='hljs-comment'># Errors such as FileExistsError/PermissionError (see Open).</span>\n" +
  360. " ├── RuntimeError <span class='hljs-comment'># Raised by errors that don't fall into other categories.</span>\n" +
  361. " │ └── RecursionError <span class='hljs-comment'># Raised when the maximum recursion depth is exceeded.</span>\n" +
  362. " ├── StopIteration <span class='hljs-comment'># Raised by next() when run on an empty iterator.</span>\n" +
  363. " ├── TypeError <span class='hljs-comment'># Raised when an argument is of the wrong type.</span>\n" +
  364. " └── ValueError <span class='hljs-comment'># When argument has the right type but inappropriate value.</span>\n" +
  365. " └── UnicodeError <span class='hljs-comment'># Raised when encoding/decoding strings to/from bytes fails.</span>\n";
  366. const DIAGRAM_8_A =
  367. '+-----------+------------+------------+------------+\n' +
  368. '| | List | Set | Dict |\n' +
  369. '+-----------+------------+------------+------------+\n';
  370. const DIAGRAM_8_B =
  371. '┏━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┓\n' +
  372. '┃ │ List │ Set │ Dict ┃\n' +
  373. '┠───────────┼────────────┼────────────┼────────────┨\n' +
  374. '┃ getitem() │ IndexError │ │ KeyError ┃\n' +
  375. '┃ pop() │ IndexError │ KeyError │ KeyError ┃\n' +
  376. '┃ remove() │ ValueError │ KeyError │ ┃\n' +
  377. '┃ index() │ ValueError │ │ ┃\n' +
  378. '┗━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┛\n';
  379. const DIAGRAM_9_A =
  380. '+------------------+--------------+--------------+--------------+\n' +
  381. '| | excel | excel-tab | unix |\n' +
  382. '+------------------+--------------+--------------+--------------+\n';
  383. const DIAGRAM_9_B =
  384. "┏━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━┓\n" +
  385. "┃ │ excel │ excel-tab │ unix ┃\n" +
  386. "┠──────────────────┼──────────────┼──────────────┼──────────────┨\n" +
  387. "┃ delimiter │ ',' │ '\\t' │ ',' ┃\n" +
  388. "┃ quotechar │ '\"' │ '\"' │ '\"' ┃\n" +
  389. "┃ doublequote │ True │ True │ True ┃\n" +
  390. "┃ skipinitialspace │ False │ False │ False ┃\n" +
  391. "┃ lineterminator │ '\\r\\n' │ '\\r\\n' │ '\\n' ┃\n" +
  392. "┃ quoting │ 0 │ 0 │ 1 ┃\n" +
  393. "┃ escapechar │ None │ None │ None ┃\n" +
  394. "┗━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━┛\n";
  395. const DIAGRAM_95_A =
  396. "+------------+--------------+-----------+-----------------------------------+\n" +
  397. "| Dialect | pip3 install | import | Dependencies |\n" +
  398. "+------------+--------------+-----------+-----------------------------------+\n";
  399. const DIAGRAM_95_B =
  400. "┏━━━━━━━━━━━━┯━━━━━━━━━━━━━━┯━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n" +
  401. "┃ Dialect │ pip3 install │ import │ Dependencies ┃\n" +
  402. "┠────────────┼──────────────┼───────────┼───────────────────────────────────┨\n" +
  403. "┃ mysql │ mysqlclient │ MySQLdb │ www.pypi.org/project/mysqlclient ┃\n" +
  404. "┃ postgresql │ psycopg2 │ psycopg2 │ www.psycopg.org/docs/install.html ┃\n" +
  405. "┃ mssql │ pyodbc │ pyodbc │ apt install g++ unixodbc-dev ┃\n" +
  406. "┃ oracle │ cx_oracle │ cx_Oracle │ Oracle Instant Client ┃\n" +
  407. "┗━━━━━━━━━━━━┷━━━━━━━━━━━━━━┷━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n";
  408. const DIAGRAM_10_A =
  409. '+-------------+-------------+\n' +
  410. '| Classes | Metaclasses |\n' +
  411. '+-------------+-------------|\n' +
  412. '| MyClass <-- MyMetaClass |\n';
  413. const DIAGRAM_10_B =
  414. '┏━━━━━━━━━━━━━┯━━━━━━━━━━━━━┓\n' +
  415. '┃ Classes │ Metaclasses ┃\n' +
  416. '┠─────────────┼─────────────┨\n' +
  417. '┃ MyClass ←──╴MyMetaClass ┃\n' +
  418. '┃ │ ↑ ┃\n' +
  419. '┃ object ←─────╴type ←╮ ┃\n' +
  420. '┃ │ │ ╰──╯ ┃\n' +
  421. '┃ str ←─────────╯ ┃\n' +
  422. '┗━━━━━━━━━━━━━┷━━━━━━━━━━━━━┛\n';
  423. const DIAGRAM_11_A =
  424. '+-------------+-------------+\n' +
  425. '| Classes | Metaclasses |\n' +
  426. '+-------------+-------------|\n' +
  427. '| MyClass | MyMetaClass |\n';
  428. const DIAGRAM_11_B =
  429. '┏━━━━━━━━━━━━━┯━━━━━━━━━━━━━┓\n' +
  430. '┃ Classes │ Metaclasses ┃\n' +
  431. '┠─────────────┼─────────────┨\n' +
  432. '┃ MyClass │ MyMetaClass ┃\n' +
  433. '┃ ↑ │ ↑ ┃\n' +
  434. '┃ object╶─────→ type ┃\n' +
  435. '┃ ↓ │ ┃\n' +
  436. '┃ str │ ┃\n' +
  437. '┗━━━━━━━━━━━━━┷━━━━━━━━━━━━━┛\n';
  438. const DIAGRAM_12_A =
  439. '+-----------+-----------+------+-----------+\n' +
  440. '| sampwidth | min | zero | max |\n' +
  441. '+-----------+-----------+------+-----------+\n';
  442. const DIAGRAM_12_B =
  443. '┏━━━━━━━━━━━┯━━━━━━━━━━━┯━━━━━━┯━━━━━━━━━━━┓\n' +
  444. '┃ sampwidth │ min │ zero │ max ┃\n' +
  445. '┠───────────┼───────────┼──────┼───────────┨\n' +
  446. '┃ 1 │ 0 │ 128 │ 255 ┃\n' +
  447. '┃ 2 │ -32768 │ 0 │ 32767 ┃\n' +
  448. '┃ 3 │ -8388608 │ 0 │ 8388607 ┃\n' +
  449. '┗━━━━━━━━━━━┷━━━━━━━━━━━┷━━━━━━┷━━━━━━━━━━━┛\n';
  450. const DIAGRAM_13_A =
  451. '| sr.apply(…) | 3 | sum 3 | s 3 |';
  452. const DIAGRAM_13_B =
  453. "┏━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┓\n" +
  454. "┃ │ 'sum' │ ['sum'] │ {'s': 'sum'} ┃\n" +
  455. "┠─────────────────┼─────────────┼─────────────┼───────────────┨\n" +
  456. "┃ sr.apply(…) │ 3 │ sum 3 │ s 3 ┃\n" +
  457. "┃ sr.agg(…) │ │ │ ┃\n" +
  458. "┗━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┛\n" +
  459. "\n" +
  460. "┏━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┓\n" +
  461. "┃ │ 'rank' │ ['rank'] │ {'r': 'rank'} ┃\n" +
  462. "┠─────────────────┼─────────────┼─────────────┼───────────────┨\n" +
  463. "┃ sr.apply(…) │ │ rank │ ┃\n" +
  464. "┃ sr.agg(…) │ x 1 │ x 1 │ r x 1 ┃\n" +
  465. "┃ sr.transform(…) │ y 2 │ y 2 │ y 2 ┃\n" +
  466. "┗━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┛\n";
  467. const DIAGRAM_14_A =
  468. "| | 'rank' | ['rank'] | {'r': 'rank'} |";
  469. const DIAGRAM_15_A =
  470. '+------------------------+---------------+------------+------------+--------------------------+';
  471. const DIAGRAM_15_B =
  472. "┏━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n" +
  473. "┃ │ 'outer' │ 'inner' │ 'left' │ Description ┃\n" +
  474. "┠────────────────────────┼───────────────┼────────────┼────────────┼──────────────────────────┨\n" +
  475. "┃ l.merge(r, on='y', │ x y z │ x y z │ x y z │ Merges on column if 'on' ┃\n" +
  476. "┃ how=…) │ 0 1 2 . │ 3 4 5 │ 1 2 . │ or 'left/right_on' are ┃\n" +
  477. "┃ │ 1 3 4 5 │ │ 3 4 5 │ set, else on shared cols.┃\n" +
  478. "┃ │ 2 . 6 7 │ │ │ Uses 'inner' by default. ┃\n" +
  479. "┠────────────────────────┼───────────────┼────────────┼────────────┼──────────────────────────┨\n" +
  480. "┃ l.join(r, lsuffix='l', │ x yl yr z │ │ x yl yr z │ Merges on row keys. ┃\n" +
  481. "┃ rsuffix='r', │ a 1 2 . . │ x yl yr z │ 1 2 . . │ Uses 'left' by default. ┃\n" +
  482. "┃ how=…) │ b 3 4 4 5 │ 3 4 4 5 │ 3 4 4 5 │ If r is a Series, it is ┃\n" +
  483. "┃ │ c . . 6 7 │ │ │ treated as a column. ┃\n" +
  484. "┠────────────────────────┼───────────────┼────────────┼────────────┼──────────────────────────┨\n" +
  485. "┃ pd.concat([l, r], │ x y z │ y │ │ Adds rows at the bottom. ┃\n" +
  486. "┃ axis=0, │ a 1 2 . │ 2 │ │ Uses 'outer' by default. ┃\n" +
  487. "┃ join=…) │ b 3 4 . │ 4 │ │ A Series is treated as a ┃\n" +
  488. "┃ │ b . 4 5 │ 4 │ │ column. To add a row use ┃\n" +
  489. "┃ │ c . 6 7 │ 6 │ │ pd.concat([l, DF([sr])]).┃\n" +
  490. "┠────────────────────────┼───────────────┼────────────┼────────────┼──────────────────────────┨\n" +
  491. "┃ pd.concat([l, r], │ x y y z │ │ │ Adds columns at the ┃\n" +
  492. "┃ axis=1, │ a 1 2 . . │ x y y z │ │ right end. Uses 'outer' ┃\n" +
  493. "┃ join=…) │ b 3 4 4 5 │ 3 4 4 5 │ │ by default. A Series is ┃\n" +
  494. "┃ │ c . . 6 7 │ │ │ treated as a column. ┃\n" +
  495. "┠────────────────────────┼───────────────┼────────────┼────────────┼──────────────────────────┨\n" +
  496. "┃ l.combine_first(r) │ x y z │ │ │ Adds missing rows and ┃\n" +
  497. "┃ │ a 1 2 . │ │ │ columns. Also updates ┃\n" +
  498. "┃ │ b 3 4 5 │ │ │ items that contain NaN. ┃\n" +
  499. "┃ │ c . 6 7 │ │ │ R must be a DataFrame. ┃\n" +
  500. "┗━━━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n";
  501. const DIAGRAM_16_A =
  502. '| df.apply(…) | | x y | |';
  503. const DIAGRAM_16_B =
  504. "┏━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┓\n" +
  505. "┃ │ 'sum' │ ['sum'] │ {'x': 'sum'} ┃\n" +
  506. "┠─────────────────┼─────────────┼─────────────┼───────────────┨\n" +
  507. "┃ df.apply(…) │ │ x y │ ┃\n" +
  508. "┃ df.agg(…) │ x 4 │ sum 4 6 │ x 4 ┃\n" +
  509. "┃ │ y 6 │ │ ┃\n" +
  510. "┗━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┛\n" +
  511. "\n" +
  512. "┏━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┓\n" +
  513. "┃ │ 'rank' │ ['rank'] │ {'x': 'rank'} ┃\n" +
  514. "┠─────────────────┼─────────────┼─────────────┼───────────────┨\n" +
  515. "┃ df.apply(…) │ x y │ x y │ x ┃\n" +
  516. "┃ df.agg(…) │ a 1 1 │ rank rank │ a 1 ┃\n" +
  517. "┃ df.transform(…) │ b 2 2 │ a 1 1 │ b 2 ┃\n" +
  518. "┃ │ │ b 2 2 │ ┃\n" +
  519. "┗━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┛\n";
  520. const DIAGRAM_17_A =
  521. "| | 'rank' | ['rank'] | {'x': 'rank'} |";
  522. const DIAGRAM_18_A =
  523. '| gb.agg(…) | x y | x y | x y | x |';
  524. const DIAGRAM_18_B =
  525. "┏━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┓\n" +
  526. "┃ │ 'sum' │ 'rank' │ ['rank'] │ {'x': 'rank'} ┃\n" +
  527. "┠─────────────────┼─────────────┼─────────────┼─────────────┼───────────────┨\n" +
  528. "┃ gb.agg(…) │ x y │ x y │ x y │ x ┃\n" +
  529. "┃ │ z │ a 1 1 │ rank rank │ a 1 ┃\n" +
  530. "┃ │ 3 1 2 │ b 1 1 │ a 1 1 │ b 1 ┃\n" +
  531. "┃ │ 6 11 13 │ c 2 2 │ b 1 1 │ c 2 ┃\n" +
  532. "┃ │ │ │ c 2 2 │ ┃\n" +
  533. "┠─────────────────┼─────────────┼─────────────┼─────────────┼───────────────┨\n" +
  534. "┃ gb.transform(…) │ x y │ x y │ │ ┃\n" +
  535. "┃ │ a 1 2 │ a 1 1 │ │ ┃\n" +
  536. "┃ │ b 11 13 │ b 1 1 │ │ ┃\n" +
  537. "┃ │ c 11 13 │ c 2 2 │ │ ┃\n" +
  538. "┗━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┛\n";
  539. const MENU = '<a href="https://raw.githubusercontent.com/gto76/python-cheatsheet/main/README.md">Download text file</a>, <a href="https://transactions.sendowl.com/products/78175486/4422834F/view">Buy PDF</a>, <a href="https://github.com/gto76/python-cheatsheet">Fork me on GitHub</a>, <a href="https://github.com/gto76/python-cheatsheet/wiki/Frequently-Asked-Questions">Check out FAQ</a> or <a href="index.html?theme=dark3">Switch to dark theme</a>.\n';
  540. const DARK_THEME_SCRIPT =
  541. '<script>\n' +
  542. ' // Changes the image and link to theme if URL ends with "index.html?theme=dark". \n' +
  543. ' if (window.location.search.search(/[?&]theme=dark/) !== -1) {\n' +
  544. '\n' +
  545. ' var link_to_theme = document.createElement("a")\n' +
  546. ' link_to_theme.href = "index.html"\n' +
  547. ' link_to_theme.text = "Switch to light theme"\n' +
  548. ' document.getElementsByClassName("banner")[0].firstChild.children[4].replaceWith(link_to_theme)\n' +
  549. '\n' +
  550. ' var img_dark = document.createElement("img");\n' +
  551. ' img_dark.src = "web/image_orig_blue6.png";\n' +
  552. ' img_dark.alt = "Monthy Python";\n' +
  553. ' if ((window.location.search.search(/[?&]theme=dark2/) !== -1) ||\n' +
  554. ' (window.location.search.search(/[?&]theme=dark3/) !== -1)) {\n' +
  555. ' img_dark.style = "width: 910px;";\n' +
  556. ' } else {\n' +
  557. ' img_dark.style = "width: 960px;";\n' +
  558. ' }\n' +
  559. ' document.getElementsByClassName("banner")[1].firstChild.replaceWith(img_dark);\n' +
  560. ' }\n' +
  561. '</script>';
  562. function main() {
  563. const html = getMd();
  564. initDom(html);
  565. modifyPage();
  566. var template = readFile('web/template.html');
  567. template = updateDate(template);
  568. const tokens = template.split('<div id=main_container></div>');
  569. const text = `${tokens[0]} ${document.body.innerHTML} ${tokens[1]}`;
  570. writeToFile('index.html', text);
  571. }
  572. function getMd() {
  573. var readme = readFile('README.md');
  574. var readme = readme.replace("#semaphore-event-barrier", "#semaphoreeventbarrier");
  575. var readme = readme.replace("#semaphore-event-barrier", "#semaphoreeventbarrier");
  576. var readme = readme.replace("#dataframe-plot-encode-decode", "#dataframeplotencodedecode");
  577. const converter = new showdown.Converter();
  578. return converter.makeHtml(readme);
  579. }
  580. function initDom(html) {
  581. const { JSDOM } = jsdom;
  582. const dom = new JSDOM(html);
  583. const $ = (require('jquery'))(dom.window);
  584. global.$ = $;
  585. global.document = dom.window.document;
  586. }
  587. function modifyPage() {
  588. changeMenu();
  589. addDarkThemeScript();
  590. removeOrigToc();
  591. addToc();
  592. insertLinks();
  593. unindentBanner();
  594. updateDiagrams();
  595. highlightCode();
  596. fixPandasDiagram();
  597. removePlotImages();
  598. fixABCSequenceDiv();
  599. fixStructFormatDiv();
  600. }
  601. function changeMenu() {
  602. $('sup').first().html(MENU)
  603. }
  604. function addDarkThemeScript() {
  605. $('#main').before(DARK_THEME_SCRIPT);
  606. }
  607. function removeOrigToc() {
  608. const headerContents = $('#contents');
  609. const contentsList = headerContents.next();
  610. headerContents.remove();
  611. contentsList.remove();
  612. }
  613. function addToc() {
  614. const nodes = $.parseHTML(TOC);
  615. $('#main').before(nodes);
  616. }
  617. function insertLinks() {
  618. $('h2').each(function() {
  619. const aId = $(this).attr('id');
  620. const text = $(this).text();
  621. const line = `<a href="#${aId}" name="${aId}">#</a>${text}`;
  622. $(this).html(line);
  623. });
  624. }
  625. function unindentBanner() {
  626. const montyImg = $('img').first();
  627. montyImg.parent().addClass('banner');
  628. const downloadPraragrapth = $('p').first();
  629. downloadPraragrapth.addClass('banner');
  630. }
  631. function updateDiagrams() {
  632. $(`code:contains(${DIAGRAM_1_A})`).html(DIAGRAM_1_B);
  633. $(`code:contains(${DIAGRAM_2_A})`).html(DIAGRAM_2_B);
  634. $(`code:contains(${DIAGRAM_3_A})`).html(DIAGRAM_3_B);
  635. $(`code:contains(${DIAGRAM_4_A})`).html(DIAGRAM_4_B);
  636. $(`code:contains(${DIAGRAM_5_A})`).parent().remove();
  637. $(`code:contains(${DIAGRAM_6_A})`).html(DIAGRAM_6_B);
  638. $(`code:contains(${DIAGRAM_7_A})`).html(DIAGRAM_7_B);
  639. $(`code:contains(${DIAGRAM_8_A})`).html(DIAGRAM_8_B);
  640. $(`code:contains(${DIAGRAM_9_A})`).html(DIAGRAM_9_B);
  641. $(`code:contains(${DIAGRAM_95_A})`).html(DIAGRAM_95_B);
  642. $(`code:contains(${DIAGRAM_10_A})`).html(DIAGRAM_10_B);
  643. $(`code:contains(${DIAGRAM_11_A})`).html(DIAGRAM_11_B);
  644. $(`code:contains(${DIAGRAM_12_A})`).html(DIAGRAM_12_B).removeClass("text").removeClass("language-text").addClass("python");
  645. $(`code:contains(${DIAGRAM_13_A})`).html(DIAGRAM_13_B).removeClass("text").removeClass("language-text").addClass("python");
  646. $(`code:contains(${DIAGRAM_14_A})`).parent().remove();
  647. $(`code:contains(${DIAGRAM_15_A})`).html(DIAGRAM_15_B).removeClass("text").removeClass("language-text").addClass("python");
  648. $(`code:contains(${DIAGRAM_16_A})`).html(DIAGRAM_16_B).removeClass("text").removeClass("language-text").addClass("python");
  649. $(`code:contains(${DIAGRAM_17_A})`).parent().remove();
  650. $(`code:contains(${DIAGRAM_18_A})`).html(DIAGRAM_18_B).removeClass("text").removeClass("language-text").addClass("python");
  651. }
  652. function highlightCode() {
  653. changeCodeLanguages();
  654. $('code').each(function(index) {
  655. hljs.highlightBlock(this);
  656. });
  657. fixClasses();
  658. fixHighlights();
  659. preventPageBreaks();
  660. fixPageBreaksFile();
  661. fixPageBreaksStruct();
  662. insertPageBreaks();
  663. }
  664. function changeCodeLanguages() {
  665. setApaches(['<D>', '<T>', '<DT>', '<TD>', '<a>', '<n>']);
  666. $('code').not('.python').not('.text').not('.bash').not('.apache').addClass('python');
  667. $('code:contains(<el> = <2d_array>[row_index, column_index])').removeClass().addClass('bash');
  668. $('code:contains(<2d_array> = <2d_array>[row_indexes])').removeClass().addClass('bash');
  669. $('code:contains(<2d_bools> = <2d_array> ><== <el/1d/2d_array>)').removeClass().addClass('bash');
  670. $('code.perl').removeClass().addClass('python');
  671. }
  672. function setApaches(elements) {
  673. for (el of elements) {
  674. $(`code:contains(${el})`).addClass('apache');
  675. }
  676. }
  677. function fixClasses() {
  678. // Changes class="hljs-keyword" to class="hljs-title" of 'class' keyword.
  679. $('.hljs-class').filter(':contains(class \')').find(':first-child').removeClass('hljs-keyword').addClass('hljs-title')
  680. }
  681. function fixHighlights() {
  682. $(`code:contains(@lru_cache(maxsize=None))`).html(LRU_CACHE);
  683. $(`code:contains(@debug(print_result=True))`).html(PARAMETRIZED_DECORATOR);
  684. $(`code:contains(print/str/repr([<el>]))`).html(REPR_USE_CASES);
  685. $(`code:contains((self, a=None):)`).html(CONSTRUCTOR_OVERLOADING);
  686. $(`code:contains(make_dataclass(\'<class_name>\')`).html(DATACLASS);
  687. $(`code:contains(shutil.copy)`).html(SHUTIL_COPY);
  688. $(`code:contains(os.rename)`).html(OS_RENAME);
  689. $(`code:contains(\'<n>s\')`).html(STRUCT_FORMAT);
  690. $(`code:contains(\'<class_name>\', <tuple_of_parents>, <dict_of_class_attributes>)`).html(TYPE);
  691. $(`code:contains(ValueError: malformed node)`).html(EVAL);
  692. $(`code:contains(import asyncio, collections, curses, curses.textpad, enum, random)`).html(COROUTINES);
  693. $(`code:contains(pip3 install tqdm)`).html(PROGRESS_BAR);
  694. $(`code:contains(>>> logging.basicConfig(level=)`).html(LOGGING_EXAMPLE);
  695. $(`code:contains(samples_f = (sin(i *)`).html(AUDIO);
  696. $(`code:contains(collections, dataclasses, enum, io, itertools)`).html(MARIO);
  697. $(`code:contains(pip3 install pyinstaller)`).html(PYINSTALLER);
  698. $(`ul:contains(Only available in)`).html(INDEX);
  699. }
  700. function preventPageBreaks() {
  701. $(':header').each(function(index) {
  702. var el = $(this)
  703. var untilPre = el.nextUntil('pre')
  704. var untilH2 = el.nextUntil('h2')
  705. if ((untilPre.length < untilH2.length) || el.prop('tagName') === 'H1') {
  706. untilPre.add(el).next().add(el).wrapAll("<div></div>");
  707. } else {
  708. untilH2.add(el).wrapAll("<div></div>");
  709. }
  710. });
  711. }
  712. function fixPageBreaksFile() {
  713. const modesDiv = $('#file').parent().parent().parent()
  714. move(modesDiv, 'file')
  715. move(modesDiv, 'exceptions-1')
  716. }
  717. function fixPageBreaksStruct() {
  718. const formatDiv = $('#floatingpointtypes').parent().parent().parent().parent()
  719. move(formatDiv, 'floatingpointtypes')
  720. move(formatDiv, 'integertypesuseacapitalletterforunsignedtypeminimumandstandardsizesareinbrackets')
  721. move(formatDiv, 'forstandardsizesstartformatstringwith')
  722. }
  723. function move(anchor_el, el_id) {
  724. const el = $('#'+el_id).parent()
  725. anchor_el.after(el)
  726. }
  727. function insertPageBreaks() {
  728. insertPageBreakBefore('#decorator')
  729. // insertPageBreakBefore('#print')
  730. }
  731. function insertPageBreakBefore(an_id) {
  732. $('<div class="pagebreak"></div>').insertBefore($(an_id).parent())
  733. }
  734. function fixPandasDiagram() {
  735. const diagram_15 = '┏━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━┓';
  736. $(`code:contains(${diagram_15})`).find(".hljs-keyword:contains(and)").after("and");
  737. $(`code:contains(${diagram_15})`).find(".hljs-keyword:contains(as)").after("as");
  738. $(`code:contains(${diagram_15})`).find(".hljs-keyword:contains(is)").after("is");
  739. $(`code:contains(${diagram_15})`).find(".hljs-keyword:contains(if)").after("if");
  740. $(`code:contains(${diagram_15})`).find(".hljs-keyword:contains(or)").after("or");
  741. $(`code:contains(${diagram_15})`).find(".hljs-keyword:contains(else)").after("else");
  742. $(`code:contains(${diagram_15})`).find(".hljs-keyword").remove();
  743. }
  744. function removePlotImages() {
  745. $('img[alt="Covid Deaths"]').remove();
  746. $('img[alt="Covid Cases"]').remove();
  747. }
  748. function fixABCSequenceDiv() {
  749. $('#abcsequence').parent().insertBefore($('#tableofrequiredandautomaticallyavailablespecialmethods').parent())
  750. }
  751. function fixStructFormatDiv() {
  752. const div = $('#format-2').parent()
  753. $('#format-2').insertBefore(div)
  754. $('#forstandardtypesizesandmanualalignmentpaddingstartformatstringwith').parent().insertBefore(div)
  755. }
  756. function updateDate(template) {
  757. const date = new Date();
  758. const date_str = date.toLocaleString('en-us', {month: 'long', day: 'numeric', year: 'numeric'});
  759. template = template.replace('May 20, 2021', date_str);
  760. template = template.replace('May 20, 2021', date_str);
  761. return template;
  762. }
  763. // UTIL
  764. function readFile(filename) {
  765. try {
  766. return fs.readFileSync(filename, 'utf8');
  767. } catch(e) {
  768. console.error('Error:', e.stack);
  769. }
  770. }
  771. function writeToFile(filename, text) {
  772. try {
  773. return fs.writeFileSync(filename, text, 'utf8');
  774. } catch(e) {
  775. console.error('Error:', e.stack);
  776. }
  777. }
  778. main();