-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathnumber_tower.py
More file actions
62 lines (57 loc) · 1.55 KB
/
number_tower.py
File metadata and controls
62 lines (57 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from __future__ import print_function
import numbers
import warnings
import fractions
import decimal
warnings.simplefilter("error", DeprecationWarning)
VALUES = (
("int", 123),
("float", 1.5),
("complex", complex(0, 1.5)),
("decimal.Decimal", decimal.Decimal("1.1")),
("fractions.Fraction", fractions.Fraction(1, 7)),
("bytes", b"123"),
("str", "123"),
)
TYPES = (
("int", int),
("float", float),
("complex", complex),
("decimal.Decimal", decimal.Decimal),
("fractions.Fraction", fractions.Fraction),
)
for type_descr, num_type in TYPES:
accepted = []
rejected = []
for value_descr, value in VALUES:
try:
num_type(value)
except TypeError:
rejected.append(value_descr)
else:
accepted.append(value_descr)
if accepted:
print("``%s(obj)`` accept:" % type_descr)
print()
for name in accepted:
print("* ``%s``" % name)
print()
if rejected:
print("``%s(obj)`` reject:" % type_descr)
print()
for name in rejected:
print("* ``%s``" % name)
print()
print()
for tower_name, tower_type in (
("Number", numbers.Number),
("Complex", numbers.Complex),
("Real", numbers.Real),
("Rational", numbers.Rational),
("Integral", numbers.Integral),
):
subclasses = []
for type_descr, num_type in TYPES:
if issubclass(num_type, tower_type):
subclasses.append(type_descr)
print("%s subclasses: %s" % (tower_name, ", ".join(subclasses)))