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.

617 lines
43 KiB

3 years ago
5 years ago
5 years ago
3 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
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 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
3 years ago
3 years ago
3 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
5 years ago
4 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="#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' +
  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 CONSTRUCTOR_OVERLOADING =
  49. '<span class="hljs-class"><span class="hljs-keyword">class</span> &lt;<span class="hljs-title">name</span>&gt;:</span>\n' +
  50. ' <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' +
  51. ' self.a = a\n';
  52. const DATACLASS =
  53. '<span class="hljs-keyword">from</span> dataclasses <span class="hljs-keyword">import</span> make_dataclass\n' +
  54. '&lt;class&gt; = make_dataclass(<span class="hljs-string">\'&lt;class_name&gt;\'</span>, &lt;coll_of_attribute_names&gt;)\n' +
  55. '&lt;class&gt; = make_dataclass(<span class="hljs-string">\'&lt;class_name&gt;\'</span>, &lt;coll_of_tuples&gt;)\n' +
  56. '&lt;tuple&gt; = (<span class="hljs-string">\'&lt;attr_name&gt;\'</span>, &lt;type&gt; [, &lt;default_value&gt;])';
  57. const SHUTIL_COPY =
  58. 'shutil.copy(from, to) <span class="hljs-comment"># Copies the file. \'to\' can exist or be a dir.</span>\n' +
  59. 'shutil.copytree(from, to) <span class="hljs-comment"># Copies the directory. \'to\' must not exist.</span>\n';
  60. const OS_RENAME =
  61. 'os.rename(from, to) <span class="hljs-comment"># Renames/moves the file or directory.</span>\n' +
  62. 'os.replace(from, to) <span class="hljs-comment"># Same, but overwrites \'to\' if it exists.</span>\n';
  63. const TYPE =
  64. '&lt;class&gt; = type(<span class="hljs-string">\'&lt;class_name&gt;\'</span>, &lt;tuple_of_parents&gt;, &lt;dict_of_class_attributes&gt;)';
  65. const EVAL =
  66. '<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> ast <span class="hljs-keyword">import</span> literal_eval\n' +
  67. '<span class="hljs-meta">&gt;&gt;&gt; </span>literal_eval(<span class="hljs-string">\'[1, 2, 3]\'</span>)\n' +
  68. '[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]\n' +
  69. '<span class="hljs-meta">&gt;&gt;&gt; </span>literal_eval(<span class="hljs-string">\'1 + 2\'</span>)\n' +
  70. 'ValueError: malformed node or string\n';
  71. const PROGRESS_BAR =
  72. '<span class="hljs-comment"># $ pip3 install tqdm</span>\n' +
  73. '<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> tqdm <span class="hljs-keyword">import</span> tqdm\n' +
  74. '<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> time <span class="hljs-keyword">import</span> sleep\n' +
  75. '<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' +
  76. '<span class="hljs-meta">... </span> sleep(<span class="hljs-number">1</span>)\n' +
  77. 'Processing: 100%|████████████████████| 3/3 [00:03&lt;00:00, 1.00s/it]\n';
  78. const PYINSTALLER =
  79. '$ pip3 install pyinstaller\n' +
  80. '$ pyinstaller script.py <span class="hljs-comment"># Compiles into \'./dist/script\' directory.</span>\n' +
  81. '$ pyinstaller script.py --onefile <span class="hljs-comment"># Compiles into \'./dist/script\' console app.</span>\n' +
  82. '$ pyinstaller script.py --windowed <span class="hljs-comment"># Compiles into \'./dist/script\' windowed app.</span>\n' +
  83. '$ pyinstaller script.py --add-data \'&lt;path&gt;:.\' <span class="hljs-comment"># Adds file to the root of the executable.</span>\n';
  84. const INDEX =
  85. '<li><strong>Only available in the <a href="https://transactions.sendowl.com/products/78175486/4422834F/view">PDF</a>.</strong></li>\n' +
  86. '<li><strong>Ctrl+F / ⌘F is usually sufficient.</strong></li>\n' +
  87. '<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';
  88. const DIAGRAM_1_A =
  89. '+------------------+------------+------------+------------+\n' +
  90. '| | Sequence | Collection | Iterable |\n' +
  91. '+------------------+------------+------------+------------+\n';
  92. const DIAGRAM_1_B =
  93. '┏━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┓\n' +
  94. '┃ │ Sequence │ Collection │ Iterable ┃\n' +
  95. '┠──────────────────┼────────────┼────────────┼────────────┨\n' +
  96. '┃ list, range, str │ ✓ │ ✓ │ ✓ ┃\n' +
  97. '┃ dict, set │ │ ✓ │ ✓ ┃\n' +
  98. '┃ iter │ │ │ ✓ ┃\n' +
  99. '┗━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┛\n';
  100. const DIAGRAM_2_A =
  101. '+--------------------+----------+----------+----------+----------+----------+\n' +
  102. '| | Integral | Rational | Real | Complex | Number |\n' +
  103. '+--------------------+----------+----------+----------+----------+----------+\n';
  104. const DIAGRAM_2_B =
  105. '┏━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┓\n' +
  106. '┃ │ Integral │ Rational │ Real │ Complex │ Number ┃\n' +
  107. '┠────────────────────┼──────────┼──────────┼──────────┼──────────┼──────────┨\n' +
  108. '┃ int │ ✓ │ ✓ │ ✓ │ ✓ │ ✓ ┃\n' +
  109. '┃ fractions.Fraction │ │ ✓ │ ✓ │ ✓ │ ✓ ┃\n' +
  110. '┃ float │ │ │ ✓ │ ✓ │ ✓ ┃\n' +
  111. '┃ complex │ │ │ │ ✓ │ ✓ ┃\n' +
  112. '┃ decimal.Decimal │ │ │ │ │ ✓ ┃\n' +
  113. '┗━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┛\n';
  114. const DIAGRAM_3_A =
  115. '+---------------+----------+----------+----------+----------+----------+\n';
  116. const DIAGRAM_3_B =
  117. '┏━━━━━━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┓\n' +
  118. '┃ │ [ !#$%…] │ [a-zA-Z] │ [¼½¾] │ [²³¹] │ [0-9] ┃\n' +
  119. '┠───────────────┼──────────┼──────────┼──────────┼──────────┼──────────┨\n' +
  120. '┃ isprintable() │ ✓ │ ✓ │ ✓ │ ✓ │ ✓ ┃\n' +
  121. '┃ isalnum() │ │ ✓ │ ✓ │ ✓ │ ✓ ┃\n' +
  122. '┃ isnumeric() │ │ │ ✓ │ ✓ │ ✓ ┃\n' +
  123. '┃ isdigit() │ │ │ │ ✓ │ ✓ ┃\n' +
  124. '┃ isdecimal() │ │ │ │ │ ✓ ┃\n' +
  125. '┗━━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┛\n';
  126. const DIAGRAM_4_A =
  127. "+--------------+----------------+----------------+----------------+----------------+\n" +
  128. "| | {<float>} | {<float>:f} | {<float>:e} | {<float>:%} |\n" +
  129. "+--------------+----------------+----------------+----------------+----------------+\n";
  130. const DIAGRAM_4_B =
  131. "┏━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━┓\n" +
  132. "┃ │ {&lt;float&gt;} │ {&lt;float&gt;:f} │ {&lt;float&gt;:e} │ {&lt;float&gt;:%} ┃\n" +
  133. "┠──────────────┼────────────────┼────────────────┼────────────────┼────────────────┨\n" +
  134. "┃ 0.000056789 │ '5.6789e-05' │ '0.000057' │ '5.678900e-05' │ '0.005679%' ┃\n" +
  135. "┃ 0.00056789 │ '0.00056789' │ '0.000568' │ '5.678900e-04' │ '0.056789%' ┃\n" +
  136. "┃ 0.0056789 │ '0.0056789' │ '0.005679' │ '5.678900e-03' │ '0.567890%' ┃\n" +
  137. "┃ 0.056789 │ '0.056789' │ '0.056789' │ '5.678900e-02' │ '5.678900%' ┃\n" +
  138. "┃ 0.56789 │ '0.56789' │ '0.567890' │ '5.678900e-01' │ '56.789000%' ┃\n" +
  139. "┃ 5.6789 │ '5.6789' │ '5.678900' │ '5.678900e+00' │ '567.890000%' ┃\n" +
  140. "┃ 56.789 │ '56.789' │ '56.789000' │ '5.678900e+01' │ '5678.900000%' ┃\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. "┗━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━┛\n";
  158. const DIAGRAM_6_A =
  159. '+------------+------------+------------+------------+--------------+\n' +
  160. '| | Iterable | Collection | Sequence | abc.Sequence |\n' +
  161. '+------------+------------+------------+------------+--------------+\n';
  162. const DIAGRAM_6_B =
  163. '┏━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━━━┓\n' +
  164. '┃ │ Iterable │ Collection │ Sequence │ abc.Sequence ┃\n' +
  165. '┠────────────┼────────────┼────────────┼────────────┼──────────────┨\n' +
  166. '┃ iter() │ ! │ ! │ ✓ │ ✓ ┃\n' +
  167. '┃ contains() │ ✓ │ ✓ │ ✓ │ ✓ ┃\n' +
  168. '┃ len() │ │ ! │ ! │ ! ┃\n' +
  169. '┃ getitem() │ │ │ ! │ ! ┃\n' +
  170. '┃ reversed() │ │ │ ✓ │ ✓ ┃\n' +
  171. '┃ index() │ │ │ │ ✓ ┃\n' +
  172. '┃ count() │ │ │ │ ✓ ┃\n' +
  173. '┗━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━━━┛\n';
  174. const DIAGRAM_7_A =
  175. 'BaseException\n' +
  176. ' +-- SystemExit';
  177. const DIAGRAM_7_B =
  178. "BaseException\n" +
  179. " ├── SystemExit <span class='hljs-comment'># Raised by the sys.exit() function.</span>\n" +
  180. " ├── KeyboardInterrupt <span class='hljs-comment'># Raised when the user hits the interrupt key (ctrl-c).</span>\n" +
  181. " └── Exception <span class='hljs-comment'># User-defined exceptions should be derived from this class.</span>\n" +
  182. " ├── ArithmeticError <span class='hljs-comment'># Base class for arithmetic errors.</span>\n" +
  183. " │ └── ZeroDivisionError <span class='hljs-comment'># Raised when dividing by zero.</span>\n" +
  184. " ├── AttributeError <span class='hljs-comment'># Raised when an attribute is missing.</span>\n" +
  185. " ├── EOFError <span class='hljs-comment'># Raised by input() when it hits end-of-file condition.</span>\n" +
  186. " ├── LookupError <span class='hljs-comment'># Raised when a look-up on a collection fails.</span>\n" +
  187. " │ ├── IndexError <span class='hljs-comment'># Raised when a sequence index is out of range.</span>\n" +
  188. " │ └── KeyError <span class='hljs-comment'># Raised when a dictionary key or set element is not found.</span>\n" +
  189. " ├── NameError <span class='hljs-comment'># Raised when a variable name is not found.</span>\n" +
  190. " ├── OSError <span class='hljs-comment'># Errors such as “file not found” or “disk full” (see Open).</span>\n" +
  191. " │ └── FileNotFoundError <span class='hljs-comment'># When a file or directory is requested but doesn't exist.</span>\n" +
  192. " ├── RuntimeError <span class='hljs-comment'># Raised by errors that don't fall in other categories.</span>\n" +
  193. " │ └── RecursionError <span class='hljs-comment'># Raised when the maximum recursion depth is exceeded.</span>\n" +
  194. " ├── StopIteration <span class='hljs-comment'># Raised by next() when run on an empty iterator.</span>\n" +
  195. " ├── TypeError <span class='hljs-comment'># Raised when an argument is of wrong type.</span>\n" +
  196. " └── ValueError <span class='hljs-comment'># When an argument is of right type but inappropriate value.</span>\n" +
  197. " └── UnicodeError <span class='hljs-comment'># Raised when encoding/decoding strings to/from bytes fails.</span>\n";
  198. const DIAGRAM_8_A =
  199. '+-----------+------------+------------+------------+\n' +
  200. '| | List | Set | Dict |\n' +
  201. '+-----------+------------+------------+------------+\n';
  202. const DIAGRAM_8_B =
  203. '┏━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┓\n' +
  204. '┃ │ List │ Set │ Dict ┃\n' +
  205. '┠───────────┼────────────┼────────────┼────────────┨\n' +
  206. '┃ getitem() │ IndexError │ │ KeyError ┃\n' +
  207. '┃ pop() │ IndexError │ KeyError │ KeyError ┃\n' +
  208. '┃ remove() │ ValueError │ KeyError │ ┃\n' +
  209. '┃ index() │ ValueError │ │ ┃\n' +
  210. '┗━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┛\n';
  211. const DIAGRAM_9_A =
  212. '+------------------+--------------+--------------+--------------+\n' +
  213. '| | excel | excel-tab | unix |\n' +
  214. '+------------------+--------------+--------------+--------------+\n';
  215. const DIAGRAM_9_B =
  216. "┏━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━┓\n" +
  217. "┃ │ excel │ excel-tab │ unix ┃\n" +
  218. "┠──────────────────┼──────────────┼──────────────┼──────────────┨\n" +
  219. "┃ delimiter │ ',' │ '\\t' │ ',' ┃\n" +
  220. "┃ quotechar │ '\"' │ '\"' │ '\"' ┃\n" +
  221. "┃ doublequote │ True │ True │ True ┃\n" +
  222. "┃ skipinitialspace │ False │ False │ False ┃\n" +
  223. "┃ lineterminator │ '\\r\\n' │ '\\r\\n' │ '\\n' ┃\n" +
  224. "┃ quoting │ 0 │ 0 │ 1 ┃\n" +
  225. "┃ escapechar │ None │ None │ None ┃\n" +
  226. "┗━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━┛\n";
  227. const DIAGRAM_10_A =
  228. '+-------------+-------------+\n' +
  229. '| Classes | Metaclasses |\n' +
  230. '+-------------+-------------|\n' +
  231. '| MyClass --> MyMetaClass |\n';
  232. const DIAGRAM_10_B =
  233. '┏━━━━━━━━━━━━━┯━━━━━━━━━━━━━┓\n' +
  234. '┃ Classes │ Metaclasses ┃\n' +
  235. '┠─────────────┼─────────────┨\n' +
  236. '┃ MyClass ──→ MyMetaClass ┃\n' +
  237. '┃ │ ↓ ┃\n' +
  238. '┃ object ─────→ type ←╮ ┃\n' +
  239. '┃ │ ↑ ╰──╯ ┃\n' +
  240. '┃ str ──────────╯ ┃\n' +
  241. '┗━━━━━━━━━━━━━┷━━━━━━━━━━━━━┛\n';
  242. const DIAGRAM_11_A =
  243. '+-------------+-------------+\n' +
  244. '| Classes | Metaclasses |\n' +
  245. '+-------------+-------------|\n' +
  246. '| MyClass | MyMetaClass |\n';
  247. const DIAGRAM_11_B =
  248. '┏━━━━━━━━━━━━━┯━━━━━━━━━━━━━┓\n' +
  249. '┃ Classes │ Metaclasses ┃\n' +
  250. '┠─────────────┼─────────────┨\n' +
  251. '┃ MyClass │ MyMetaClass ┃\n' +
  252. '┃ ↓ │ ↓ ┃\n' +
  253. '┃ object ←───── type ┃\n' +
  254. '┃ ↑ │ ┃\n' +
  255. '┃ str │ ┃\n' +
  256. '┗━━━━━━━━━━━━━┷━━━━━━━━━━━━━┛\n';
  257. const DIAGRAM_12_A =
  258. '+-----------+-------------+------+-------------+\n' +
  259. '| sampwidth | min | zero | max |\n' +
  260. '+-----------+-------------+------+-------------+\n';
  261. const DIAGRAM_12_B =
  262. '┏━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━┯━━━━━━━━━━━━━┓\n' +
  263. '┃ sampwidth │ min │ zero │ max ┃\n' +
  264. '┠───────────┼─────────────┼──────┼─────────────┨\n' +
  265. '┃ 1 │ 0 │ 128 │ 255 ┃\n' +
  266. '┃ 2 │ -32768 │ 0 │ 32767 ┃\n' +
  267. '┃ 3 │ -8388608 │ 0 │ 8388607 ┃\n' +
  268. '┃ 4 │ -2147483648 │ 0 │ 2147483647 ┃\n' +
  269. '┗━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━┷━━━━━━━━━━━━━┛\n';
  270. const DIAGRAM_13_A =
  271. '| sr.apply(…) | 3 | sum 3 | s 3 |';
  272. const DIAGRAM_13_B =
  273. "┏━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┓\n" +
  274. "┃ │ 'sum' │ ['sum'] │ {'s': 'sum'} ┃\n" +
  275. "┠─────────────┼─────────────┼─────────────┼───────────────┨\n" +
  276. "┃ sr.apply(…) │ 3 │ sum 3 │ s 3 ┃\n" +
  277. "┃ sr.agg(…) │ │ │ ┃\n" +
  278. "┗━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┛\n";
  279. const DIAGRAM_14_A =
  280. '| sr.apply(…) | | rank | |';
  281. const DIAGRAM_14_B =
  282. "┏━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┓\n" +
  283. "┃ │ 'rank' │ ['rank'] │ {'r': 'rank'} ┃\n" +
  284. "┠─────────────┼─────────────┼─────────────┼───────────────┨\n" +
  285. "┃ sr.apply(…) │ │ rank │ ┃\n" +
  286. "┃ sr.agg(…) │ x 1 │ x 1 │ r x 1 ┃\n" +
  287. "┃ sr.trans(…) │ y 2 │ y 2 │ y 2 ┃\n" +
  288. "┗━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┛\n";
  289. const DIAGRAM_15_A =
  290. '+------------------------+---------------+------------+------------+--------------------------+';
  291. const DIAGRAM_15_B =
  292. "┏━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n" +
  293. "┃ │ 'outer' │ 'inner' │ 'left' │ Description ┃\n" +
  294. "┠────────────────────────┼───────────────┼────────────┼────────────┼──────────────────────────┨\n" +
  295. "┃ l.merge(r, on='y', │ x y z │ x y z │ x y z │ Joins/merges on column. ┃\n" +
  296. "┃ how=…) │ 0 1 2 . │ 3 4 5 │ 1 2 . │ Also accepts left_on and ┃\n" +
  297. "┃ │ 1 3 4 5 │ │ 3 4 5 │ right_on parameters. ┃\n" +
  298. "┃ │ 2 . 6 7 │ │ │ Uses 'inner' by default. ┃\n" +
  299. "┠────────────────────────┼───────────────┼────────────┼────────────┼──────────────────────────┨\n" +
  300. "┃ l.join(r, lsuffix='l', │ x yl yr z │ │ x yl yr z │ Joins/merges on row keys.┃\n" +
  301. "┃ rsuffix='r', │ a 1 2 . . │ x yl yr z │ 1 2 . . │ Uses 'left' by default. ┃\n" +
  302. "┃ how=…) │ b 3 4 4 5 │ 3 4 4 5 │ 3 4 4 5 │ If r is a series, it is ┃\n" +
  303. "┃ │ c . . 6 7 │ │ │ treated as a column. ┃\n" +
  304. "┠────────────────────────┼───────────────┼────────────┼────────────┼──────────────────────────┨\n" +
  305. "┃ pd.concat([l, r], │ x y z │ y │ │ Adds rows at the bottom. ┃\n" +
  306. "┃ axis=0, │ a 1 2 . │ 2 │ │ Uses 'outer' by default. ┃\n" +
  307. "┃ join=…) │ b 3 4 . │ 4 │ │ A series is treated as a ┃\n" +
  308. "┃ │ b . 4 5 │ 4 │ │ column. Use l.append(sr) ┃\n" +
  309. "┃ │ c . 6 7 │ 6 │ │ to add a row instead. ┃\n" +
  310. "┠────────────────────────┼───────────────┼────────────┼────────────┼──────────────────────────┨\n" +
  311. "┃ pd.concat([l, r], │ x y y z │ │ │ Adds columns at the ┃\n" +
  312. "┃ axis=1, │ a 1 2 . . │ x y y z │ │ right end. Uses 'outer' ┃\n" +
  313. "┃ join=…) │ b 3 4 4 5 │ 3 4 4 5 │ │ by default. A series is ┃\n" +
  314. "┃ │ c . . 6 7 │ │ │ treated as a column. ┃\n" +
  315. "┠────────────────────────┼───────────────┼────────────┼────────────┼──────────────────────────┨\n" +
  316. "┃ l.combine_first(r) │ x y z │ │ │ Adds missing rows and ┃\n" +
  317. "┃ │ a 1 2 . │ │ │ columns. Also updates ┃\n" +
  318. "┃ │ b 3 4 5 │ │ │ items that contain NaN. ┃\n" +
  319. "┃ │ c . 6 7 │ │ │ R must be a DataFrame. ┃\n" +
  320. "┗━━━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n";
  321. const DIAGRAM_16_A =
  322. '| df.apply(…) | | x y | |';
  323. const DIAGRAM_16_B =
  324. "┏━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┓\n" +
  325. "┃ │ 'sum' │ ['sum'] │ {'x': 'sum'} ┃\n" +
  326. "┠─────────────┼─────────────┼─────────────┼───────────────┨\n" +
  327. "┃ df.apply(…) │ │ x y │ ┃\n" +
  328. "┃ df.agg(…) │ x 4 │ sum 4 6 │ x 4 ┃\n" +
  329. "┃ │ y 6 │ │ ┃\n" +
  330. "┗━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┛\n";
  331. const DIAGRAM_17_A =
  332. '| df.apply(…) | x y | x y | x |';
  333. const DIAGRAM_17_B =
  334. "┏━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┓\n" +
  335. "┃ │ 'rank' │ ['rank'] │ {'x': 'rank'} ┃\n" +
  336. "┠─────────────┼─────────────┼─────────────┼───────────────┨\n" +
  337. "┃ df.apply(…) │ x y │ x y │ x ┃\n" +
  338. "┃ df.agg(…) │ a 1 1 │ rank rank │ a 1 ┃\n" +
  339. "┃ df.trans(…) │ b 2 2 │ a 1 1 │ b 2 ┃\n" +
  340. "┃ │ │ b 2 2 │ ┃\n" +
  341. "┗━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┛\n";
  342. const DIAGRAM_18_A =
  343. '| gb.agg(…) | x y | x y | x y | x |';
  344. const DIAGRAM_18_B =
  345. "┏━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┓\n" +
  346. "┃ │ 'sum' │ 'rank' │ ['rank'] │ {'x': 'rank'} ┃\n" +
  347. "┠─────────────┼─────────────┼─────────────┼─────────────┼───────────────┨\n" +
  348. "┃ gb.agg(…) │ x y │ x y │ x y │ x ┃\n" +
  349. "┃ │ z │ a 1 1 │ rank rank │ a 1 ┃\n" +
  350. "┃ │ 3 1 2 │ b 1 1 │ a 1 1 │ b 1 ┃\n" +
  351. "┃ │ 6 11 13 │ c 2 2 │ b 1 1 │ c 2 ┃\n" +
  352. "┃ │ │ │ c 2 2 │ ┃\n" +
  353. "┠─────────────┼─────────────┼─────────────┼─────────────┼───────────────┨\n" +
  354. "┃ gb.trans(…) │ x y │ x y │ │ ┃\n" +
  355. "┃ │ a 1 2 │ a 1 1 │ │ ┃\n" +
  356. "┃ │ b 11 13 │ b 1 1 │ │ ┃\n" +
  357. "┃ │ c 11 13 │ c 1 1 │ │ ┃\n" +
  358. "┗━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┛\n";
  359. function main() {
  360. const html = getMd();
  361. initDom(html);
  362. modifyPage();
  363. var template = readFile('web/template.html');
  364. template = updateDate(template);
  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>\', <tuple_of_parents>, <dict_of_class_attributes>)`).html(TYPE);
  465. $(`code:contains(ValueError: malformed node)`).html(EVAL);
  466. $(`code:contains(pip3 install tqdm)`).html(PROGRESS_BAR);
  467. $(`code:contains(pip3 install pyinstaller)`).html(PYINSTALLER);
  468. $(`ul:contains(Only available in)`).html(INDEX);
  469. }
  470. function preventPageBreaks() {
  471. $(':header').each(function(index) {
  472. var el = $(this)
  473. var untilPre = el.nextUntil('pre')
  474. var untilH2 = el.nextUntil('h2')
  475. if ((untilPre.length < untilH2.length) || el.prop('tagName') === 'H1') {
  476. untilPre.add(el).next().add(el).wrapAll("<div></div>");
  477. } else {
  478. untilH2.add(el).wrapAll("<div></div>");
  479. }
  480. });
  481. }
  482. function fixPageBreaksFile() {
  483. const modesDiv = $('#file').parent().parent().parent()
  484. move(modesDiv, 'file')
  485. move(modesDiv, 'exceptions-1')
  486. }
  487. function fixPageBreaksStruct() {
  488. const formatDiv = $('#floatingpointtypes').parent().parent().parent().parent()
  489. move(formatDiv, 'floatingpointtypes')
  490. move(formatDiv, 'integertypesuseacapitalletterforunsignedtypeminimumandstandardsizesareinbrackets')
  491. move(formatDiv, 'forstandardsizesstartformatstringwith')
  492. }
  493. function move(anchor_el, el_id) {
  494. const el = $('#'+el_id).parent()
  495. anchor_el.after(el)
  496. }
  497. function insertPageBreaks() {
  498. insertPageBreakBefore('#decorator')
  499. // insertPageBreakBefore('#print')
  500. }
  501. function insertPageBreakBefore(an_id) {
  502. $('<div class="pagebreak"></div>').insertBefore($(an_id).parent())
  503. }
  504. function fixPandasDiagram() {
  505. const diagram_15 = '┏━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━┓';
  506. $(`code:contains(${diagram_15})`).find(".hljs-keyword:contains(and)").after("and");
  507. $(`code:contains(${diagram_15})`).find(".hljs-keyword:contains(as)").after("as");
  508. $(`code:contains(${diagram_15})`).find(".hljs-keyword:contains(is)").after("is");
  509. $(`code:contains(${diagram_15})`).find(".hljs-keyword").remove();
  510. }
  511. function removePlotImages() {
  512. $('img[alt="Covid Deaths"]').remove();
  513. $('img[alt="Covid Cases"]').remove();
  514. }
  515. function updateDate(template) {
  516. const date = new Date();
  517. const date_str = date.toLocaleString('en-us', {month: 'long', day: 'numeric', year: 'numeric'});
  518. template = template.replace('May 20, 2021', date_str);
  519. template = template.replace('May 20, 2021', date_str);
  520. return template;
  521. }
  522. // UTIL
  523. function readFile(filename) {
  524. try {
  525. return fs.readFileSync(filename, 'utf8');
  526. } catch(e) {
  527. console.error('Error:', e.stack);
  528. }
  529. }
  530. function writeToFile(filename, text) {
  531. try {
  532. return fs.writeFileSync(filename, text, 'utf8');
  533. } catch(e) {
  534. console.error('Error:', e.stack);
  535. }
  536. }
  537. main();