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.

587 lines
42 KiB

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