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.

566 lines
40 KiB

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