rapydscript-ns 0.9.3 → 0.9.4

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.
Files changed (98) hide show
  1. package/.agignore +1 -1
  2. package/.github/workflows/ci.yml +38 -38
  3. package/=template.pyj +5 -5
  4. package/CHANGELOG.md +9 -0
  5. package/HACKING.md +103 -103
  6. package/LICENSE +24 -24
  7. package/PYTHON_GAPS.md +48 -116
  8. package/README.md +35 -15
  9. package/TODO.md +1 -26
  10. package/add-toc-to-readme +2 -2
  11. package/bin/export +75 -75
  12. package/bin/rapydscript +0 -0
  13. package/bin/web-repl-export +102 -102
  14. package/build +2 -2
  15. package/language-service/index.js +9 -7
  16. package/package.json +1 -1
  17. package/publish.py +37 -37
  18. package/session.vim +4 -4
  19. package/setup.cfg +2 -2
  20. package/src/ast.pyj +6 -0
  21. package/src/baselib-containers.pyj +23 -1
  22. package/src/baselib-str.pyj +13 -2
  23. package/src/compiler.pyj +36 -36
  24. package/src/errors.pyj +30 -30
  25. package/src/lib/aes.pyj +646 -646
  26. package/src/lib/collections.pyj +227 -3
  27. package/src/lib/copy.pyj +120 -120
  28. package/src/lib/elementmaker.pyj +83 -83
  29. package/src/lib/encodings.pyj +126 -126
  30. package/src/lib/gettext.pyj +569 -569
  31. package/src/lib/itertools.pyj +580 -580
  32. package/src/lib/math.pyj +193 -193
  33. package/src/lib/operator.pyj +11 -11
  34. package/src/lib/pprint.pyj +455 -0
  35. package/src/lib/random.pyj +118 -118
  36. package/src/lib/react.pyj +74 -74
  37. package/src/lib/statistics.pyj +0 -0
  38. package/src/lib/traceback.pyj +63 -63
  39. package/src/lib/uuid.pyj +77 -77
  40. package/src/monaco-language-service/diagnostics.js +2 -2
  41. package/src/monaco-language-service/dts.js +550 -550
  42. package/src/output/codegen.pyj +4 -1
  43. package/src/output/comments.pyj +45 -45
  44. package/src/output/exceptions.pyj +201 -201
  45. package/src/output/jsx.pyj +164 -164
  46. package/src/output/treeshake.pyj +182 -182
  47. package/src/output/utils.pyj +72 -72
  48. package/src/parse.pyj +28 -7
  49. package/src/string_interpolation.pyj +72 -72
  50. package/src/tokenizer.pyj +18 -2
  51. package/src/unicode_aliases.pyj +576 -576
  52. package/src/utils.pyj +192 -192
  53. package/test/_import_one.pyj +37 -37
  54. package/test/_import_two/__init__.pyj +11 -11
  55. package/test/_import_two/level2/deep.pyj +4 -4
  56. package/test/_import_two/other.pyj +6 -6
  57. package/test/_import_two/sub.pyj +13 -13
  58. package/test/aes_vectors.pyj +421 -421
  59. package/test/annotations.pyj +80 -80
  60. package/test/baselib.pyj +23 -0
  61. package/test/chainmap.pyj +185 -0
  62. package/test/decorators.pyj +77 -77
  63. package/test/docstrings.pyj +39 -39
  64. package/test/elementmaker_test.pyj +45 -45
  65. package/test/functions.pyj +151 -151
  66. package/test/generators.pyj +41 -41
  67. package/test/generic.pyj +370 -370
  68. package/test/internationalization.pyj +73 -73
  69. package/test/lint.pyj +164 -164
  70. package/test/loops.pyj +85 -85
  71. package/test/numpy.pyj +734 -734
  72. package/test/pprint.pyj +232 -0
  73. package/test/repl.pyj +121 -121
  74. package/test/scoped_flags.pyj +76 -76
  75. package/test/statistics.pyj +224 -0
  76. package/test/unit/index.js +80 -0
  77. package/test/unit/language-service-completions.js +2 -0
  78. package/test/unit/language-service-dts.js +543 -543
  79. package/test/unit/language-service-hover.js +455 -455
  80. package/test/unit/language-service.js +63 -2
  81. package/test/unit/web-repl.js +323 -0
  82. package/tools/compiler.d.ts +367 -367
  83. package/tools/completer.js +131 -131
  84. package/tools/export.js +4 -2
  85. package/tools/gettext.js +185 -185
  86. package/tools/ini.js +65 -65
  87. package/tools/msgfmt.js +187 -187
  88. package/tools/repl.js +223 -223
  89. package/tools/test.js +118 -118
  90. package/tools/utils.js +128 -128
  91. package/tools/web_repl.js +95 -95
  92. package/try +41 -41
  93. package/web-repl/env.js +196 -196
  94. package/web-repl/index.html +163 -163
  95. package/web-repl/prism.css +139 -139
  96. package/web-repl/prism.js +113 -113
  97. package/web-repl/rapydscript.js +228 -226
  98. 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')
@@ -5275,6 +5275,86 @@ assrt.equal(fib(15), 610)
5275
5275
  js_checks: [/let\s[^;]*ρσ_with_exception/, /let\s[^;]*ρσ_with_suppress/],
5276
5276
  },
5277
5277
 
5278
+ // ── BigInt literals ────────────────────────────────────────────────────
5279
+
5280
+ {
5281
+ name: "bigint_literal_basic",
5282
+ description: "42n compiles to JS BigInt literal and has bigint type",
5283
+ src: [
5284
+ "# globals: assrt",
5285
+ "x = 42n",
5286
+ "assrt.ok(jstype(x) == 'bigint', 'type is bigint')",
5287
+ "assrt.ok(x == 42n, 'value is 42n')",
5288
+ ].join("\n"),
5289
+ js_checks: ["42n"],
5290
+ },
5291
+
5292
+ {
5293
+ name: "bigint_literal_hex",
5294
+ description: "0xFFn compiles to JS hex BigInt literal",
5295
+ src: [
5296
+ "# globals: assrt",
5297
+ "x = 0xFFn",
5298
+ "assrt.ok(x == 255n, 'hex bigint value')",
5299
+ ].join("\n"),
5300
+ js_checks: ["0xFFn"],
5301
+ },
5302
+
5303
+ {
5304
+ name: "bigint_literal_binary",
5305
+ description: "0b1010n compiles to JS binary BigInt literal",
5306
+ src: [
5307
+ "# globals: assrt",
5308
+ "x = 0b1010n",
5309
+ "assrt.ok(x == 10n, 'binary bigint value')",
5310
+ ].join("\n"),
5311
+ js_checks: ["0b1010n"],
5312
+ },
5313
+
5314
+ {
5315
+ name: "bigint_literal_octal",
5316
+ description: "0o77n compiles to JS octal BigInt literal",
5317
+ src: [
5318
+ "# globals: assrt",
5319
+ "x = 0o77n",
5320
+ "assrt.ok(x == 63n, 'octal bigint value')",
5321
+ ].join("\n"),
5322
+ js_checks: ["0o77n"],
5323
+ },
5324
+
5325
+ {
5326
+ name: "bigint_literal_zero",
5327
+ description: "0n compiles and works",
5328
+ src: [
5329
+ "# globals: assrt",
5330
+ "x = 0n",
5331
+ "assrt.ok(jstype(x) == 'bigint', 'type is bigint')",
5332
+ "assrt.ok(x == 0n, 'value is 0n')",
5333
+ ].join("\n"),
5334
+ js_checks: ["0n"],
5335
+ },
5336
+
5337
+ {
5338
+ name: "bigint_literal_arithmetic",
5339
+ description: "BigInt arithmetic works correctly",
5340
+ src: [
5341
+ "# globals: assrt",
5342
+ "x = 10n + 3n",
5343
+ "assrt.ok(x == 13n, 'addition')",
5344
+ ].join("\n"),
5345
+ },
5346
+
5347
+ {
5348
+ name: "bigint_literal_large",
5349
+ description: "Large BigInt literal preserves precision",
5350
+ src: [
5351
+ "# globals: assrt",
5352
+ "x = 999999999999999999999n",
5353
+ "assrt.ok(x == 999999999999999999999n, 'large bigint')",
5354
+ ].join("\n"),
5355
+ js_checks: ["999999999999999999999n"],
5356
+ },
5357
+
5278
5358
  {
5279
5359
  name: "with_statement_suppresses_exception",
5280
5360
  description: "with statement __exit__ returning True suppresses the exception",
@@ -969,6 +969,7 @@ function make_tests(CompletionEngine, detect_context, SourceAnalyzer, DtsRegistr
969
969
  assert_has(list, 'Counter', 'Counter in collections completions');
970
970
  assert_has(list, 'OrderedDict', 'OrderedDict in collections completions');
971
971
  assert_has(list, 'defaultdict', 'defaultdict in collections completions');
972
+ assert_has(list, 'ChainMap', 'ChainMap in collections completions');
972
973
  },
973
974
  },
974
975
 
@@ -1370,6 +1371,7 @@ function make_tests(CompletionEngine, detect_context, SourceAnalyzer, DtsRegistr
1370
1371
  assert_has(list, 'Counter', 'Counter from collections');
1371
1372
  assert_has(list, 'OrderedDict', 'OrderedDict from collections');
1372
1373
  assert_has(list, 'defaultdict', 'defaultdict from collections');
1374
+ assert_has(list, 'ChainMap', 'ChainMap from collections');
1373
1375
  },
1374
1376
  },
1375
1377