From c2b3a1a761083cabb568f841f5bc53852a4f7ea5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Thu, 27 Jun 2019 18:26:25 +0200 Subject: [PATCH] ABC --- README.md | 38 ++++++++++++++++++++++++++++++-------- index.html | 36 +++++++++++++++++++++++++++--------- parse.js | 43 ++++++++++++++++++++++++++++++++++++++++++- web/script_2.js | 40 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 139 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index f7ac43d..1bd66b9 100644 --- a/README.md +++ b/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() # Or: math.floor() - = float() - = complex(real=0, imag=0) # Or: + j + = int() # Or: math.floor() + = float() + = complex(real=0, imag=0) # Or: + j + = fractions.Fraction(numerator=0, denominator=1) ``` * **`'int()'` and `'float()'` raise ValueError on malformed strings.** ### Basic Functions ```python - = pow(, ) # Or: ** + = pow(, ) # Or: ** = abs() = round() = round(, ±ndigits) diff --git a/index.html b/index.html index 39ede9c..fea4acc 100644 --- a/index.html +++ b/index.html @@ -365,14 +365,31 @@ Point(x=1, y=2

ABC

An abstract base class introduces virtual subclasses, that don’t inherit from it but are still recognized by isinstance() and issubclass().

-
from numbers import Integral, Rational, Real, Complex, Number
-from collections.abc import Sequence, Collection, Iterable
-
-
>>> isinstance(123, Number)
-True
+
>>> from collections.abc import Sequence, Collection, Iterable
 >>> isinstance([1, 2, 3], Iterable)
 True
 
+
┏━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━┓
+┃                  │ Sequence │ Collection │ Iterable ┃
+┠──────────────────┼──────────┼────────────┼──────────┨
+┃ list, range, str │    ✓     │     ✓      │    ✓     ┃
+┃ dict, set        │          │     ✓      │    ✓     ┃
+┃ iter             │          │            │    ✓     ┃
+┗━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━┛
+
+
>>> from numbers import Integral, Rational, Real, Complex, Number
+>>> isinstance(123, Number)
+True
+
+
┏━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━┯━━━━━━━━━┯━━━━━━━━┓
+┃                    │ Integral │ Rational │ Real │ Complex │ Number ┃
+┠────────────────────┼──────────┼──────────┼──────┼─────────┼────────┨
+┃ int                │    ✓     │    ✓     │  ✓   │    ✓    │   ✓    ┃
+┃ fractions.Fraction │          │    ✓     │  ✓   │    ✓    │   ✓    ┃
+┃ float              │          │          │  ✓   │    ✓    │   ✓    ┃
+┃ complex            │          │          │      │    ✓    │   ✓    ┃
+┗━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━┷━━━━━━━━━┷━━━━━━━━┛
+

#String

<str>  = <str>.strip()                       # Strips all whitespace characters from both ends.
 <str>  = <str>.strip('<chars>')              # Strips all passed characters from both ends.
@@ -480,15 +497,16 @@ Point(x=1, y=2
 {90:b}           # '1011010'
 

#Numbers

-
<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

-
<num>  = pow(<num>, <num>)           # Or: <num> ** <num>
+
<num>  = pow(<num>, <num>)            # Or: <num> ** <num>
 <real> = abs(<num>)
 <int>  = round(<real>)
 <real> = round(<real>, ±ndigits)
diff --git a/parse.js b/parse.js
index cb1e860..f9ea021 100755
--- a/parse.js
+++ b/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() {
diff --git a/web/script_2.js b/web/script_2.js
index 91c7cc2..710c31f 100644
--- a/web/script_2.js
+++ b/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);
 }