Browse Source

ABC

pull/36/head
Jure Šorn 5 years ago
parent
commit
c2b3a1a761
4 changed files with 139 additions and 18 deletions
  1. 38
      README.md
  2. 36
      index.html
  3. 43
      parse.js
  4. 40
      web/script_2.js

38
README.md

@ -249,15 +249,36 @@ from types import FunctionType, MethodType, LambdaType, GeneratorType
**An abstract base class introduces virtual subclasses, that don’t inherit from it but are still recognized by isinstance() and issubclass().**
```python
from numbers import Integral, Rational, Real, Complex, Number
from collections.abc import Sequence, Collection, Iterable
>>> from collections.abc import Sequence, Collection, Iterable
>>> isinstance([1, 2, 3], Iterable)
True
```
```text
+------------------+----------+------------+----------+
| | Sequence | Collection | Iterable |
+------------------+----------+------------+----------+
| list, range, str | yes | yes | yes |
| dict, set | | yes | yes |
| iter | | | yes |
+------------------+----------+------------+----------+
```
```python
>>> from numbers import Integral, Rational, Real, Complex, Number
>>> isinstance(123, Number)
True
>>> isinstance([1, 2, 3], Iterable)
True
```
```text
+--------------------+----------+----------+------+---------+--------+
| | Integral | Rational | Real | Complex | Number |
+--------------------+----------+----------+------+---------+--------+
| int | yes | yes | yes | yes | yes |
| fractions.Fraction | | yes | yes | yes | yes |
| float | | | yes | yes | yes |
| complex | | | | yes | yes |
+--------------------+----------+----------+------+---------+--------+
```
@ -408,15 +429,16 @@ Format
Numbers
-------
```python
<int> = int(<float/str/bool>) # Or: math.floor(<float>)
<float> = float(<int/str/bool>)
<complex> = complex(real=0, imag=0) # Or: <real> + <real>j
<int> = int(<float/str/bool>) # Or: math.floor(<float>)
<float> = float(<int/str/bool>)
<complex> = complex(real=0, imag=0) # Or: <real> + <real>j
<Fraction> = fractions.Fraction(numerator=0, denominator=1)
```
* **`'int(<str>)'` and `'float(<str>)'` raise ValueError on malformed strings.**
### Basic Functions
```python
<num> = pow(<num>, <num>) # Or: <num> ** <num>
<num> = pow(<num>, <num>) # Or: <num> ** <num>
<real> = abs(<num>)
<int> = round(<real>)
<real> = round(<real>, ±ndigits)

36
index.html

@ -365,14 +365,31 @@ Point(x=<span class="hljs-number">1</span>, y=<span class="hljs-number">2</span>
</code></pre>
<h3 id="abc">ABC</h3>
<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>
<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
<span class="hljs-keyword">from</span> collections.abc <span class="hljs-keyword">import</span> Sequence, Collection, Iterable
</code></pre>
<pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span>isinstance(<span class="hljs-number">123</span>, Number)
<span class="hljs-keyword">True</span>
<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
<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)
<span class="hljs-keyword">True</span>
</code></pre>
<pre><code class="text language-text">┏━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━┓
┃ │ Sequence │ Collection │ Iterable ┃
┠──────────────────┼──────────┼────────────┼──────────┨
┃ list, range, str │ ✓ │ ✓ │ ✓ ┃
┃ dict, set │ │ ✓ │ ✓ ┃
┃ iter │ │ │ ✓ ┃
┗━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━┛
</code></pre>
<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
<span class="hljs-meta">&gt;&gt;&gt; </span>isinstance(<span class="hljs-number">123</span>, Number)
<span class="hljs-keyword">True</span>
</code></pre>
<pre><code class="text language-text">┏━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━┯━━━━━━━━━┯━━━━━━━━┓
┃ │ Integral │ Rational │ Real │ Complex │ Number ┃
┠────────────────────┼──────────┼──────────┼──────┼─────────┼────────┨
┃ int │ ✓ │ ✓ │ ✓ │ ✓ │ ✓ ┃
┃ fractions.Fraction │ │ ✓ │ ✓ │ ✓ │ ✓ ┃
┃ float │ │ │ ✓ │ ✓ │ ✓ ┃
┃ complex │ │ │ │ ✓ │ ✓ ┃
┗━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━┷━━━━━━━━━┷━━━━━━━━┛
</code></pre>
<h2 id="string"><a href="#string" name="string">#</a>String</h2>
<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>
&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 @@ Point(x=<span class="hljs-number">1</span>, y=<span class="hljs-number">2</span>
{<span class="hljs-number">90</span>:b} <span class="hljs-comment"># '1011010'</span>
</code></pre>
<h2 id="numbers"><a href="#numbers" name="numbers">#</a>Numbers</h2>
<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>
&lt;float&gt; = float(&lt;int/str/bool&gt;)
&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>
<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>
&lt;float&gt; = float(&lt;int/str/bool&gt;)
&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>
&lt;Fraction&gt; = fractions.Fraction(numerator=<span class="hljs-number">0</span>, denominator=<span class="hljs-number">1</span>)
</code></pre>
<ul>
<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>
</ul>
<h3 id="basicfunctions">Basic Functions</h3>
<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>
<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>
&lt;real&gt; = abs(&lt;num&gt;)
&lt;int&gt; = round(&lt;real&gt;)
&lt;real&gt; = round(&lt;real&gt;, ±ndigits)

43
parse.js

@ -74,6 +74,44 @@ const DIAGRAM_2_B =
'┃ str │ ┃\n' +
'┗━━━━━━━━━┷━━━━━━━━━━━━━┛\n';
const DIAGRAM_3_A =
'+------------------+----------+------------+----------+\n' +
'| | Sequence | Collection | Iterable |\n' +
'+------------------+----------+------------+----------+\n' +
'| list, range, str | yes | yes | yes |\n' +
'| dict, set | | yes | yes |\n' +
'| iter | | | yes |\n' +
'+------------------+----------+------------+----------+\n';
const DIAGRAM_3_B =
'┏━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━┓\n' +
'┃ │ Sequence │ Collection │ Iterable ┃\n' +
'┠──────────────────┼──────────┼────────────┼──────────┨\n' +
'┃ list, range, str │ ✓ │ ✓ │ ✓ ┃\n' +
'┃ dict, set │ │ ✓ │ ✓ ┃\n' +
'┃ iter │ │ │ ✓ ┃\n' +
'┗━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━┛\n';
const DIAGRAM_4_A =
'+--------------------+----------+----------+------+---------+--------+\n' +
'| | Integral | Rational | Real | Complex | Number |\n' +
'+--------------------+----------+----------+------+---------+--------+\n' +
'| int | yes | yes | yes | yes | yes |\n' +
'| fractions.Fraction | | yes | yes | yes | yes |\n' +
'| float | | | yes | yes | yes |\n' +
'| complex | | | | yes | yes |\n' +
'+--------------------+----------+----------+------+---------+--------+\n';
const DIAGRAM_4_B =
'┏━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━┯━━━━━━━━━┯━━━━━━━━┓\n' +
'┃ │ Integral │ Rational │ Real │ Complex │ Number ┃\n' +
'┠────────────────────┼──────────┼──────────┼──────┼─────────┼────────┨\n' +
'┃ int │ ✓ │ ✓ │ ✓ │ ✓ │ ✓ ┃\n' +
'┃ fractions.Fraction │ │ ✓ │ ✓ │ ✓ │ ✓ ┃\n' +
'┃ float │ │ │ ✓ │ ✓ │ ✓ ┃\n' +
'┃ complex │ │ │ │ ✓ │ ✓ ┃\n' +
'┗━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━┷━━━━━━━━━┷━━━━━━━━┛\n';
function main() {
const html = getMd();
@ -102,7 +140,10 @@ function getMd() {
function switchClassDiagrams(readme) {
readme = readme.replace(DIAGRAM_1_A, DIAGRAM_1_B)
return readme.replace(DIAGRAM_2_A, DIAGRAM_2_B)
readme = readme.replace(DIAGRAM_2_A, DIAGRAM_2_B)
readme = readme.replace(DIAGRAM_3_A, DIAGRAM_3_B)
readme = readme.replace(DIAGRAM_4_A, DIAGRAM_4_B)
return readme
}
function modifyPage() {

40
web/script_2.js

@ -42,6 +42,44 @@ const DIAGRAM_2_B =
'┃ str │ ┃\n' +
'┗━━━━━━━━━┷━━━━━━━━━━━━━┛\n';
const DIAGRAM_3_A =
'+------------------+----------+------------+----------+\n' +
'| | Sequence | Collection | Iterable |\n' +
'+------------------+----------+------------+----------+\n' +
'| list, range, str | yes | yes | yes |\n' +
'| dict, set | | yes | yes |\n' +
'| iter | | | yes |\n' +
'+------------------+----------+------------+----------+\n';
const DIAGRAM_3_B =
'┏━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━┓\n' +
'┃ │ Sequence │ Collection │ Iterable ┃\n' +
'┠──────────────────┼──────────┼────────────┼──────────┨\n' +
'┃ list, range, str │ ✓ │ ✓ │ ✓ ┃\n' +
'┃ dict, set │ │ ✓ │ ✓ ┃\n' +
'┃ iter │ │ │ ✓ ┃\n' +
'┗━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━┛\n';
const DIAGRAM_4_A =
'+--------------------+----------+----------+------+---------+--------+\n' +
'| | Integral | Rational | Real | Complex | Number |\n' +
'+--------------------+----------+----------+------+---------+--------+\n' +
'| int | yes | yes | yes | yes | yes |\n' +
'| fractions.Fraction | | yes | yes | yes | yes |\n' +
'| float | | | yes | yes | yes |\n' +
'| complex | | | | yes | yes |\n' +
'+--------------------+----------+----------+------+---------+--------+\n';
const DIAGRAM_4_B =
'┏━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━┯━━━━━━━━━┯━━━━━━━━┓\n' +
'┃ │ Integral │ Rational │ Real │ Complex │ Number ┃\n' +
'┠────────────────────┼──────────┼──────────┼──────┼─────────┼────────┨\n' +
'┃ int │ ✓ │ ✓ │ ✓ │ ✓ │ ✓ ┃\n' +
'┃ fractions.Fraction │ │ ✓ │ ✓ │ ✓ │ ✓ ┃\n' +
'┃ float │ │ │ ✓ │ ✓ │ ✓ ┃\n' +
'┃ complex │ │ │ │ ✓ │ ✓ ┃\n' +
'┗━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━┷━━━━━━━━━┷━━━━━━━━┛\n';
// isFontAvailable:
(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);
@ -49,6 +87,8 @@ const DIAGRAM_2_B =
if (!isFontAvailable('Menlo')) {
$(`code:contains(${DIAGRAM_1_B})`).html(DIAGRAM_1_A);
$(`code:contains(${DIAGRAM_2_B})`).html(DIAGRAM_2_A);
$(`code:contains(${DIAGRAM_3_B})`).html(DIAGRAM_3_A);
$(`code:contains(${DIAGRAM_4_B})`).html(DIAGRAM_4_A);
// var htmlString = $('code:contains(ᴺᴱᵂ)').html().replace(/ᴺᴱᵂ/g, '');
// $('code:contains(ᴺᴱᵂ)').html(htmlString);
}

Loading…
Cancel
Save