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.

663 lines
45 KiB

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