rapydscript-ns 0.9.3 → 0.9.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.agignore +1 -1
- package/.github/workflows/ci.yml +38 -38
- package/=template.pyj +5 -5
- package/CHANGELOG.md +18 -0
- package/HACKING.md +103 -103
- package/LICENSE +24 -24
- package/PYTHON_GAPS.md +52 -142
- package/README.md +51 -21
- package/TODO.md +1 -26
- package/add-toc-to-readme +2 -2
- package/bin/export +75 -75
- package/bin/rapydscript +0 -0
- package/bin/web-repl-export +102 -102
- package/build +2 -2
- package/language-service/index.js +88 -36
- package/package.json +1 -1
- package/publish.py +37 -37
- package/release/baselib-plain-pretty.js +157 -31
- package/release/baselib-plain-ugly.js +5 -5
- package/release/compiler.js +724 -426
- package/release/signatures.json +29 -29
- package/session.vim +4 -4
- package/setup.cfg +2 -2
- package/src/ast.pyj +7 -0
- package/src/baselib-containers.pyj +41 -4
- package/src/baselib-errors.pyj +4 -3
- package/src/baselib-internal.pyj +47 -18
- package/src/baselib-str.pyj +16 -3
- package/src/compiler.pyj +36 -36
- package/src/errors.pyj +30 -30
- package/src/lib/aes.pyj +646 -646
- package/src/lib/collections.pyj +227 -3
- package/src/lib/copy.pyj +120 -120
- package/src/lib/elementmaker.pyj +83 -83
- package/src/lib/encodings.pyj +126 -126
- package/src/lib/gettext.pyj +569 -569
- package/src/lib/itertools.pyj +580 -580
- package/src/lib/math.pyj +193 -193
- package/src/lib/operator.pyj +11 -11
- package/src/lib/pprint.pyj +455 -0
- package/src/lib/random.pyj +118 -118
- package/src/lib/react.pyj +74 -74
- package/src/lib/statistics.pyj +0 -0
- package/src/lib/traceback.pyj +63 -63
- package/src/lib/uuid.pyj +77 -77
- package/src/monaco-language-service/completions.js +21 -14
- package/src/monaco-language-service/diagnostics.js +2 -2
- package/src/monaco-language-service/dts.js +58 -15
- package/src/monaco-language-service/package.json +3 -0
- package/src/output/classes.pyj +25 -2
- package/src/output/codegen.pyj +4 -1
- package/src/output/comments.pyj +45 -45
- package/src/output/exceptions.pyj +201 -201
- package/src/output/jsx.pyj +164 -164
- package/src/output/treeshake.pyj +182 -182
- package/src/output/utils.pyj +72 -72
- package/src/parse.pyj +42 -7
- package/src/string_interpolation.pyj +72 -72
- package/src/tokenizer.pyj +18 -2
- package/src/unicode_aliases.pyj +576 -576
- package/src/utils.pyj +192 -192
- package/test/_import_one.pyj +37 -37
- package/test/_import_two/__init__.pyj +11 -11
- package/test/_import_two/level2/deep.pyj +4 -4
- package/test/_import_two/other.pyj +6 -6
- package/test/_import_two/sub.pyj +13 -13
- package/test/aes_vectors.pyj +421 -421
- package/test/annotations.pyj +80 -80
- package/test/baselib.pyj +23 -0
- package/test/chainmap.pyj +185 -0
- package/test/dataclasses.pyj +3 -4
- package/test/decorators.pyj +77 -77
- package/test/docstrings.pyj +39 -39
- package/test/elementmaker_test.pyj +45 -45
- package/test/enum.pyj +1 -1
- package/test/functions.pyj +151 -151
- package/test/generators.pyj +41 -41
- package/test/generic.pyj +370 -370
- package/test/internationalization.pyj +73 -73
- package/test/lint.pyj +164 -164
- package/test/loops.pyj +85 -85
- package/test/numpy.pyj +734 -734
- package/test/pprint.pyj +232 -0
- package/test/python_features.pyj +1 -1
- package/test/repl.pyj +121 -121
- package/test/scoped_flags.pyj +76 -76
- package/test/statistics.pyj +224 -0
- package/test/str.pyj +4 -4
- package/test/unit/index.js +455 -0
- package/test/unit/language-service-completions.js +2 -0
- package/test/unit/language-service-dts.js +113 -0
- package/test/unit/language-service-hover.js +455 -455
- package/test/unit/language-service.js +135 -2
- package/test/unit/web-repl.js +349 -1
- package/tools/compiler.d.ts +367 -367
- package/tools/completer.js +131 -131
- package/tools/export.js +4 -2
- package/tools/gettext.js +185 -185
- package/tools/ini.js +65 -65
- package/tools/msgfmt.js +187 -187
- package/tools/repl.js +223 -223
- package/tools/test.js +118 -118
- package/tools/utils.js +141 -128
- package/tools/web_repl.js +95 -95
- package/try +41 -41
- package/web-repl/env.js +196 -196
- package/web-repl/index.html +163 -163
- package/web-repl/prism.css +139 -139
- package/web-repl/prism.js +113 -113
- package/web-repl/rapydscript.js +228 -226
- package/web-repl/sha1.js +25 -25
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
# globals: assrt
|
|
2
|
+
# vim:fileencoding=utf-8
|
|
3
|
+
#
|
|
4
|
+
# statistics.pyj
|
|
5
|
+
# Tests for the statistics standard library module.
|
|
6
|
+
|
|
7
|
+
from statistics import (
|
|
8
|
+
StatisticsError,
|
|
9
|
+
mean, fmean, geometric_mean, harmonic_mean,
|
|
10
|
+
median, median_low, median_high, median_grouped, mode, multimode,
|
|
11
|
+
variance, pvariance, stdev, pstdev,
|
|
12
|
+
quantiles,
|
|
13
|
+
covariance, correlation, linear_regression,
|
|
14
|
+
NormalDist,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
ae = assrt.equal
|
|
18
|
+
ade = assrt.deepEqual
|
|
19
|
+
ok = assrt.ok
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def close(a, b, tol=1e-9):
|
|
23
|
+
return Math.abs(a - b) < tol
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
# ── 1. mean ───────────────────────────────────────────────────────────────────
|
|
27
|
+
|
|
28
|
+
ae(mean([1, 2, 3, 4]), 2.5)
|
|
29
|
+
ae(mean([1, 2, 3]), 2)
|
|
30
|
+
ae(mean([5]), 5)
|
|
31
|
+
ae(mean([-1, 1]), 0)
|
|
32
|
+
ok(close(mean([1, 2, 4]), 7 / 3))
|
|
33
|
+
# any iterable works
|
|
34
|
+
ae(mean(x for x in [2, 4, 6]), 4)
|
|
35
|
+
|
|
36
|
+
# ── 2. fmean ──────────────────────────────────────────────────────────────────
|
|
37
|
+
|
|
38
|
+
ae(fmean([1, 2, 3, 4]), 2.5)
|
|
39
|
+
ok(close(fmean([3.5, 4.0, 5.25]), 4.25))
|
|
40
|
+
# weighted
|
|
41
|
+
ok(close(fmean([10, 20, 30], [1, 1, 1]), 20))
|
|
42
|
+
ok(close(fmean([1, 2, 3, 4], [4, 3, 2, 1]), 2.0))
|
|
43
|
+
|
|
44
|
+
# ── 3. geometric_mean ─────────────────────────────────────────────────────────
|
|
45
|
+
|
|
46
|
+
ok(close(geometric_mean([4, 1, 1 / 32]), 0.5))
|
|
47
|
+
ok(close(geometric_mean([2, 8]), 4.0))
|
|
48
|
+
ok(close(geometric_mean([10]), 10.0))
|
|
49
|
+
|
|
50
|
+
# ── 4. harmonic_mean ──────────────────────────────────────────────────────────
|
|
51
|
+
|
|
52
|
+
ok(close(harmonic_mean([40, 60]), 48.0))
|
|
53
|
+
ok(close(harmonic_mean([1, 2, 4]), 12 / 7))
|
|
54
|
+
ae(harmonic_mean([0, 5, 10]), 0) # a zero forces the result to zero
|
|
55
|
+
# weighted
|
|
56
|
+
ok(close(harmonic_mean([40, 60], [5, 30]), 56.0))
|
|
57
|
+
|
|
58
|
+
# ── 5. median family ──────────────────────────────────────────────────────────
|
|
59
|
+
|
|
60
|
+
ae(median([1, 3, 5]), 3)
|
|
61
|
+
ae(median([1, 3, 5, 7]), 4)
|
|
62
|
+
ae(median([5, 1, 3]), 3) # unsorted input
|
|
63
|
+
ae(median_low([1, 3, 5, 7]), 3)
|
|
64
|
+
ae(median_low([1, 3, 5]), 3)
|
|
65
|
+
ae(median_high([1, 3, 5, 7]), 5)
|
|
66
|
+
ae(median_high([1, 3, 5]), 3)
|
|
67
|
+
ae(median_grouped([52, 52, 53, 54]), 52.5)
|
|
68
|
+
ok(close(median_grouped([1, 2, 2, 3, 4, 4, 4, 4, 4, 5]), 3.7))
|
|
69
|
+
ok(close(median_grouped([1, 3, 3, 5, 7], 2), 3.5))
|
|
70
|
+
|
|
71
|
+
# ── 6. mode / multimode ───────────────────────────────────────────────────────
|
|
72
|
+
|
|
73
|
+
ae(mode([1, 1, 2, 3, 3, 3, 4]), 3)
|
|
74
|
+
ae(mode([1, 1, 1, 2, 2]), 1)
|
|
75
|
+
ae(mode(['red', 'blue', 'red', 'green']), 'red')
|
|
76
|
+
ae(mode([7]), 7)
|
|
77
|
+
# ties: first mode encountered wins
|
|
78
|
+
ae(mode([4, 4, 5, 5]), 4)
|
|
79
|
+
ade(multimode([1, 1, 2, 2, 3]), [1, 2])
|
|
80
|
+
ade(multimode([1, 2, 3]), [1, 2, 3])
|
|
81
|
+
ade(multimode([]), [])
|
|
82
|
+
ade(multimode(['a', 'a', 'b', 'c', 'c']), ['a', 'c'])
|
|
83
|
+
|
|
84
|
+
# ── 7. variance / stdev ───────────────────────────────────────────────────────
|
|
85
|
+
|
|
86
|
+
ae(variance([1, 2, 3, 4, 5]), 2.5)
|
|
87
|
+
ae(pvariance([1, 2, 3, 4, 5]), 2.0)
|
|
88
|
+
ok(close(stdev([1, 2, 3, 4, 5]), Math.sqrt(2.5)))
|
|
89
|
+
ok(close(pstdev([1, 2, 3, 4, 5]), Math.sqrt(2.0)))
|
|
90
|
+
ok(close(variance([2.75, 1.75, 1.25, 0.25, 0.5, 1.25, 3.5]), 1.3720238095238095))
|
|
91
|
+
ok(close(pvariance([0.0, 0.25, 0.25, 1.25, 1.5, 1.75, 2.75, 3.25]), 1.25))
|
|
92
|
+
# explicit centre argument
|
|
93
|
+
ae(variance([1, 2, 3, 4, 5], 3), 2.5)
|
|
94
|
+
ae(pvariance([1, 2, 3, 4, 5], 3), 2.0)
|
|
95
|
+
|
|
96
|
+
# ── 8. quantiles ──────────────────────────────────────────────────────────────
|
|
97
|
+
|
|
98
|
+
ade(quantiles([1, 2, 3, 4]), [1.25, 2.5, 3.75])
|
|
99
|
+
_q = quantiles([1, 2, 3, 4, 5, 6, 7, 8, 9])
|
|
100
|
+
ae(_q.length, 3)
|
|
101
|
+
_qi = quantiles([1, 2, 3, 4, 5], n=4, method='inclusive')
|
|
102
|
+
ae(_qi.length, 3)
|
|
103
|
+
ade(quantiles([0, 100], n=2, method='inclusive'), [50])
|
|
104
|
+
# deciles
|
|
105
|
+
ae(quantiles([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], n=10).length, 9)
|
|
106
|
+
|
|
107
|
+
# ── 9. covariance / correlation / linear_regression ──────────────────────────
|
|
108
|
+
|
|
109
|
+
_cx = [1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
110
|
+
_cy = [1, 2, 3, 1, 2, 3, 1, 2, 3]
|
|
111
|
+
ok(close(covariance(_cx, _cy), 0.75))
|
|
112
|
+
|
|
113
|
+
_px = [1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
114
|
+
_py = [9, 8, 7, 6, 5, 4, 3, 2, 1]
|
|
115
|
+
ok(close(correlation(_px, _px), 1.0))
|
|
116
|
+
ok(close(correlation(_px, _py), -1.0))
|
|
117
|
+
# ranked (Spearman) correlation handles a monotonic non-linear relation
|
|
118
|
+
ok(close(correlation([1, 2, 3, 4, 5], [1, 4, 9, 16, 25], method='ranked'), 1.0))
|
|
119
|
+
|
|
120
|
+
_lr = linear_regression([1, 2, 3, 4, 5], [2, 4, 6, 8, 10])
|
|
121
|
+
ok(close(_lr.slope, 2.0))
|
|
122
|
+
ok(close(_lr.intercept, 0.0))
|
|
123
|
+
_lr2 = linear_regression([0, 1, 2, 3], [1, 3, 5, 7])
|
|
124
|
+
ok(close(_lr2.slope, 2.0))
|
|
125
|
+
ok(close(_lr2.intercept, 1.0))
|
|
126
|
+
_lrp = linear_regression([1, 2, 3], [2, 4, 6], proportional=True)
|
|
127
|
+
ok(close(_lrp.slope, 2.0))
|
|
128
|
+
ae(_lrp.intercept, 0.0)
|
|
129
|
+
|
|
130
|
+
# ── 10. NormalDist — basics ───────────────────────────────────────────────────
|
|
131
|
+
|
|
132
|
+
_nd = NormalDist(100, 15)
|
|
133
|
+
ae(_nd.mean, 100)
|
|
134
|
+
ae(_nd.median, 100)
|
|
135
|
+
ae(_nd.mode, 100)
|
|
136
|
+
ae(_nd.stdev, 15)
|
|
137
|
+
ae(_nd.variance, 225)
|
|
138
|
+
ae(NormalDist().mean, 0)
|
|
139
|
+
ae(NormalDist().stdev, 1)
|
|
140
|
+
|
|
141
|
+
# ── 11. NormalDist — pdf / cdf / inv_cdf ─────────────────────────────────────
|
|
142
|
+
|
|
143
|
+
ok(close(_nd.cdf(100), 0.5, 1e-6))
|
|
144
|
+
ok(close(_nd.cdf(115), 0.8413447460685429, 1e-5))
|
|
145
|
+
ok(close(_nd.cdf(85), 0.15865525393145707, 1e-5))
|
|
146
|
+
ae(_nd.inv_cdf(0.5), 100) # exact: q = 0 ⇒ x = mu
|
|
147
|
+
ok(close(_nd.inv_cdf(0.8413447460685429), 115, 1e-4))
|
|
148
|
+
ok(close(_nd.pdf(100), 1 / Math.sqrt(2 * Math.PI * 225)))
|
|
149
|
+
# cdf and inv_cdf are inverses
|
|
150
|
+
ok(close(_nd.cdf(_nd.inv_cdf(0.25)), 0.25, 1e-5))
|
|
151
|
+
|
|
152
|
+
# ── 12. NormalDist — zscore / quantiles / from_samples ───────────────────────
|
|
153
|
+
|
|
154
|
+
ae(_nd.zscore(115), 1.0)
|
|
155
|
+
ae(_nd.zscore(70), -2.0)
|
|
156
|
+
_ndq = _nd.quantiles(4)
|
|
157
|
+
ae(_ndq.length, 3)
|
|
158
|
+
ok(close(_ndq[1], 100)) # the median cut point
|
|
159
|
+
|
|
160
|
+
_fs = NormalDist.from_samples([1, 2, 3, 4, 5])
|
|
161
|
+
ok(close(_fs.mean, 3.0))
|
|
162
|
+
ok(close(_fs.stdev, Math.sqrt(2.5)))
|
|
163
|
+
|
|
164
|
+
# ── 13. NormalDist — overlap / samples / arithmetic ──────────────────────────
|
|
165
|
+
|
|
166
|
+
ok(close(NormalDist(0, 1).overlap(NormalDist(0, 1)), 1.0, 1e-5))
|
|
167
|
+
_ov = NormalDist(0, 1).overlap(NormalDist(1, 1))
|
|
168
|
+
ok(_ov > 0.5 and _ov < 0.7) # ~0.6171
|
|
169
|
+
|
|
170
|
+
_samp = _nd.samples(50, seed=42)
|
|
171
|
+
ae(_samp.length, 50)
|
|
172
|
+
# deterministic with a fixed seed
|
|
173
|
+
_samp2 = _nd.samples(50, seed=42)
|
|
174
|
+
ade(_samp, _samp2)
|
|
175
|
+
|
|
176
|
+
# arithmetic combines distributions / scalars
|
|
177
|
+
_sum = NormalDist(2, 3).__add__(NormalDist(4, 4))
|
|
178
|
+
ae(_sum.mean, 6)
|
|
179
|
+
ae(_sum.stdev, 5) # hypot(3, 4)
|
|
180
|
+
_shift = NormalDist(10, 2).__add__(5)
|
|
181
|
+
ae(_shift.mean, 15)
|
|
182
|
+
ae(_shift.stdev, 2)
|
|
183
|
+
_scaled = NormalDist(10, 2).__mul__(3)
|
|
184
|
+
ae(_scaled.mean, 30)
|
|
185
|
+
ae(_scaled.stdev, 6)
|
|
186
|
+
|
|
187
|
+
ok(NormalDist(1, 2).__eq__(NormalDist(1, 2)))
|
|
188
|
+
ok(not NormalDist(1, 2).__eq__(NormalDist(1, 3)))
|
|
189
|
+
|
|
190
|
+
# ── 14. error handling ────────────────────────────────────────────────────────
|
|
191
|
+
|
|
192
|
+
def raises(fn):
|
|
193
|
+
try:
|
|
194
|
+
fn()
|
|
195
|
+
except StatisticsError:
|
|
196
|
+
return True
|
|
197
|
+
return False
|
|
198
|
+
|
|
199
|
+
ok(raises(def(): return mean([]);))
|
|
200
|
+
ok(raises(def(): return median([]);))
|
|
201
|
+
ok(raises(def(): return mode([]);))
|
|
202
|
+
ok(raises(def(): return variance([1]);))
|
|
203
|
+
ok(raises(def(): return pvariance([]);))
|
|
204
|
+
ok(raises(def(): return geometric_mean([1, -2, 3]);))
|
|
205
|
+
ok(raises(def(): return harmonic_mean([1, -2]);))
|
|
206
|
+
ok(raises(def(): return covariance([1, 2], [1, 2, 3]);))
|
|
207
|
+
ok(raises(def(): return correlation([1, 1, 1], [2, 3, 4]);))
|
|
208
|
+
ok(raises(def(): return linear_regression([1, 1, 1], [2, 3, 4]);))
|
|
209
|
+
|
|
210
|
+
# inv_cdf rejects out-of-range probabilities
|
|
211
|
+
ok(raises(def(): return NormalDist(0, 1).inv_cdf(0);))
|
|
212
|
+
ok(raises(def(): return NormalDist(0, 1).inv_cdf(1.5);))
|
|
213
|
+
# negative sigma is rejected
|
|
214
|
+
ok(raises(def(): return NormalDist(0, -1);))
|
|
215
|
+
|
|
216
|
+
# StatisticsError is a subclass of ValueError
|
|
217
|
+
_is_value_error = False
|
|
218
|
+
try:
|
|
219
|
+
mean([])
|
|
220
|
+
except ValueError:
|
|
221
|
+
_is_value_error = True
|
|
222
|
+
ok(_is_value_error, 'StatisticsError should be catchable as ValueError')
|
|
223
|
+
|
|
224
|
+
print('statistics: all tests passed')
|
package/test/str.pyj
CHANGED
|
@@ -196,11 +196,11 @@ for f in (str, repr):
|
|
|
196
196
|
ae(f(False), 'False')
|
|
197
197
|
ae(f(None), 'None')
|
|
198
198
|
ae(f(1), '1')
|
|
199
|
-
ae(f([1,'2']),
|
|
200
|
-
ae(f({1:[1, '2']}), '
|
|
201
|
-
ae(f({1:'a', 2:'b'}), '
|
|
199
|
+
ae(f([1,'2']), "[1, '2']")
|
|
200
|
+
ae(f({1:[1, '2']}), "{'1': [1, '2']}")
|
|
201
|
+
ae(f({1:'a', 2:'b'}), "{'1': 'a', '2': 'b'}")
|
|
202
202
|
ae(str('a'), 'a')
|
|
203
|
-
ae(repr('a'), '
|
|
203
|
+
ae(repr('a'), "'a'")
|
|
204
204
|
|
|
205
205
|
bytes = list(range(256))
|
|
206
206
|
assrt.deepEqual(bytes, list(encodings.base64decode(encodings.base64encode(bytes))))
|