Skip to content

Commit c2b3a1a

Browse files
committed
ABC
1 parent 30d07cd commit c2b3a1a

File tree

4 files changed

+139
-18
lines changed

4 files changed

+139
-18
lines changed

README.md

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -249,15 +249,36 @@ from types import FunctionType, MethodType, LambdaType, GeneratorType
249249
**An abstract base class introduces virtual subclasses, that don’t inherit from it but are still recognized by isinstance() and issubclass().**
250250

251251
```python
252-
from numbers import Integral, Rational, Real, Complex, Number
253-
from collections.abc import Sequence, Collection, Iterable
252+
>>> from collections.abc import Sequence, Collection, Iterable
253+
>>> isinstance([1, 2, 3], Iterable)
254+
True
255+
```
256+
257+
```text
258+
+------------------+----------+------------+----------+
259+
| | Sequence | Collection | Iterable |
260+
+------------------+----------+------------+----------+
261+
| list, range, str | yes | yes | yes |
262+
| dict, set | | yes | yes |
263+
| iter | | | yes |
264+
+------------------+----------+------------+----------+
254265
```
255266

256267
```python
268+
>>> from numbers import Integral, Rational, Real, Complex, Number
257269
>>> isinstance(123, Number)
258270
True
259-
>>> isinstance([1, 2, 3], Iterable)
260-
True
271+
```
272+
273+
```text
274+
+--------------------+----------+----------+------+---------+--------+
275+
| | Integral | Rational | Real | Complex | Number |
276+
+--------------------+----------+----------+------+---------+--------+
277+
| int | yes | yes | yes | yes | yes |
278+
| fractions.Fraction | | yes | yes | yes | yes |
279+
| float | | | yes | yes | yes |
280+
| complex | | | | yes | yes |
281+
+--------------------+----------+----------+------+---------+--------+
261282
```
262283

263284

@@ -408,15 +429,16 @@ Format
408429
Numbers
409430
-------
410431
```python
411-
<int> = int(<float/str/bool>) # Or: math.floor(<float>)
412-
<float> = float(<int/str/bool>)
413-
<complex> = complex(real=0, imag=0) # Or: <real> + <real>j
432+
<int> = int(<float/str/bool>) # Or: math.floor(<float>)
433+
<float> = float(<int/str/bool>)
434+
<complex> = complex(real=0, imag=0) # Or: <real> + <real>j
435+
<Fraction> = fractions.Fraction(numerator=0, denominator=1)
414436
```
415437
* **`'int(<str>)'` and `'float(<str>)'` raise ValueError on malformed strings.**
416438

417439
### Basic Functions
418440
```python
419-
<num> = pow(<num>, <num>) # Or: <num> ** <num>
441+
<num> = pow(<num>, <num>) # Or: <num> ** <num>
420442
<real> = abs(<num>)
421443
<int> = round(<real>)
422444
<real> = round(<real>, ±ndigits)

index.html

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -365,14 +365,31 @@ <h4 id="sometypesdonothavebuiltinnamessotheymustbeimported">Some types do not ha
365365
</code></pre>
366366
<h3 id="abc">ABC</h3>
367367
<p><strong>An abstract base class introduces virtual subclasses, that don’t inherit from it but are still recognized by isinstance() and issubclass().</strong></p>
368-
<pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> numbers <span class="hljs-keyword">import</span> Integral, Rational, Real, Complex, Number
369-
<span class="hljs-keyword">from</span> collections.abc <span class="hljs-keyword">import</span> Sequence, Collection, Iterable
370-
</code></pre>
371-
<pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span>isinstance(<span class="hljs-number">123</span>, Number)
372-
<span class="hljs-keyword">True</span>
368+
<pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> collections.abc <span class="hljs-keyword">import</span> Sequence, Collection, Iterable
373369
<span class="hljs-meta">&gt;&gt;&gt; </span>isinstance([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>], Iterable)
374370
<span class="hljs-keyword">True</span>
375371
</code></pre>
372+
<pre><code class="text language-text">┏━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━┓
373+
┃ │ Sequence │ Collection │ Iterable ┃
374+
┠──────────────────┼──────────┼────────────┼──────────┨
375+
┃ list, range, str │ ✓ │ ✓ │ ✓ ┃
376+
┃ dict, set │ │ ✓ │ ✓ ┃
377+
┃ iter │ │ │ ✓ ┃
378+
┗━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━┛
379+
</code></pre>
380+
<pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> numbers <span class="hljs-keyword">import</span> Integral, Rational, Real, Complex, Number
381+
<span class="hljs-meta">&gt;&gt;&gt; </span>isinstance(<span class="hljs-number">123</span>, Number)
382+
<span class="hljs-keyword">True</span>
383+
</code></pre>
384+
<pre><code class="text language-text">┏━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━┯━━━━━━━━━┯━━━━━━━━┓
385+
┃ │ Integral │ Rational │ Real │ Complex │ Number ┃
386+
┠────────────────────┼──────────┼──────────┼──────┼─────────┼────────┨
387+
┃ int │ ✓ │ ✓ │ ✓ │ ✓ │ ✓ ┃
388+
┃ fractions.Fraction │ │ ✓ │ ✓ │ ✓ │ ✓ ┃
389+
┃ float │ │ │ ✓ │ ✓ │ ✓ ┃
390+
┃ complex │ │ │ │ ✓ │ ✓ ┃
391+
┗━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━┷━━━━━━━━━┷━━━━━━━━┛
392+
</code></pre>
376393
<h2 id="string"><a href="#string" name="string">#</a>String</h2>
377394
<pre><code class="python language-python hljs">&lt;str&gt; = &lt;str&gt;.strip() <span class="hljs-comment"># Strips all whitespace characters from both ends.</span>
378395
&lt;str&gt; = &lt;str&gt;.strip(<span class="hljs-string">'&lt;chars&gt;'</span>) <span class="hljs-comment"># Strips all passed characters from both ends.</span>
@@ -480,15 +497,16 @@ <h4 id="inttypes">Int types:</h4>
480497
{<span class="hljs-number">90</span>:b} <span class="hljs-comment"># '1011010'</span>
481498
</code></pre>
482499
<h2 id="numbers"><a href="#numbers" name="numbers">#</a>Numbers</h2>
483-
<pre><code class="python language-python hljs">&lt;int&gt; = int(&lt;float/str/bool&gt;) <span class="hljs-comment"># Or: math.floor(&lt;float&gt;)</span>
484-
&lt;float&gt; = float(&lt;int/str/bool&gt;)
485-
&lt;complex&gt; = complex(real=<span class="hljs-number">0</span>, imag=<span class="hljs-number">0</span>) <span class="hljs-comment"># Or: &lt;real&gt; + &lt;real&gt;j</span>
500+
<pre><code class="python language-python hljs">&lt;int&gt; = int(&lt;float/str/bool&gt;) <span class="hljs-comment"># Or: math.floor(&lt;float&gt;)</span>
501+
&lt;float&gt; = float(&lt;int/str/bool&gt;)
502+
&lt;complex&gt; = complex(real=<span class="hljs-number">0</span>, imag=<span class="hljs-number">0</span>) <span class="hljs-comment"># Or: &lt;real&gt; + &lt;real&gt;j</span>
503+
&lt;Fraction&gt; = fractions.Fraction(numerator=<span class="hljs-number">0</span>, denominator=<span class="hljs-number">1</span>)
486504
</code></pre>
487505
<ul>
488506
<li><strong><code class="python hljs"><span class="hljs-string">'int(&lt;str&gt;)'</span></code> and <code class="python hljs"><span class="hljs-string">'float(&lt;str&gt;)'</span></code> raise ValueError on malformed strings.</strong></li>
489507
</ul>
490508
<h3 id="basicfunctions">Basic Functions</h3>
491-
<pre><code class="python language-python hljs">&lt;num&gt; = pow(&lt;num&gt;, &lt;num&gt;) <span class="hljs-comment"># Or: &lt;num&gt; ** &lt;num&gt;</span>
509+
<pre><code class="python language-python hljs">&lt;num&gt; = pow(&lt;num&gt;, &lt;num&gt;) <span class="hljs-comment"># Or: &lt;num&gt; ** &lt;num&gt;</span>
492510
&lt;real&gt; = abs(&lt;num&gt;)
493511
&lt;int&gt; = round(&lt;real&gt;)
494512
&lt;real&gt; = round(&lt;real&gt;, ±ndigits)

parse.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,44 @@ const DIAGRAM_2_B =
7474
'┃ str │ ┃\n' +
7575
'┗━━━━━━━━━┷━━━━━━━━━━━━━┛\n';
7676

77+
const DIAGRAM_3_A =
78+
'+------------------+----------+------------+----------+\n' +
79+
'| | Sequence | Collection | Iterable |\n' +
80+
'+------------------+----------+------------+----------+\n' +
81+
'| list, range, str | yes | yes | yes |\n' +
82+
'| dict, set | | yes | yes |\n' +
83+
'| iter | | | yes |\n' +
84+
'+------------------+----------+------------+----------+\n';
85+
86+
const DIAGRAM_3_B =
87+
'┏━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━┓\n' +
88+
'┃ │ Sequence │ Collection │ Iterable ┃\n' +
89+
'┠──────────────────┼──────────┼────────────┼──────────┨\n' +
90+
'┃ list, range, str │ ✓ │ ✓ │ ✓ ┃\n' +
91+
'┃ dict, set │ │ ✓ │ ✓ ┃\n' +
92+
'┃ iter │ │ │ ✓ ┃\n' +
93+
'┗━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━┛\n';
94+
95+
const DIAGRAM_4_A =
96+
'+--------------------+----------+----------+------+---------+--------+\n' +
97+
'| | Integral | Rational | Real | Complex | Number |\n' +
98+
'+--------------------+----------+----------+------+---------+--------+\n' +
99+
'| int | yes | yes | yes | yes | yes |\n' +
100+
'| fractions.Fraction | | yes | yes | yes | yes |\n' +
101+
'| float | | | yes | yes | yes |\n' +
102+
'| complex | | | | yes | yes |\n' +
103+
'+--------------------+----------+----------+------+---------+--------+\n';
104+
105+
const DIAGRAM_4_B =
106+
'┏━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━┯━━━━━━━━━┯━━━━━━━━┓\n' +
107+
'┃ │ Integral │ Rational │ Real │ Complex │ Number ┃\n' +
108+
'┠────────────────────┼──────────┼──────────┼──────┼─────────┼────────┨\n' +
109+
'┃ int │ ✓ │ ✓ │ ✓ │ ✓ │ ✓ ┃\n' +
110+
'┃ fractions.Fraction │ │ ✓ │ ✓ │ ✓ │ ✓ ┃\n' +
111+
'┃ float │ │ │ ✓ │ ✓ │ ✓ ┃\n' +
112+
'┃ complex │ │ │ │ ✓ │ ✓ ┃\n' +
113+
'┗━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━┷━━━━━━━━━┷━━━━━━━━┛\n';
114+
77115

78116
function main() {
79117
const html = getMd();
@@ -102,7 +140,10 @@ function getMd() {
102140

103141
function switchClassDiagrams(readme) {
104142
readme = readme.replace(DIAGRAM_1_A, DIAGRAM_1_B)
105-
return readme.replace(DIAGRAM_2_A, DIAGRAM_2_B)
143+
readme = readme.replace(DIAGRAM_2_A, DIAGRAM_2_B)
144+
readme = readme.replace(DIAGRAM_3_A, DIAGRAM_3_B)
145+
readme = readme.replace(DIAGRAM_4_A, DIAGRAM_4_B)
146+
return readme
106147
}
107148

108149
function modifyPage() {

web/script_2.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,53 @@ const DIAGRAM_2_B =
4242
'┃ str │ ┃\n' +
4343
'┗━━━━━━━━━┷━━━━━━━━━━━━━┛\n';
4444

45+
const DIAGRAM_3_A =
46+
'+------------------+----------+------------+----------+\n' +
47+
'| | Sequence | Collection | Iterable |\n' +
48+
'+------------------+----------+------------+----------+\n' +
49+
'| list, range, str | yes | yes | yes |\n' +
50+
'| dict, set | | yes | yes |\n' +
51+
'| iter | | | yes |\n' +
52+
'+------------------+----------+------------+----------+\n';
53+
54+
const DIAGRAM_3_B =
55+
'┏━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━┓\n' +
56+
'┃ │ Sequence │ Collection │ Iterable ┃\n' +
57+
'┠──────────────────┼──────────┼────────────┼──────────┨\n' +
58+
'┃ list, range, str │ ✓ │ ✓ │ ✓ ┃\n' +
59+
'┃ dict, set │ │ ✓ │ ✓ ┃\n' +
60+
'┃ iter │ │ │ ✓ ┃\n' +
61+
'┗━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━┛\n';
62+
63+
const DIAGRAM_4_A =
64+
'+--------------------+----------+----------+------+---------+--------+\n' +
65+
'| | Integral | Rational | Real | Complex | Number |\n' +
66+
'+--------------------+----------+----------+------+---------+--------+\n' +
67+
'| int | yes | yes | yes | yes | yes |\n' +
68+
'| fractions.Fraction | | yes | yes | yes | yes |\n' +
69+
'| float | | | yes | yes | yes |\n' +
70+
'| complex | | | | yes | yes |\n' +
71+
'+--------------------+----------+----------+------+---------+--------+\n';
72+
73+
const DIAGRAM_4_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+
'┗━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━┷━━━━━━━━━┷━━━━━━━━┛\n';
82+
4583

4684
// isFontAvailable:
4785
(function(d){function c(c){b.style.fontFamily=c;e.appendChild(b);f=b.clientWidth;e.removeChild(b);return f}var f,e=d.body,b=d.createElement("span");b.innerHTML=Array(100).join("wi");b.style.cssText=["position:absolute","width:auto","font-size:128px","left:-99999px"].join(" !important;");var g=c("monospace"),h=c("serif"),k=c("sans-serif");window.isFontAvailable=function(b){return g!==c(b+",monospace")||k!==c(b+",sans-serif")||h!==c(b+",serif")}})(document);
4886

4987
if (!isFontAvailable('Menlo')) {
5088
$(`code:contains(${DIAGRAM_1_B})`).html(DIAGRAM_1_A);
5189
$(`code:contains(${DIAGRAM_2_B})`).html(DIAGRAM_2_A);
90+
$(`code:contains(${DIAGRAM_3_B})`).html(DIAGRAM_3_A);
91+
$(`code:contains(${DIAGRAM_4_B})`).html(DIAGRAM_4_A);
5292
// var htmlString = $('code:contains(ᴺᴱᵂ)').html().replace(/ᴺᴱᵂ/g, '');
5393
// $('code:contains(ᴺᴱᵂ)').html(htmlString);
5494
}

0 commit comments

Comments
 (0)