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.

642 lines
44 KiB

4 years ago
5 years ago
3 years ago
5 years ago
4 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
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
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
5 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
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="#imports">Import</a>, <a href="#decorator">Decorator</a>, <a href="#class">Class</a>, <a href="#ducktypes">Duck_Types</a>, <a href="#enum">Enum</a>, <a href="#exceptions">Exception</a>],\n' +
  35. ' <strong><span class="hljs-string">\'4. System\'</span></strong>: [<a href="#exit">Exit</a>, <a href="#print">Print</a>, <a href="#input">Input</a>, <a href="#commandlinearguments">Command_Line_Arguments</a>, <a href="#open">Open</a>, <a href="#paths">Path</a>, <a href="#oscommands">OS_Commands</a>],\n' +
  36. ' <strong><span class="hljs-string">\'5. Data\'</span></strong>: [<a href="#json">JSON</a>, <a href="#pickle">Pickle</a>, <a href="#csv">CSV</a>, <a href="#sqlite">SQLite</a>, <a href="#bytes">Bytes</a>, <a href="#struct">Struct</a>, <a href="#array">Array</a>, <a href="#memoryview">Memory_View</a>, <a href="#deque">Deque</a>],\n' +
  37. ' <strong><span class="hljs-string">\'6. Advanced\'</span></strong>: [<a href="#threading">Threading</a>, <a href="#operator">Operator</a>, <a href="#introspection">Introspection</a>, <a href="#metaprogramming">Metaprograming</a>, <a href="#eval">Eval</a>, <a href="#coroutines">Coroutine</a>],\n' +
  38. ' <strong><span class="hljs-string">\'7. Libraries\'</span></strong>: [<a href="#progressbar">Progress_Bar</a>, <a href="#plot">Plot</a>, <a href="#table">Table</a>, <a href="#curses">Curses</a>, <a href="#logging">Logging</a>, <a href="#scraping">Scraping</a>, <a href="#web">Web</a>, <a href="#profiling">Profile</a>,\n' +
  39. ' <a href="#numpy">NumPy</a>, <a href="#image">Image</a>, <a href="#audio">Audio</a>, <a href="#pygame">Games</a>, <a href="#pandas">Data</a>]\n' +
  40. '}\n' +
  41. '</code></pre>\n';
  42. const LRU_CACHE =
  43. '<span class="hljs-keyword">from</span> functools <span class="hljs-keyword">import</span> lru_cache\n' +
  44. '\n' +
  45. '<span class="hljs-meta">@lru_cache(maxsize=None)</span>\n' +
  46. '<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">fib</span><span class="hljs-params">(n)</span>:</span>\n' +
  47. ' <span class="hljs-keyword">return</span> n <span class="hljs-keyword">if</span> n &lt; <span class="hljs-number">2</span> <span class="hljs-keyword">else</span> fib(n-<span class="hljs-number">2</span>) + fib(n-<span class="hljs-number">1</span>)\n';
  48. const 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 into 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. "\n" +
  280. "┏━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┓\n" +
  281. "┃ │ 'rank' │ ['rank'] │ {'r': 'rank'} ┃\n" +
  282. "┠─────────────────┼─────────────┼─────────────┼───────────────┨\n" +
  283. "┃ sr.apply(…) │ │ rank │ ┃\n" +
  284. "┃ sr.agg(…) │ x 1 │ x 1 │ r x 1 ┃\n" +
  285. "┃ sr.transform(…) │ y 2 │ y 2 │ y 2 ┃\n" +
  286. "┗━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┛\n";
  287. const DIAGRAM_15_A =
  288. '+------------------------+---------------+------------+------------+--------------------------+';
  289. const DIAGRAM_15_B =
  290. "┏━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n" +
  291. "┃ │ 'outer' │ 'inner' │ 'left' │ Description ┃\n" +
  292. "┠────────────────────────┼───────────────┼────────────┼────────────┼──────────────────────────┨\n" +
  293. "┃ l.merge(r, on='y', │ x y z │ x y z │ x y z │ Joins/merges on column. ┃\n" +
  294. "┃ how=…) │ 0 1 2 . │ 3 4 5 │ 1 2 . │ Also accepts left_on and ┃\n" +
  295. "┃ │ 1 3 4 5 │ │ 3 4 5 │ right_on parameters. ┃\n" +
  296. "┃ │ 2 . 6 7 │ │ │ Uses 'inner' by default. ┃\n" +
  297. "┠────────────────────────┼───────────────┼────────────┼────────────┼──────────────────────────┨\n" +
  298. "┃ l.join(r, lsuffix='l', │ x yl yr z │ │ x yl yr z │ Joins/merges on row keys.┃\n" +
  299. "┃ rsuffix='r', │ a 1 2 . . │ x yl yr z │ 1 2 . . │ Uses 'left' by default. ┃\n" +
  300. "┃ how=…) │ b 3 4 4 5 │ 3 4 4 5 │ 3 4 4 5 │ If r is a series, it is ┃\n" +
  301. "┃ │ c . . 6 7 │ │ │ treated as a column. ┃\n" +
  302. "┠────────────────────────┼───────────────┼────────────┼────────────┼──────────────────────────┨\n" +
  303. "┃ pd.concat([l, r], │ x y z │ y │ │ Adds rows at the bottom. ┃\n" +
  304. "┃ axis=0, │ a 1 2 . │ 2 │ │ Uses 'outer' by default. ┃\n" +
  305. "┃ join=…) │ b 3 4 . │ 4 │ │ A series is treated as a ┃\n" +
  306. "┃ │ b . 4 5 │ 4 │ │ column. Use l.append(sr) ┃\n" +
  307. "┃ │ c . 6 7 │ 6 │ │ to add a row instead. ┃\n" +
  308. "┠────────────────────────┼───────────────┼────────────┼────────────┼──────────────────────────┨\n" +
  309. "┃ pd.concat([l, r], │ x y y z │ │ │ Adds columns at the ┃\n" +
  310. "┃ axis=1, │ a 1 2 . . │ x y y z │ │ right end. Uses 'outer' ┃\n" +
  311. "┃ join=…) │ b 3 4 4 5 │ 3 4 4 5 │ │ by default. A series is ┃\n" +
  312. "┃ │ c . . 6 7 │ │ │ treated as a column. ┃\n" +
  313. "┠────────────────────────┼───────────────┼────────────┼────────────┼──────────────────────────┨\n" +
  314. "┃ l.combine_first(r) │ x y z │ │ │ Adds missing rows and ┃\n" +
  315. "┃ │ a 1 2 . │ │ │ columns. Also updates ┃\n" +
  316. "┃ │ b 3 4 5 │ │ │ items that contain NaN. ┃\n" +
  317. "┃ │ c . 6 7 │ │ │ R must be a DataFrame. ┃\n" +
  318. "┗━━━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n";
  319. const DIAGRAM_16_A =
  320. '| df.apply(…) | | x y | |';
  321. const DIAGRAM_16_B =
  322. "┏━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┓\n" +
  323. "┃ │ 'sum' │ ['sum'] │ {'x': 'sum'} ┃\n" +
  324. "┠─────────────────┼─────────────┼─────────────┼───────────────┨\n" +
  325. "┃ df.apply(…) │ │ x y │ ┃\n" +
  326. "┃ df.agg(…) │ x 4 │ sum 4 6 │ x 4 ┃\n" +
  327. "┃ │ y 6 │ │ ┃\n" +
  328. "┗━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┛\n" +
  329. "\n" +
  330. "┏━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┓\n" +
  331. "┃ │ 'rank' │ ['rank'] │ {'x': 'rank'} ┃\n" +
  332. "┠─────────────────┼─────────────┼─────────────┼───────────────┨\n" +
  333. "┃ df.apply(…) │ x y │ x y │ x ┃\n" +
  334. "┃ df.agg(…) │ a 1 1 │ rank rank │ a 1 ┃\n" +
  335. "┃ df.transform(…) │ b 2 2 │ a 1 1 │ b 2 ┃\n" +
  336. "┃ │ │ b 2 2 │ ┃\n" +
  337. "┗━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┛\n";
  338. const DIAGRAM_18_A =
  339. '| gb.agg(…) | x y | x y | x y | x |';
  340. const DIAGRAM_18_B =
  341. "┏━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┓\n" +
  342. "┃ │ 'sum' │ 'rank' │ ['rank'] │ {'x': 'rank'} ┃\n" +
  343. "┠─────────────────┼─────────────┼─────────────┼─────────────┼───────────────┨\n" +
  344. "┃ gb.agg(…) │ x y │ x y │ x y │ x ┃\n" +
  345. "┃ │ z │ a 1 1 │ rank rank │ a 1 ┃\n" +
  346. "┃ │ 3 1 2 │ b 1 1 │ a 1 1 │ b 1 ┃\n" +
  347. "┃ │ 6 11 13 │ c 2 2 │ b 1 1 │ c 2 ┃\n" +
  348. "┃ │ │ │ c 2 2 │ ┃\n" +
  349. "┠─────────────────┼─────────────┼─────────────┼─────────────┼───────────────┨\n" +
  350. "┃ gb.transform(…) │ x y │ x y │ │ ┃\n" +
  351. "┃ │ a 1 2 │ a 1 1 │ │ ┃\n" +
  352. "┃ │ b 11 13 │ b 1 1 │ │ ┃\n" +
  353. "┃ │ c 11 13 │ c 2 2 │ │ ┃\n" +
  354. "┗━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┛\n";
  355. const MENU = '<a href="https://raw.githubusercontent.com/gto76/python-cheatsheet/main/README.md">Download text file</a>, <a href="https://transactions.sendowl.com/products/78175486/4422834F/view">Buy PDF</a>, <a href="https://github.com/gto76/python-cheatsheet">Fork me on GitHub</a>, <a href="https://github.com/gto76/python-cheatsheet/wiki/Frequently-Asked-Questions">Check out FAQ</a> or <a href="index.html?theme=dark3">Switch to dark theme</a>.\n';
  356. const DARK_THEME_SCRIPT =
  357. '<script>\n' +
  358. ' // Changes the image and link to theme if URL ends with "index.html?theme=dark". \n' +
  359. ' if (window.location.search.search(/[?&]theme=dark/) !== -1) {\n' +
  360. '\n' +
  361. ' var link_to_theme = document.createElement("a")\n' +
  362. ' link_to_theme.href = "index.html"\n' +
  363. ' link_to_theme.text = "Switch to light theme"\n' +
  364. ' document.getElementsByClassName("banner")[0].firstChild.children[4].replaceWith(link_to_theme)\n' +
  365. '\n' +
  366. ' var img_dark = document.createElement("img");\n' +
  367. ' img_dark.src = "web/image_orig_blue6.png";\n' +
  368. ' img_dark.alt = "Monthy Python";\n' +
  369. ' if ((window.location.search.search(/[?&]theme=dark2/) !== -1) ||\n' +
  370. ' (window.location.search.search(/[?&]theme=dark3/) !== -1)) {\n' +
  371. ' img_dark.style = "width: 910px;";\n' +
  372. ' } else {\n' +
  373. ' img_dark.style = "width: 960px;";\n' +
  374. ' }\n' +
  375. ' document.getElementsByClassName("banner")[1].firstChild.replaceWith(img_dark);\n' +
  376. ' }\n' +
  377. '</script>';
  378. function main() {
  379. const html = getMd();
  380. initDom(html);
  381. modifyPage();
  382. var template = readFile('web/template.html');
  383. template = updateDate(template);
  384. const tokens = template.split('<div id=main_container></div>');
  385. const text = `${tokens[0]} ${document.body.innerHTML} ${tokens[1]}`;
  386. writeToFile('index.html', text);
  387. }
  388. function getMd() {
  389. var readme = readFile('README.md');
  390. var readme = readme.replace("#semaphore-event-barrier", "#semaphoreeventbarrier");
  391. const converter = new showdown.Converter();
  392. return converter.makeHtml(readme);
  393. }
  394. function initDom(html) {
  395. const { JSDOM } = jsdom;
  396. const dom = new JSDOM(html);
  397. const $ = (require('jquery'))(dom.window);
  398. global.$ = $;
  399. global.document = dom.window.document;
  400. }
  401. function modifyPage() {
  402. changeMenu();
  403. addDarkThemeScript();
  404. removeOrigToc();
  405. addToc();
  406. insertLinks();
  407. unindentBanner();
  408. updateDiagrams();
  409. highlightCode();
  410. fixPandasDiagram();
  411. removePlotImages();
  412. }
  413. function changeMenu() {
  414. $('sup').first().html(MENU)
  415. }
  416. function addDarkThemeScript() {
  417. $('#main').before(DARK_THEME_SCRIPT);
  418. }
  419. function removeOrigToc() {
  420. const headerContents = $('#contents');
  421. const contentsList = headerContents.next();
  422. headerContents.remove();
  423. contentsList.remove();
  424. }
  425. function addToc() {
  426. const nodes = $.parseHTML(TOC);
  427. $('#main').before(nodes);
  428. }
  429. function insertLinks() {
  430. $('h2').each(function() {
  431. const aId = $(this).attr('id');
  432. const text = $(this).text();
  433. const line = `<a href="#${aId}" name="${aId}">#</a>${text}`;
  434. $(this).html(line);
  435. });
  436. }
  437. function unindentBanner() {
  438. const montyImg = $('img').first();
  439. montyImg.parent().addClass('banner');
  440. const downloadPraragrapth = $('p').first();
  441. downloadPraragrapth.addClass('banner');
  442. }
  443. function updateDiagrams() {
  444. $(`code:contains(${DIAGRAM_1_A})`).html(DIAGRAM_1_B);
  445. $(`code:contains(${DIAGRAM_2_A})`).html(DIAGRAM_2_B);
  446. $(`code:contains(${DIAGRAM_3_A})`).html(DIAGRAM_3_B);
  447. $(`code:contains(${DIAGRAM_4_A})`).html(DIAGRAM_4_B);
  448. $(`code:contains(${DIAGRAM_5_A})`).html(DIAGRAM_5_B);
  449. $(`code:contains(${DIAGRAM_6_A})`).html(DIAGRAM_6_B);
  450. $(`code:contains(${DIAGRAM_7_A})`).html(DIAGRAM_7_B);
  451. $(`code:contains(${DIAGRAM_8_A})`).html(DIAGRAM_8_B);
  452. $(`code:contains(${DIAGRAM_9_A})`).html(DIAGRAM_9_B);
  453. $(`code:contains(${DIAGRAM_10_A})`).html(DIAGRAM_10_B);
  454. $(`code:contains(${DIAGRAM_11_A})`).html(DIAGRAM_11_B);
  455. $(`code:contains(${DIAGRAM_12_A})`).html(DIAGRAM_12_B).removeClass("text").removeClass("language-text").addClass("python");
  456. $(`code:contains(${DIAGRAM_13_A})`).html(DIAGRAM_13_B).removeClass("text").removeClass("language-text").addClass("python");
  457. $(`code:contains(${DIAGRAM_15_A})`).html(DIAGRAM_15_B).removeClass("text").removeClass("language-text").addClass("python");
  458. $(`code:contains(${DIAGRAM_16_A})`).html(DIAGRAM_16_B).removeClass("text").removeClass("language-text").addClass("python");
  459. $(`code:contains(${DIAGRAM_18_A})`).html(DIAGRAM_18_B).removeClass("text").removeClass("language-text").addClass("python");
  460. }
  461. function highlightCode() {
  462. setApaches(['<D>', '<T>', '<DT>', '<TD>', '<a>', '<n>']);
  463. $('code').not('.python').not('.text').not('.bash').not('.apache').addClass('python');
  464. $('code').each(function(index) {
  465. hljs.highlightBlock(this);
  466. });
  467. fixClasses();
  468. fixHighlights();
  469. preventPageBreaks();
  470. fixPageBreaksFile();
  471. fixPageBreaksStruct();
  472. insertPageBreaks();
  473. }
  474. function setApaches(elements) {
  475. for (el of elements) {
  476. $(`code:contains(${el})`).addClass('apache');
  477. }
  478. }
  479. function fixClasses() {
  480. // Changes class="hljs-keyword" to class="hljs-title" of 'class' keyword.
  481. $('.hljs-class').filter(':contains(class \')').find(':first-child').removeClass('hljs-keyword').addClass('hljs-title')
  482. }
  483. function fixHighlights() {
  484. $(`code:contains(@lru_cache(maxsize=None))`).html(LRU_CACHE);
  485. $(`code:contains((self, a=None):)`).html(CONSTRUCTOR_OVERLOADING);
  486. $(`code:contains(make_dataclass(\'<class_name>\')`).html(DATACLASS);
  487. $(`code:contains(shutil.copy)`).html(SHUTIL_COPY);
  488. $(`code:contains(os.rename)`).html(OS_RENAME);
  489. $(`code:contains(\'<class_name>\', <tuple_of_parents>, <dict_of_class_attributes>)`).html(TYPE);
  490. $(`code:contains(ValueError: malformed node)`).html(EVAL);
  491. $(`code:contains(pip3 install tqdm)`).html(PROGRESS_BAR);
  492. $(`code:contains(pip3 install pyinstaller)`).html(PYINSTALLER);
  493. $(`ul:contains(Only available in)`).html(INDEX);
  494. }
  495. function preventPageBreaks() {
  496. $(':header').each(function(index) {
  497. var el = $(this)
  498. var untilPre = el.nextUntil('pre')
  499. var untilH2 = el.nextUntil('h2')
  500. if ((untilPre.length < untilH2.length) || el.prop('tagName') === 'H1') {
  501. untilPre.add(el).next().add(el).wrapAll("<div></div>");
  502. } else {
  503. untilH2.add(el).wrapAll("<div></div>");
  504. }
  505. });
  506. }
  507. function fixPageBreaksFile() {
  508. const modesDiv = $('#file').parent().parent().parent()
  509. move(modesDiv, 'file')
  510. move(modesDiv, 'exceptions-1')
  511. }
  512. function fixPageBreaksStruct() {
  513. const formatDiv = $('#floatingpointtypes').parent().parent().parent().parent()
  514. move(formatDiv, 'floatingpointtypes')
  515. move(formatDiv, 'integertypesuseacapitalletterforunsignedtypeminimumandstandardsizesareinbrackets')
  516. move(formatDiv, 'forstandardsizesstartformatstringwith')
  517. }
  518. function move(anchor_el, el_id) {
  519. const el = $('#'+el_id).parent()
  520. anchor_el.after(el)
  521. }
  522. function insertPageBreaks() {
  523. insertPageBreakBefore('#decorator')
  524. // insertPageBreakBefore('#print')
  525. }
  526. function insertPageBreakBefore(an_id) {
  527. $('<div class="pagebreak"></div>').insertBefore($(an_id).parent())
  528. }
  529. function fixPandasDiagram() {
  530. const diagram_15 = '┏━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━┓';
  531. $(`code:contains(${diagram_15})`).find(".hljs-keyword:contains(and)").after("and");
  532. $(`code:contains(${diagram_15})`).find(".hljs-keyword:contains(as)").after("as");
  533. $(`code:contains(${diagram_15})`).find(".hljs-keyword:contains(is)").after("is");
  534. $(`code:contains(${diagram_15})`).find(".hljs-keyword").remove();
  535. }
  536. function removePlotImages() {
  537. $('img[alt="Covid Deaths"]').remove();
  538. $('img[alt="Covid Cases"]').remove();
  539. }
  540. function updateDate(template) {
  541. const date = new Date();
  542. const date_str = date.toLocaleString('en-us', {month: 'long', day: 'numeric', year: 'numeric'});
  543. template = template.replace('May 20, 2021', date_str);
  544. template = template.replace('May 20, 2021', date_str);
  545. return template;
  546. }
  547. // UTIL
  548. function readFile(filename) {
  549. try {
  550. return fs.readFileSync(filename, 'utf8');
  551. } catch(e) {
  552. console.error('Error:', e.stack);
  553. }
  554. }
  555. function writeToFile(filename, text) {
  556. try {
  557. return fs.writeFileSync(filename, text, 'utf8');
  558. } catch(e) {
  559. console.error('Error:', e.stack);
  560. }
  561. }
  562. main();