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.

610 lines
44 KiB

4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 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
4 years ago
  1. #!/usr/bin/env node
  2. // Usage: node parse.js
  3. // Script that creates index.html out of web/template.html and README.md.
  4. // It is written in JS because this code used to be executed on the client side.
  5. // To install dependencies run:
  6. // $ npm install -g jsdom jquery showdown highlightjs
  7. // If running on Mac and modules can't be found after installation add:
  8. // export NODE_PATH=/usr/local/lib/node_modules
  9. // to the ~/.bash_profile or ~/.bashrc file and run '$ bash'.
  10. const fs = require('fs');
  11. const jsdom = require('jsdom');
  12. const showdown = require('showdown');
  13. const hljs = require('highlightjs');
  14. const TOC =
  15. '<br>' +
  16. '<h2 id="toc">Contents</h2>\n' +
  17. '<pre><code class="hljs bash"><strong>ToC</strong> = {\n' +
  18. ' <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' +
  19. ' <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' +
  20. ' <strong><span class="hljs-string">\'3. Syntax\'</span></strong>: [<a href="#arguments">Args</a>, <a href="#inline">Inline</a>, <a href="#closure">Closure</a>, <a href="#decorator">Decorator</a>, <a href="#class">Class</a>, <a href="#ducktypes">Duck_Type</a>, <a href="#enum">Enum</a>, <a href="#exceptions">Exception</a>],\n' +
  21. ' <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="#path">Path</a>, <a href="#oscommands">OS_Commands</a>],\n' +
  22. ' <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' +
  23. ' <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="#metaprograming">Metaprograming</a>, <a href="#eval">Eval</a>, <a href="#coroutines">Coroutine</a>],\n' +
  24. ' <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' +
  25. ' <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>, <a href="#pysimplegui">GUI</a>]\n' +
  26. '}\n' +
  27. '</code></pre>\n';
  28. const LRU_CACHE =
  29. '<span class="hljs-keyword">from</span> functools <span class="hljs-keyword">import</span> lru_cache\n' +
  30. '\n' +
  31. '<span class="hljs-meta">@lru_cache(maxsize=None)</span>\n' +
  32. '<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">fib</span><span class="hljs-params">(n)</span>:</span>\n' +
  33. ' <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';
  34. const CONSTRUCTOR_OVERLOADING =
  35. '<span class="hljs-class"><span class="hljs-keyword">class</span> &lt;<span class="hljs-title">name</span>&gt;:</span>\n' +
  36. ' <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' +
  37. ' self.a = a\n';
  38. const DATACLASS =
  39. '<span class="hljs-keyword">from</span> dataclasses <span class="hljs-keyword">import</span> make_dataclass\n' +
  40. '&lt;class&gt; = make_dataclass(<span class="hljs-string">\'&lt;class_name&gt;\'</span>, &lt;coll_of_attribute_names&gt;)\n' +
  41. '&lt;class&gt; = make_dataclass(<span class="hljs-string">\'&lt;class_name&gt;\'</span>, &lt;coll_of_tuples&gt;)\n' +
  42. '&lt;tuple&gt; = (<span class="hljs-string">\'&lt;attr_name&gt;\'</span>, &lt;type&gt; [, &lt;default_value&gt;])';
  43. const SHUTIL_COPY =
  44. 'shutil.copy(from, to) <span class="hljs-comment"># Copies the file. \'to\' can exist or be a dir.</span>\n' +
  45. 'shutil.copytree(from, to) <span class="hljs-comment"># Copies the directory. \'to\' must not exist.</span>\n';
  46. const OS_RENAME =
  47. 'os.rename(from, to) <span class="hljs-comment"># Renames/moves the file or directory.</span>\n' +
  48. 'os.replace(from, to) <span class="hljs-comment"># Same, but overwrites \'to\' if it exists.</span>\n';
  49. const TYPE =
  50. '&lt;class&gt; = type(<span class="hljs-string">\'&lt;class_name&gt;\'</span>, &lt;parents_tuple&gt;, &lt;attributes_dict&gt;)';
  51. const EVAL =
  52. '<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> ast <span class="hljs-keyword">import</span> literal_eval\n' +
  53. '<span class="hljs-meta">&gt;&gt;&gt; </span>literal_eval(<span class="hljs-string">\'1 + 2\'</span>)\n' +
  54. '<span class="hljs-number">3</span>\n' +
  55. '<span class="hljs-meta">&gt;&gt;&gt; </span>literal_eval(<span class="hljs-string">\'[1, 2, 3]\'</span>)\n' +
  56. '[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]\n' +
  57. '<span class="hljs-meta">&gt;&gt;&gt; </span>literal_eval(<span class="hljs-string">\'abs(1)\'</span>)\n' +
  58. 'ValueError: malformed node or string\n';
  59. const PROGRESS_BAR =
  60. '<span class="hljs-comment"># $ pip3 install tqdm</span>\n' +
  61. '<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> tqdm <span class="hljs-keyword">import</span> tqdm\n' +
  62. '<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> time <span class="hljs-keyword">import</span> sleep\n' +
  63. '<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' +
  64. '<span class="hljs-meta">... </span> sleep(<span class="hljs-number">1</span>)\n' +
  65. 'Processing: 100%|████████████████████| 3/3 [00:03&lt;00:00, 1.00s/it]\n';
  66. const NUMPY_1 =
  67. '&lt;el&gt; = &lt;2d_array&gt;[<span class="hljs-number">0</span>, <span class="hljs-number">0</span>] <span class="hljs-comment"># First element.</span>\n' +
  68. '&lt;1d_view&gt; = &lt;2d_array&gt;[<span class="hljs-number">0</span>] <span class="hljs-comment"># First row.</span>\n' +
  69. '&lt;1d_view&gt; = &lt;2d_array&gt;[:, <span class="hljs-number">0</span>] <span class="hljs-comment"># First column. Also [..., 0].</span>\n' +
  70. '&lt;3d_view&gt; = &lt;2d_array&gt;[<span class="hljs-keyword">None</span>, :, :] <span class="hljs-comment"># Expanded by dimension of size 1.</span>\n';
  71. const NUMPY_2 =
  72. '&lt;1d_array&gt; = &lt;2d_array&gt;[&lt;1d_row_indexes&gt;, &lt;1d_column_indexes&gt;]\n' +
  73. '&lt;2d_array&gt; = &lt;2d_array&gt;[&lt;2d_row_indexes&gt;, &lt;2d_column_indexes&gt;]\n';
  74. const NUMPY_3 =
  75. '&lt;2d_bools&gt; = &lt;2d_array&gt; &gt; <span class="hljs-number">0</span>\n' +
  76. '&lt;1d_array&gt; = &lt;2d_array&gt;[&lt;2d_bools&gt;]\n';
  77. const PYINSTALLER =
  78. '$ pip3 install pyinstaller\n' +
  79. '$ pyinstaller script.py <span class="hljs-comment"># Compiles into \'./dist/script\' directory.</span>\n' +
  80. '$ pyinstaller script.py --onefile <span class="hljs-comment"># Compiles into \'./dist/script\' console app.</span>\n' +
  81. '$ pyinstaller script.py --windowed <span class="hljs-comment"># Compiles into \'./dist/script\' windowed app.</span>\n' +
  82. '$ pyinstaller script.py --add-data \'&lt;path&gt;:.\' <span class="hljs-comment"># Adds file to the root of the executable.</span>\n';
  83. const INDEX =
  84. '<li><strong>Only available in <a href="https://transactions.sendowl.com/products/78175486/4422834F/view">PDF</a>.</strong></li>\n' +
  85. '<li><strong>Ctrl+F / ⌘F is usually sufficient.</strong></li>\n' +
  86. '<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';
  87. const DIAGRAM_1_A =
  88. '+------------------+------------+------------+------------+\n' +
  89. '| | Sequence | Collection | Iterable |\n' +
  90. '+------------------+------------+------------+------------+\n';
  91. const DIAGRAM_1_B =
  92. '┏━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┓\n' +
  93. '┃ │ Sequence │ Collection │ Iterable ┃\n' +
  94. '┠──────────────────┼────────────┼────────────┼────────────┨\n' +
  95. '┃ list, range, str │ ✓ │ ✓ │ ✓ ┃\n' +
  96. '┃ dict, set │ │ ✓ │ ✓ ┃\n' +
  97. '┃ iter │ │ │ ✓ ┃\n' +
  98. '┗━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┛\n';
  99. const DIAGRAM_2_A =
  100. '+--------------------+----------+----------+----------+----------+----------+\n' +
  101. '| | Integral | Rational | Real | Complex | Number |\n' +
  102. '+--------------------+----------+----------+----------+----------+----------+\n';
  103. const DIAGRAM_2_B =
  104. '┏━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┓\n' +
  105. '┃ │ Integral │ Rational │ Real │ Complex │ Number ┃\n' +
  106. '┠────────────────────┼──────────┼──────────┼──────────┼──────────┼──────────┨\n' +
  107. '┃ int │ ✓ │ ✓ │ ✓ │ ✓ │ ✓ ┃\n' +
  108. '┃ fractions.Fraction │ │ ✓ │ ✓ │ ✓ │ ✓ ┃\n' +
  109. '┃ float │ │ │ ✓ │ ✓ │ ✓ ┃\n' +
  110. '┃ complex │ │ │ │ ✓ │ ✓ ┃\n' +
  111. '┃ decimal.Decimal │ │ │ │ │ ✓ ┃\n' +
  112. '┗━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┛\n';
  113. const DIAGRAM_3_A =
  114. '+---------------+----------+----------+----------+----------+----------+\n';
  115. const DIAGRAM_3_B =
  116. '┏━━━━━━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┓\n' +
  117. '┃ │ [ !#$%…] │ [a-zA-Z] │ [¼½¾] │ [²³¹] │ [0-9] ┃\n' +
  118. '┠───────────────┼──────────┼──────────┼──────────┼──────────┼──────────┨\n' +
  119. '┃ isprintable() │ ✓ │ ✓ │ ✓ │ ✓ │ ✓ ┃\n' +
  120. '┃ isalnum() │ │ ✓ │ ✓ │ ✓ │ ✓ ┃\n' +
  121. '┃ isnumeric() │ │ │ ✓ │ ✓ │ ✓ ┃\n' +
  122. '┃ isdigit() │ │ │ │ ✓ │ ✓ ┃\n' +
  123. '┃ isdecimal() │ │ │ │ │ ✓ ┃\n' +
  124. '┗━━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┛\n';
  125. const DIAGRAM_4_A =
  126. "+---------------+-----------------+-----------------+-----------------+-----------------+\n" +
  127. "| | {<float>} | {<float>:f} | {<float>:e} | {<float>:%} |\n" +
  128. "+---------------+-----------------+-----------------+-----------------+-----------------+\n";
  129. const DIAGRAM_4_B =
  130. "┏━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━┓\n" +
  131. "┃ │ {&lt;float&gt;} │ {&lt;float&gt;:f} │ {&lt;float&gt;:e} │ {&lt;float&gt;:%} ┃\n" +
  132. "┠───────────────┼─────────────────┼─────────────────┼─────────────────┼─────────────────┨\n" +
  133. "┃ 0.000056789 │ '5.6789e-05' │ '0.000057' │ '5.678900e-05' │ '0.005679%' ┃\n" +
  134. "┃ 0.00056789 │ '0.00056789' │ '0.000568' │ '5.678900e-04' │ '0.056789%' ┃\n" +
  135. "┃ 0.0056789 │ '0.0056789' │ '0.005679' │ '5.678900e-03' │ '0.567890%' ┃\n" +
  136. "┃ 0.056789 │ '0.056789' │ '0.056789' │ '5.678900e-02' │ '5.678900%' ┃\n" +
  137. "┃ 0.56789 │ '0.56789' │ '0.567890' │ '5.678900e-01' │ '56.789000%' ┃\n" +
  138. "┃ 5.6789 │ '5.6789' │ '5.678900' │ '5.678900e+00' │ '567.890000%' ┃\n" +
  139. "┃ 56.789 │ '56.789' │ '56.789000' │ '5.678900e+01' │ '5678.900000%' ┃\n" +
  140. "┃ 567.89 │ '567.89' │ '567.890000' │ '5.678900e+02' │ '56789.000000%' ┃\n" +
  141. "┗━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━┛\n";
  142. const DIAGRAM_5_A =
  143. "+---------------+-----------------+-----------------+-----------------+-----------------+\n" +
  144. "| | {<float>:.2} | {<float>:.2f} | {<float>:.2e} | {<float>:.2%} |\n" +
  145. "+---------------+-----------------+-----------------+-----------------+-----------------+\n";
  146. const DIAGRAM_5_B =
  147. "┏━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━┓\n" +
  148. "┃ │ {&lt;float&gt;:.2} │ {&lt;float&gt;:.2f} │ {&lt;float&gt;:.2e} │ {&lt;float&gt;:.2%} ┃\n" +
  149. "┠───────────────┼─────────────────┼─────────────────┼─────────────────┼─────────────────┨\n" +
  150. "┃ 0.000056789 │ '5.7e-05' │ '0.00' │ '5.68e-05' │ '0.01%' ┃\n" +
  151. "┃ 0.00056789 │ '0.00057' │ '0.00' │ '5.68e-04' │ '0.06%' ┃\n" +
  152. "┃ 0.0056789 │ '0.0057' │ '0.01' │ '5.68e-03' │ '0.57%' ┃\n" +
  153. "┃ 0.056789 │ '0.057' │ '0.06' │ '5.68e-02' │ '5.68%' ┃\n" +
  154. "┃ 0.56789 │ '0.57' │ '0.57' │ '5.68e-01' │ '56.79%' ┃\n" +
  155. "┃ 5.6789 │ '5.7' │ '5.68' │ '5.68e+00' │ '567.89%' ┃\n" +
  156. "┃ 56.789 │ '5.7e+01' │ '56.79' │ '5.68e+01' │ '5678.90%' ┃\n" +
  157. "┃ 567.89 │ '5.7e+02' │ '567.89' │ '5.68e+02' │ '56789.00%' ┃\n" +
  158. "┗━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━┛\n";
  159. const DIAGRAM_6_A =
  160. '+------------+------------+------------+------------+--------------+\n' +
  161. '| | Iterable | Collection | Sequence | abc.Sequence |\n' +
  162. '+------------+------------+------------+------------+--------------+\n';
  163. const DIAGRAM_6_B =
  164. '┏━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━━━┓\n' +
  165. '┃ │ Iterable │ Collection │ Sequence │ abc.Sequence ┃\n' +
  166. '┠────────────┼────────────┼────────────┼────────────┼──────────────┨\n' +
  167. '┃ iter() │ ! │ ! │ ✓ │ ✓ ┃\n' +
  168. '┃ contains() │ ✓ │ ✓ │ ✓ │ ✓ ┃\n' +
  169. '┃ len() │ │ ! │ ! │ ! ┃\n' +
  170. '┃ getitem() │ │ │ ! │ ! ┃\n' +
  171. '┃ reversed() │ │ │ ✓ │ ✓ ┃\n' +
  172. '┃ index() │ │ │ │ ✓ ┃\n' +
  173. '┃ count() │ │ │ │ ✓ ┃\n' +
  174. '┗━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━━━┛\n';
  175. const DIAGRAM_7_A =
  176. 'BaseException\n' +
  177. ' +-- SystemExit';
  178. const DIAGRAM_7_B =
  179. "BaseException\n" +
  180. " ├── SystemExit <span class='hljs-comment'># Raised by the sys.exit() function.</span>\n" +
  181. " ├── KeyboardInterrupt <span class='hljs-comment'># Raised when the user hits the interrupt key (ctrl-c).</span>\n" +
  182. " └── Exception <span class='hljs-comment'># User-defined exceptions should be derived from this class.</span>\n" +
  183. " ├── ArithmeticError <span class='hljs-comment'># Base class for arithmetic errors.</span>\n" +
  184. " │ └── ZeroDivisionError <span class='hljs-comment'># Raised when dividing by zero.</span>\n" +
  185. " ├── AttributeError <span class='hljs-comment'># Raised when an attribute is missing.</span>\n" +
  186. " ├── EOFError <span class='hljs-comment'># Raised by input() when it hits end-of-file condition.</span>\n" +
  187. " ├── LookupError <span class='hljs-comment'># Raised when a look-up on a collection fails.</span>\n" +
  188. " │ ├── IndexError <span class='hljs-comment'># Raised when a sequence index is out of range.</span>\n" +
  189. " │ └── KeyError <span class='hljs-comment'># Raised when a dictionary key or set element is not found.</span>\n" +
  190. " ├── NameError <span class='hljs-comment'># Raised when a variable name is not found.</span>\n" +
  191. " ├── OSError <span class='hljs-comment'># Errors such as “file not found” or “disk full” (see Open).</span>\n" +
  192. " │ └── FileNotFoundError <span class='hljs-comment'># When a file or directory is requested but doesn't exist.</span>\n" +
  193. " ├── RuntimeError <span class='hljs-comment'># Raised by errors that don't fall in other categories.</span>\n" +
  194. " │ └── RecursionError <span class='hljs-comment'># Raised when the maximum recursion depth is exceeded.</span>\n" +
  195. " ├── StopIteration <span class='hljs-comment'># Raised by next() when run on an empty iterator.</span>\n" +
  196. " ├── TypeError <span class='hljs-comment'># Raised when an argument is of wrong type.</span>\n" +
  197. " └── ValueError <span class='hljs-comment'># When an argument is of right type but inappropriate value.</span>\n" +
  198. " └── UnicodeError <span class='hljs-comment'># Raised when encoding/decoding strings to/from bytes fails.</span>\n";
  199. const DIAGRAM_8_A =
  200. '+-----------+------------+------------+------------+\n' +
  201. '| | list | dict | set |\n' +
  202. '+-----------+------------+------------+------------+\n';
  203. const DIAGRAM_8_B =
  204. '┏━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┓\n' +
  205. '┃ │ list │ dict │ set ┃\n' +
  206. '┠───────────┼────────────┼────────────┼────────────┨\n' +
  207. '┃ getitem() │ IndexError │ KeyError │ ┃\n' +
  208. '┃ pop() │ IndexError │ KeyError │ KeyError ┃\n' +
  209. '┃ remove() │ ValueError │ │ KeyError ┃\n' +
  210. '┃ index() │ ValueError │ │ ┃\n' +
  211. '┗━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┛\n';
  212. const DIAGRAM_9_A =
  213. '+------------------+--------------+--------------+--------------+\n' +
  214. '| | excel | excel-tab | unix |\n' +
  215. '+------------------+--------------+--------------+--------------+\n';
  216. const DIAGRAM_9_B =
  217. "┏━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━┓\n" +
  218. "┃ │ excel │ excel-tab │ unix ┃\n" +
  219. "┠──────────────────┼──────────────┼──────────────┼──────────────┨\n" +
  220. "┃ delimiter │ ',' │ '\\t' │ ',' ┃\n" +
  221. "┃ quotechar │ '\"' │ '\"' │ '\"' ┃\n" +
  222. "┃ doublequote │ True │ True │ True ┃\n" +
  223. "┃ skipinitialspace │ False │ False │ False ┃\n" +
  224. "┃ lineterminator │ '\\r\\n' │ '\\r\\n' │ '\\n' ┃\n" +
  225. "┃ quoting │ 0 │ 0 │ 1 ┃\n" +
  226. "┃ escapechar │ None │ None │ None ┃\n" +
  227. "┗━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━┛\n";
  228. const DIAGRAM_10_A =
  229. '+-------------+-------------+\n' +
  230. '| Classes | Metaclasses |\n' +
  231. '+-------------+-------------|\n' +
  232. '| MyClass --> MyMetaClass |\n';
  233. const DIAGRAM_10_B =
  234. '┏━━━━━━━━━━━━━┯━━━━━━━━━━━━━┓\n' +
  235. '┃ Classes │ Metaclasses ┃\n' +
  236. '┠─────────────┼─────────────┨\n' +
  237. '┃ MyClass ──→ MyMetaClass ┃\n' +
  238. '┃ │ ↓ ┃\n' +
  239. '┃ object ─────→ type ←╮ ┃\n' +
  240. '┃ │ ↑ ╰──╯ ┃\n' +
  241. '┃ str ──────────╯ ┃\n' +
  242. '┗━━━━━━━━━━━━━┷━━━━━━━━━━━━━┛\n';
  243. const DIAGRAM_11_A =
  244. '+-------------+-------------+\n' +
  245. '| Classes | Metaclasses |\n' +
  246. '+-------------+-------------|\n' +
  247. '| MyClass | MyMetaClass |\n';
  248. const DIAGRAM_11_B =
  249. '┏━━━━━━━━━━━━━┯━━━━━━━━━━━━━┓\n' +
  250. '┃ Classes │ Metaclasses ┃\n' +
  251. '┠─────────────┼─────────────┨\n' +
  252. '┃ MyClass │ MyMetaClass ┃\n' +
  253. '┃ ↓ │ ↓ ┃\n' +
  254. '┃ object ←───── type ┃\n' +
  255. '┃ ↑ │ ┃\n' +
  256. '┃ str │ ┃\n' +
  257. '┗━━━━━━━━━━━━━┷━━━━━━━━━━━━━┛\n';
  258. const DIAGRAM_12_A =
  259. '+-----------+-------------+------+-------------+\n' +
  260. '| sampwidth | min | zero | max |\n' +
  261. '+-----------+-------------+------+-------------+\n';
  262. const DIAGRAM_12_B =
  263. '┏━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━┯━━━━━━━━━━━━━┓\n' +
  264. '┃ sampwidth │ min │ zero │ max ┃\n' +
  265. '┠───────────┼─────────────┼──────┼─────────────┨\n' +
  266. '┃ 1 │ 0 │ 128 │ 255 ┃\n' +
  267. '┃ 2 │ -32768 │ 0 │ 32767 ┃\n' +
  268. '┃ 3 │ -8388608 │ 0 │ 8388607 ┃\n' +
  269. '┃ 4 │ -2147483648 │ 0 │ 2147483647 ┃\n' +
  270. '┗━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━┷━━━━━━━━━━━━━┛\n';
  271. const DIAGRAM_13_A =
  272. '| sr.apply(…) | 3 | sum 3 | s 3 |';
  273. const DIAGRAM_13_B =
  274. "┏━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┓\n" +
  275. "┃ │ 'sum' │ ['sum'] │ {'s': 'sum'} ┃\n" +
  276. "┠─────────────┼─────────────┼─────────────┼───────────────┨\n" +
  277. "┃ sr.apply(…) │ 3 │ sum 3 │ s 3 ┃\n" +
  278. "┃ sr.agg(…) │ │ │ ┃\n" +
  279. "┗━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┛\n";
  280. const DIAGRAM_14_A =
  281. '| sr.apply(…) | | rank | |';
  282. const DIAGRAM_14_B =
  283. "┏━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┓\n" +
  284. "┃ │ 'rank' │ ['rank'] │ {'r': 'rank'} ┃\n" +
  285. "┠─────────────┼─────────────┼─────────────┼───────────────┨\n" +
  286. "┃ sr.apply(…) │ │ rank │ ┃\n" +
  287. "┃ sr.agg(…) │ x 1 │ x 1 │ r x 1 ┃\n" +
  288. "┃ sr.trans(…) │ y 2 │ y 2 │ y 2 ┃\n" +
  289. "┗━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┛\n";
  290. const DIAGRAM_15_A =
  291. '+------------------------+---------------+------------+------------+--------------------------+';
  292. const DIAGRAM_15_B =
  293. "┏━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n" +
  294. "┃ │ 'outer' │ 'inner' │ 'left' │ Description ┃\n" +
  295. "┠────────────────────────┼───────────────┼────────────┼────────────┼──────────────────────────┨\n" +
  296. "┃ l.merge(r, on='y', │ x y z │ x y z │ x y z │ Joins/merges on column. ┃\n" +
  297. "┃ how=…) │ 0 1 2 . │ 3 4 5 │ 1 2 . │ Also accepts left_on and ┃\n" +
  298. "┃ │ 1 3 4 5 │ │ 3 4 5 │ right_on parameters. ┃\n" +
  299. "┃ │ 2 . 6 7 │ │ │ Uses 'inner' by default. ┃\n" +
  300. "┠────────────────────────┼───────────────┼────────────┼────────────┼──────────────────────────┨\n" +
  301. "┃ l.join(r, lsuffix='l', │ x yl yr z │ │ x yl yr z │ Joins/merges on row keys.┃\n" +
  302. "┃ rsuffix='r', │ a 1 2 . . │ x yl yr z │ 1 2 . . │ Uses 'left' by default. ┃\n" +
  303. "┃ how=…) │ b 3 4 4 5 │ 3 4 4 5 │ 3 4 4 5 │ If r is a series, it is ┃\n" +
  304. "┃ │ c . . 6 7 │ │ │ first converted to DF. ┃\n" +
  305. "┠────────────────────────┼───────────────┼────────────┼────────────┼──────────────────────────┨\n" +
  306. "┃ pd.concat([l, r], │ x y z │ y │ │ Adds rows at the bottom. ┃\n" +
  307. "┃ axis=0, │ a 1 2 . │ 2 │ │ Uses 'outer' by default. ┃\n" +
  308. "┃ join=…) │ b 3 4 . │ 4 │ │ By default works the ┃\n" +
  309. "┃ │ b . 4 5 │ 4 │ │ same as `l.append(r)`. ┃\n" +
  310. "┃ │ c . 6 7 │ 6 │ │ ┃\n" +
  311. "┠────────────────────────┼───────────────┼────────────┼────────────┼──────────────────────────┨\n" +
  312. "┃ pd.concat([l, r], │ x y y z │ │ │ Adds columns at the ┃\n" +
  313. "┃ axis=1, │ a 1 2 . . │ x y y z │ │ right end. ┃\n" +
  314. "┃ join=…) │ b 3 4 4 5 │ 3 4 4 5 │ │ Uses 'outer' by default. ┃\n" +
  315. "┃ │ c . . 6 7 │ │ │ ┃\n" +
  316. "┠────────────────────────┼───────────────┼────────────┼────────────┼──────────────────────────┨\n" +
  317. "┃ l.combine_first(r) │ x y z │ │ │ Adds missing rows and ┃\n" +
  318. "┃ │ a 1 2 . │ │ │ columns. ┃\n" +
  319. "┃ │ b 3 4 5 │ │ │ ┃\n" +
  320. "┃ │ c . 6 7 │ │ │ ┃\n" +
  321. "┗━━━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n";
  322. const DIAGRAM_16_A =
  323. '| df.apply(…) | | x y | |';
  324. const DIAGRAM_16_B =
  325. "┏━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┓\n" +
  326. "┃ │ 'sum' │ ['sum'] │ {'x': 'sum'} ┃\n" +
  327. "┠─────────────┼─────────────┼─────────────┼───────────────┨\n" +
  328. "┃ df.apply(…) │ │ x y │ ┃\n" +
  329. "┃ df.agg(…) │ x 4 │ sum 4 6 │ x 4 ┃\n" +
  330. "┃ │ y 6 │ │ ┃\n" +
  331. "┗━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┛\n";
  332. const DIAGRAM_17_A =
  333. '| df.apply(…) | x y | x y | x |';
  334. const DIAGRAM_17_B =
  335. "┏━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┓\n" +
  336. "┃ │ 'rank' │ ['rank'] │ {'x': 'rank'} ┃\n" +
  337. "┠─────────────┼─────────────┼─────────────┼───────────────┨\n" +
  338. "┃ df.apply(…) │ x y │ x y │ x ┃\n" +
  339. "┃ df.agg(…) │ a 1 1 │ rank rank │ a 1 ┃\n" +
  340. "┃ df.trans(…) │ b 2 2 │ a 1 1 │ b 2 ┃\n" +
  341. "┃ │ │ b 2 2 │ ┃\n" +
  342. "┗━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┛\n";
  343. const DIAGRAM_18_A =
  344. '| gb.agg(…) | x y | x y | x y | x |';
  345. const DIAGRAM_18_B =
  346. "┏━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┓\n" +
  347. "┃ │ 'sum' │ 'rank' │ ['rank'] │ {'x': 'rank'} ┃\n" +
  348. "┠─────────────┼─────────────┼─────────────┼─────────────┼───────────────┨\n" +
  349. "┃ gb.agg(…) │ x y │ x y │ x y │ x ┃\n" +
  350. "┃ │ z │ a 1 1 │ rank rank │ a 1 ┃\n" +
  351. "┃ │ 3 1 2 │ b 1 1 │ a 1 1 │ b 1 ┃\n" +
  352. "┃ │ 6 11 13 │ c 2 2 │ b 1 1 │ c 2 ┃\n" +
  353. "┃ │ │ │ c 2 2 │ ┃\n" +
  354. "┠─────────────┼─────────────┼─────────────┼─────────────┼───────────────┨\n" +
  355. "┃ gb.trans(…) │ x y │ x y │ │ ┃\n" +
  356. "┃ │ a 1 2 │ a 1 1 │ │ ┃\n" +
  357. "┃ │ b 11 13 │ b 1 1 │ │ ┃\n" +
  358. "┃ │ c 11 13 │ c 1 1 │ │ ┃\n" +
  359. "┗━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┛\n";
  360. function main() {
  361. const html = getMd();
  362. initDom(html);
  363. modifyPage();
  364. const template = readFile('web/template.html');
  365. const tokens = template.split('<div id=main_container></div>');
  366. const text = `${tokens[0]} ${document.body.innerHTML} ${tokens[1]}`;
  367. writeToFile('index.html', text);
  368. }
  369. function getMd() {
  370. var readme = readFile('README.md');
  371. var readme = readme.replace("#semaphore-event-barrier", "#semaphoreeventbarrier");
  372. const converter = new showdown.Converter();
  373. return converter.makeHtml(readme);
  374. }
  375. function initDom(html) {
  376. const { JSDOM } = jsdom;
  377. const dom = new JSDOM(html);
  378. const $ = (require('jquery'))(dom.window);
  379. global.$ = $;
  380. global.document = dom.window.document;
  381. }
  382. function modifyPage() {
  383. removeOrigToc();
  384. addToc();
  385. insertLinks();
  386. unindentBanner();
  387. updateDiagrams();
  388. highlightCode();
  389. fixPandasDiagram();
  390. removePlotImages();
  391. }
  392. function removeOrigToc() {
  393. const headerContents = $('#contents');
  394. const contentsList = headerContents.next();
  395. headerContents.remove();
  396. contentsList.remove();
  397. }
  398. function addToc() {
  399. const nodes = $.parseHTML(TOC);
  400. $('#main').before(nodes);
  401. }
  402. function insertLinks() {
  403. $('h2').each(function() {
  404. const aId = $(this).attr('id');
  405. const text = $(this).text();
  406. const line = `<a href="#${aId}" name="${aId}">#</a>${text}`;
  407. $(this).html(line);
  408. });
  409. }
  410. function unindentBanner() {
  411. const montyImg = $('img').first();
  412. montyImg.parent().addClass('banner');
  413. const downloadPraragrapth = $('p').first();
  414. downloadPraragrapth.addClass('banner');
  415. }
  416. function updateDiagrams() {
  417. $(`code:contains(${DIAGRAM_1_A})`).html(DIAGRAM_1_B);
  418. $(`code:contains(${DIAGRAM_2_A})`).html(DIAGRAM_2_B);
  419. $(`code:contains(${DIAGRAM_3_A})`).html(DIAGRAM_3_B);
  420. $(`code:contains(${DIAGRAM_4_A})`).html(DIAGRAM_4_B);
  421. $(`code:contains(${DIAGRAM_5_A})`).html(DIAGRAM_5_B);
  422. $(`code:contains(${DIAGRAM_6_A})`).html(DIAGRAM_6_B);
  423. $(`code:contains(${DIAGRAM_7_A})`).html(DIAGRAM_7_B);
  424. $(`code:contains(${DIAGRAM_8_A})`).html(DIAGRAM_8_B);
  425. $(`code:contains(${DIAGRAM_9_A})`).html(DIAGRAM_9_B);
  426. $(`code:contains(${DIAGRAM_10_A})`).html(DIAGRAM_10_B);
  427. $(`code:contains(${DIAGRAM_11_A})`).html(DIAGRAM_11_B);
  428. $(`code:contains(${DIAGRAM_12_A})`).html(DIAGRAM_12_B).removeClass("text").removeClass("language-text").addClass("python");
  429. $(`code:contains(${DIAGRAM_13_A})`).html(DIAGRAM_13_B).removeClass("text").removeClass("language-text").addClass("python");
  430. $(`code:contains(${DIAGRAM_14_A})`).html(DIAGRAM_14_B).removeClass("text").removeClass("language-text").addClass("python");
  431. $(`code:contains(${DIAGRAM_15_A})`).html(DIAGRAM_15_B).removeClass("text").removeClass("language-text").addClass("python");
  432. $(`code:contains(${DIAGRAM_16_A})`).html(DIAGRAM_16_B).removeClass("text").removeClass("language-text").addClass("python");
  433. $(`code:contains(${DIAGRAM_17_A})`).html(DIAGRAM_17_B).removeClass("text").removeClass("language-text").addClass("python");
  434. $(`code:contains(${DIAGRAM_18_A})`).html(DIAGRAM_18_B).removeClass("text").removeClass("language-text").addClass("python");
  435. }
  436. function highlightCode() {
  437. setApaches(['<D>', '<T>', '<DT>', '<TD>', '<a>', '<n>']);
  438. $('code').not('.python').not('.text').not('.bash').not('.apache').addClass('python');
  439. $('code').each(function(index) {
  440. hljs.highlightBlock(this);
  441. });
  442. fixClasses();
  443. fixHighlights();
  444. preventPageBreaks();
  445. fixPageBreaksFile();
  446. fixPageBreaksStruct();
  447. insertPageBreaks();
  448. }
  449. function setApaches(elements) {
  450. for (el of elements) {
  451. $(`code:contains(${el})`).addClass('apache');
  452. }
  453. }
  454. function fixClasses() {
  455. // Changes class="hljs-keyword" to class="hljs-title" of 'class' keyword.
  456. $('.hljs-class').filter(':contains(class \')').find(':first-child').removeClass('hljs-keyword').addClass('hljs-title')
  457. }
  458. function fixHighlights() {
  459. $(`code:contains(@lru_cache(maxsize=None))`).html(LRU_CACHE);
  460. $(`code:contains((self, a=None):)`).html(CONSTRUCTOR_OVERLOADING);
  461. $(`code:contains(make_dataclass(\'<class_name>\')`).html(DATACLASS);
  462. $(`code:contains(shutil.copy)`).html(SHUTIL_COPY);
  463. $(`code:contains(os.rename)`).html(OS_RENAME);
  464. $(`code:contains(\'<class_name>\', <parents_tuple>, <attributes_dict>)`).html(TYPE);
  465. $(`code:contains(ValueError: malformed node)`).html(EVAL);
  466. $(`code:contains(pip3 install tqdm)`).html(PROGRESS_BAR);
  467. $(`code:contains(<el> = <2d_array>[0, 0])`).html(NUMPY_1).removeClass().addClass("python language-python hljs");
  468. $(`code:contains(<1d_array> = <2d_array>[<1d_row_indexes>)`).html(NUMPY_2).removeClass().addClass("python language-python hljs");
  469. $(`code:contains(<2d_bools> = <2d_array> > 0)`).html(NUMPY_3).removeClass().addClass("python language-python hljs");
  470. $(`code:contains(pip3 install pyinstaller)`).html(PYINSTALLER);
  471. $(`ul:contains(Only available in)`).html(INDEX);
  472. }
  473. function preventPageBreaks() {
  474. $(':header').each(function(index) {
  475. var el = $(this)
  476. var untilPre = el.nextUntil('pre')
  477. var untilH2 = el.nextUntil('h2')
  478. if ((untilPre.length < untilH2.length) || el.prop('tagName') === 'H1') {
  479. untilPre.add(el).next().add(el).wrapAll("<div></div>");
  480. } else {
  481. untilH2.add(el).wrapAll("<div></div>");
  482. }
  483. });
  484. }
  485. function fixPageBreaksFile() {
  486. const modesDiv = $('#file').parent().parent().parent()
  487. move(modesDiv, 'file')
  488. move(modesDiv, 'exceptions-1')
  489. }
  490. function fixPageBreaksStruct() {
  491. const formatDiv = $('#floatingpointtypes').parent().parent().parent().parent()
  492. move(formatDiv, 'floatingpointtypes')
  493. move(formatDiv, 'integertypesuseacapitalletterforunsignedtypeminimumandstandardsizesareinbrackets')
  494. move(formatDiv, 'forstandardsizesstartformatstringwith')
  495. }
  496. function move(anchor_el, el_id) {
  497. const el = $('#'+el_id).parent()
  498. anchor_el.after(el)
  499. }
  500. function insertPageBreaks() {
  501. insertPageBreakBefore('#print')
  502. }
  503. function insertPageBreakBefore(an_id) {
  504. $('<div class="pagebreak"></div>').insertBefore($(an_id).parent())
  505. }
  506. function fixPandasDiagram() {
  507. const diagram_15 = '┏━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━┓';
  508. $(`code:contains(${diagram_15})`).find(".hljs-keyword:contains(and)").after("and");
  509. $(`code:contains(${diagram_15})`).find(".hljs-keyword:contains(as)").after("as");
  510. $(`code:contains(${diagram_15})`).find(".hljs-keyword:contains(is)").after("is");
  511. $(`code:contains(${diagram_15})`).find(".hljs-keyword").remove();
  512. }
  513. function removePlotImages() {
  514. $('img[alt="Covid Deaths"]').remove();
  515. $('img[alt="Covid Cases"]').remove();
  516. }
  517. function readFile(filename) {
  518. try {
  519. return fs.readFileSync(filename, 'utf8');
  520. } catch(e) {
  521. console.error('Error:', e.stack);
  522. }
  523. }
  524. function writeToFile(filename, text) {
  525. try {
  526. return fs.writeFileSync(filename, text, 'utf8');
  527. } catch(e) {
  528. console.error('Error:', e.stack);
  529. }
  530. }
  531. main();