-
Notifications
You must be signed in to change notification settings - Fork 282
/
test_formats.py
108 lines (91 loc) · 2.66 KB
/
test_formats.py
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import doctest
import re
import datascience as ds
from datascience import formats
def assert_equal(string1, string2):
string1, string2 = str(string1), str(string2)
whitespace = re.compile('\s')
purify = lambda s: whitespace.sub('', s)
assert purify(string1) == purify(string2), "\n%s\n!=\n%s" % (string1, string2)
def test_doctests():
results = doctest.testmod(formats, optionflags=doctest.NORMALIZE_WHITESPACE)
assert results.failed == 0
def test_default_format():
fmt = ds.default_formatter.format_value
assert_equal(fmt(1.23456789), '1.23457')
assert_equal(fmt(123456789), '123456789')
assert_equal(fmt(123456789**5), '28679718602997181072337614380936720482949')
assert_equal(fmt(123.456789**5), '2.86797e+10')
assert_equal(fmt(True), 'True')
assert_equal(fmt(False), 'False')
assert_equal(fmt('hello'), 'hello')
assert_equal(fmt((1, 2)), '(1, 2)')
def test_number_format():
for fmt in [ds.NumberFormatter(2), ds.NumberFormatter]:
us = ['1,000', '12,000']
vs = ['1,000', '12,000.346']
t = ds.Table([us, vs], ['u', 'v'])
t.set_format(['u', 'v'], fmt)
assert_equal(t, """
u | v
1,000 | 1,000.00
12,000 | 12,000.35
""")
def test_currency_format():
vs = ['$60', '$162.5', '$1,625']
t = ds.Table([vs, vs], ['num', 'str'])
t.set_format('num', ds.CurrencyFormatter('$', int_to_float=True))
assert_equal(t, """
num | str
$60.00 | $60
$162.50 | $162.5
$1,625.00 | $1,625
""")
assert_equal(t.sort('num'), """
num | str
$60.00 | $60
$162.50 | $162.5
$1,625.00 | $1,625
""")
assert_equal(t.sort('str'), """
num | str
$1,625.00 | $1,625
$162.50 | $162.5
$60.00 | $60
""")
def test_currency_format_int():
t = ds.Table([[1, 2, 3]], ['money'])
t.set_format(['money'], ds.CurrencyFormatter)
assert_equal(t, """
money
$1
$2
$3
""")
def test_date_format():
vs = ['2015-07-01 22:39:44.900351']
t = ds.Table([vs], ['time'])
t.set_format('time', ds.DateFormatter("%Y-%m-%d %H:%M:%S.%f"))
assert isinstance(t['time'][0], float)
def test_percent_formatter():
vs = [0.1, 0.11111, 0.199999, 10]
t = ds.Table([vs], ['percent'])
t.set_format('percent', ds.PercentFormatter(1))
assert_equal(t, """
percent
10.0%
11.1%
20.0%
1000.0%
""")
def test_distribution_formatter():
counts = [9, 10, 18, 23]
t = ds.Table().with_column('count', counts)
t.set_format('count', ds.DistributionFormatter)
assert_equal(t, """
count
15.00%
16.67%
30.00%
38.33%
""")