rapydscript-ng 0.7.24 → 0.8.1
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/CHANGELOG.md +28 -0
- package/README.md +80 -26
- package/bin/build.ts +117 -0
- package/bin/package.json +1 -0
- package/bin/rapydscript +65 -62
- package/build_wheels.py +133 -0
- package/editor-plugins/README.md +80 -0
- package/editor-plugins/nvim.lua +321 -0
- package/local-agent.md +28 -0
- package/package.json +4 -5
- package/publish.py +27 -17
- package/release/baselib-plain-pretty.js +448 -326
- package/release/baselib-plain-ugly.js +3 -3
- package/release/compiler.js +9027 -8139
- package/release/signatures.json +27 -27
- package/release/stdlib_modules.json +1 -0
- package/src/ast.pyj +428 -280
- package/src/baselib-builtins.pyj +49 -28
- package/src/baselib-containers.pyj +241 -226
- package/src/baselib-errors.pyj +8 -1
- package/src/baselib-internal.pyj +38 -23
- package/src/baselib-itertools.pyj +13 -7
- package/src/baselib-str.pyj +59 -84
- package/src/compiler.pyj +4 -4
- package/src/errors.pyj +3 -4
- package/src/lib/aes.pyj +46 -32
- package/src/lib/elementmaker.pyj +11 -9
- package/src/lib/encodings.pyj +15 -5
- package/src/lib/gettext.pyj +9 -4
- package/src/lib/math.pyj +106 -45
- package/src/lib/operator.pyj +10 -10
- package/src/lib/pythonize.pyj +2 -1
- package/src/lib/random.pyj +28 -21
- package/src/lib/re.pyj +492 -19
- package/src/lib/traceback.pyj +171 -29
- package/src/lib/uuid.pyj +2 -2
- package/src/output/classes.pyj +28 -27
- package/src/output/codegen.pyj +87 -83
- package/src/output/comments.pyj +8 -8
- package/src/output/exceptions.pyj +20 -21
- package/src/output/functions.pyj +66 -64
- package/src/output/literals.pyj +24 -23
- package/src/output/loops.pyj +84 -142
- package/src/output/modules.pyj +55 -68
- package/src/output/operators.pyj +40 -29
- package/src/output/statements.pyj +21 -15
- package/src/output/stream.pyj +82 -56
- package/src/output/utils.pyj +12 -8
- package/src/parse.pyj +971 -620
- package/src/string_interpolation.pyj +5 -3
- package/src/tokenizer.pyj +188 -148
- package/src/utils.pyj +32 -15
- package/test/_treeshake_mod.pyj +30 -0
- package/test/_treeshake_side_effect.pyj +9 -0
- package/test/ast_serialization.pyj +392 -0
- package/test/async.pyj +95 -0
- package/test/baselib.pyj +1 -2
- package/test/embedded_compiler.pyj +57 -0
- package/test/fmt.pyj +291 -0
- package/test/generic.pyj +16 -3
- package/test/imports.pyj +8 -6
- package/test/internationalization.pyj +38 -37
- package/test/lint.pyj +120 -117
- package/test/lsp.pyj +257 -0
- package/test/repl.pyj +70 -65
- package/test/sourcemaps.pyj +315 -0
- package/test/starargs.pyj +46 -39
- package/test/str.pyj +8 -0
- package/test/traceback.pyj +263 -0
- package/test/treeshaking.pyj +181 -0
- package/test/web_repl_export.pyj +88 -0
- package/tools/ast_serialize.mjs +557 -0
- package/tools/cli.mjs +531 -0
- package/tools/compile.mjs +205 -0
- package/tools/compiler.mjs +159 -0
- package/tools/{completer.js → completer.mjs} +6 -6
- package/tools/{embedded_compiler.js → embedded_compiler.mjs} +17 -13
- package/tools/export.js +109 -56
- package/tools/fmt.mjs +975 -0
- package/tools/{gettext.js → gettext.mjs} +46 -53
- package/tools/ini.mjs +175 -0
- package/tools/{lint.js → lint.mjs} +78 -55
- package/tools/lsp.mjs +964 -0
- package/tools/lsp_protocol.mjs +259 -0
- package/tools/lsp_symbols.mjs +418 -0
- package/tools/{msgfmt.js → msgfmt.mjs} +18 -18
- package/tools/{repl.js → repl.mjs} +56 -42
- package/tools/{self.js → self.mjs} +71 -46
- package/tools/sourcemap.mjs +123 -0
- package/tools/test.mjs +252 -0
- package/tools/treeshake.mjs +400 -0
- package/tools/{utils.js → utils.mjs} +26 -23
- package/tools/{web_repl.js → web_repl.mjs} +15 -13
- package/tools/web_repl_export.mjs +97 -0
- package/tree-sitter/README.md +101 -0
- package/tree-sitter/grammar.js +992 -0
- package/tree-sitter/package.json +43 -0
- package/tree-sitter/queries/highlights.scm +232 -0
- package/tree-sitter/queries/indents.scm +64 -0
- package/tree-sitter/queries/injections.scm +14 -0
- package/tree-sitter/queries/locals.scm +108 -0
- package/tree-sitter/src/grammar.json +4976 -0
- package/tree-sitter/src/node-types.json +2901 -0
- package/tree-sitter/src/parser.c +196465 -0
- package/tree-sitter/src/scanner.c +294 -0
- package/tree-sitter/src/tree_sitter/alloc.h +54 -0
- package/tree-sitter/src/tree_sitter/array.h +330 -0
- package/tree-sitter/src/tree_sitter/parser.h +286 -0
- package/tree-sitter/test/corpus/chaining.txt +99 -0
- package/tree-sitter/test/corpus/classes.txt +147 -0
- package/tree-sitter/test/corpus/comprehensions.txt +155 -0
- package/tree-sitter/test/corpus/containers.txt +242 -0
- package/tree-sitter/test/corpus/control_flow.txt +361 -0
- package/tree-sitter/test/corpus/decorators.txt +64 -0
- package/tree-sitter/test/corpus/existential.txt +102 -0
- package/tree-sitter/test/corpus/functions.txt +293 -0
- package/tree-sitter/test/corpus/imports.txt +117 -0
- package/tree-sitter/test/corpus/literals.txt +135 -0
- package/tree-sitter/test/corpus/operators.txt +296 -0
- package/tree-sitter/test/corpus/statements.txt +189 -0
- package/tree-sitter/test/corpus/strings.txt +90 -0
- package/tree-sitter/test/corpus/subscripts.txt +227 -0
- package/tree-sitter/tree-sitter.json +36 -0
- package/web-repl/env.js +35 -23
- package/web-repl/main.js +8 -8
- package/bin/export +0 -75
- package/bin/web-repl-export +0 -102
- package/tools/cli.js +0 -523
- package/tools/compile.js +0 -184
- package/tools/compiler.js +0 -84
- package/tools/ini.js +0 -65
- package/tools/test.js +0 -110
package/src/lib/math.pyj
CHANGED
|
@@ -19,40 +19,53 @@ pi = Math.PI
|
|
|
19
19
|
e = Math.E
|
|
20
20
|
inf = Infinity
|
|
21
21
|
|
|
22
|
+
|
|
22
23
|
########################################
|
|
23
24
|
# Number-theoretic and representation functions
|
|
24
25
|
########################################
|
|
25
26
|
def ceil(x):
|
|
26
27
|
return Math.ceil(x)
|
|
28
|
+
|
|
29
|
+
|
|
27
30
|
def copysign(x, y):
|
|
28
31
|
x = Math.abs(x)
|
|
29
32
|
if y < 0:
|
|
30
33
|
return -x
|
|
31
34
|
else:
|
|
32
35
|
return x
|
|
36
|
+
|
|
37
|
+
|
|
33
38
|
def fabs(x):
|
|
34
39
|
return Math.abs(x)
|
|
40
|
+
|
|
41
|
+
|
|
35
42
|
def factorial(x):
|
|
36
43
|
if Math.abs(int(x)) is not x:
|
|
37
|
-
raise ValueError(
|
|
44
|
+
raise ValueError('factorial() only accepts integral values')
|
|
38
45
|
factorial.cache = []
|
|
39
|
-
r = def(n):
|
|
46
|
+
r = def (n):
|
|
40
47
|
if n is 0 or n is 1:
|
|
41
48
|
return 1
|
|
42
49
|
if not factorial.cache[n]:
|
|
43
|
-
factorial.cache[n] = r(n-1) * n
|
|
50
|
+
factorial.cache[n] = r(n - 1) * n
|
|
44
51
|
return factorial.cache[n]
|
|
45
52
|
return r(x)
|
|
53
|
+
|
|
54
|
+
|
|
46
55
|
def floor(x):
|
|
47
56
|
return Math.floor(x)
|
|
57
|
+
|
|
58
|
+
|
|
48
59
|
def fmod(x, y):
|
|
49
60
|
# javascript's % operator isn't consistent with C fmod implementation, this function is
|
|
50
61
|
while y <= x:
|
|
51
62
|
x -= y
|
|
52
63
|
return x
|
|
64
|
+
|
|
65
|
+
|
|
53
66
|
def fsum(iterable):
|
|
54
67
|
# like Python's fsum, this method is much more resilient to rounding errors than regular sum
|
|
55
|
-
partials = []
|
|
68
|
+
partials = [] # sorted, non-overlapping partial sums
|
|
56
69
|
for x in iterable:
|
|
57
70
|
i = 0
|
|
58
71
|
for y in partials:
|
|
@@ -64,34 +77,49 @@ def fsum(iterable):
|
|
|
64
77
|
partials[i] = lo
|
|
65
78
|
i += 1
|
|
66
79
|
x = hi
|
|
67
|
-
#partials[i:] = [x]
|
|
68
|
-
partials.splice(i, partials.length-i, x)
|
|
80
|
+
# partials[i:] = [x]
|
|
81
|
+
partials.splice(i, partials.length - i, x)
|
|
69
82
|
return sum(partials)
|
|
83
|
+
|
|
84
|
+
|
|
70
85
|
def isinf(x):
|
|
71
86
|
return not isFinite(x)
|
|
87
|
+
|
|
88
|
+
|
|
72
89
|
def isnan(x):
|
|
73
90
|
return isNaN(x)
|
|
91
|
+
|
|
92
|
+
|
|
74
93
|
def modf(x):
|
|
75
94
|
m = fmod(x, 1)
|
|
76
|
-
return m, x-m
|
|
95
|
+
return m, x - m
|
|
96
|
+
|
|
97
|
+
|
|
77
98
|
def trunc(x):
|
|
78
99
|
return x | 0
|
|
79
100
|
|
|
101
|
+
|
|
80
102
|
########################################
|
|
81
103
|
# Power and logarithmic functions
|
|
82
104
|
########################################
|
|
83
105
|
def exp(x):
|
|
84
106
|
return Math.exp(x)
|
|
107
|
+
|
|
108
|
+
|
|
85
109
|
def expm1(x):
|
|
86
110
|
# NOTE: Math.expm1() is currently only implemented in Firefox, this provides alternative implementation
|
|
87
111
|
# https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/expm1
|
|
88
|
-
#return Math.expm1(x)
|
|
112
|
+
# return Math.expm1(x)
|
|
89
113
|
if Math.abs(x) < 1e-5:
|
|
90
|
-
return x + 0.5*x*x
|
|
114
|
+
return x + 0.5 * x * x
|
|
91
115
|
else:
|
|
92
116
|
return Math.exp(x) - 1
|
|
117
|
+
|
|
118
|
+
|
|
93
119
|
def log(x, base=e):
|
|
94
|
-
return Math.log(x)/Math.log(base)
|
|
120
|
+
return Math.log(x) / Math.log(base)
|
|
121
|
+
|
|
122
|
+
|
|
95
123
|
def log1p(x):
|
|
96
124
|
# NOTE: Math.log1p() is currently only implemented in Firefox, this provides alternative implementation
|
|
97
125
|
# https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log1p
|
|
@@ -109,37 +137,62 @@ def log1p(x):
|
|
|
109
137
|
else:
|
|
110
138
|
ret += Math.pow(x, i) / i
|
|
111
139
|
return ret
|
|
140
|
+
|
|
141
|
+
|
|
112
142
|
def log10(x):
|
|
113
143
|
# NOTE: Math.log10() is currently only implemented in Firefox, this provides alternative implementation
|
|
114
144
|
# https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log10
|
|
115
145
|
# I didn't find a more accurate algorithm so I'm using the basic implementation
|
|
116
|
-
return Math.log(x)/Math.LN10
|
|
146
|
+
return Math.log(x) / Math.LN10
|
|
147
|
+
|
|
148
|
+
|
|
117
149
|
def pow(x, y):
|
|
118
150
|
if x < 0 and int(y) is not y:
|
|
119
151
|
raise ValueError('math domain error')
|
|
120
152
|
if isnan(y) and x is 1:
|
|
121
153
|
return 1
|
|
122
154
|
return Math.pow(x, y)
|
|
155
|
+
|
|
156
|
+
|
|
123
157
|
def sqrt(x):
|
|
124
158
|
return Math.sqrt(x)
|
|
125
159
|
|
|
160
|
+
|
|
126
161
|
########################################
|
|
127
162
|
# Trigonometric functions
|
|
128
163
|
########################################
|
|
129
|
-
def acos(x):
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
def
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
def
|
|
136
|
-
|
|
164
|
+
def acos(x): return Math.acos(x)
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def asin(x): return Math.asin(x)
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def atan(x): return Math.atan(x)
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def atan2(y, x): return Math.atan2(y, x)
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def cos(x): return Math.cos(x)
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def sin(x): return Math.sin(x)
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def hypot(x, y): return Math.sqrt(x * x + y * y)
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def tan(x): return Math.tan(x)
|
|
186
|
+
|
|
137
187
|
|
|
138
188
|
########################################
|
|
139
189
|
# Angular conversion
|
|
140
190
|
########################################
|
|
141
|
-
def degrees(x):
|
|
142
|
-
|
|
191
|
+
def degrees(x): return x * 180 / pi
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def radians(x): return x * pi / 180
|
|
195
|
+
|
|
143
196
|
|
|
144
197
|
########################################
|
|
145
198
|
# Hyperbolic functions
|
|
@@ -147,47 +200,55 @@ def radians(x): return x*pi/180
|
|
|
147
200
|
def acosh(x):
|
|
148
201
|
# NOTE: will be replaced with official, when it becomes mainstream
|
|
149
202
|
# https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/acosh
|
|
150
|
-
return Math.log(x + Math.sqrt(x*x - 1))
|
|
203
|
+
return Math.log(x + Math.sqrt(x * x - 1))
|
|
204
|
+
|
|
205
|
+
|
|
151
206
|
def asinh(x):
|
|
152
207
|
# NOTE: will be replaced with official, when it becomes mainstream
|
|
153
208
|
# https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/asinh
|
|
154
|
-
return Math.log(x + Math.sqrt(x*x + 1))
|
|
209
|
+
return Math.log(x + Math.sqrt(x * x + 1))
|
|
210
|
+
|
|
211
|
+
|
|
155
212
|
def atanh(x):
|
|
156
213
|
# NOTE: will be replaced with official, when it becomes mainstream
|
|
157
214
|
# https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/atanh
|
|
158
215
|
return 0.5 * Math.log((1 + x) / (1 - x))
|
|
216
|
+
|
|
217
|
+
|
|
159
218
|
def cosh(x):
|
|
160
219
|
# NOTE: will be replaced with official, when it becomes mainstream
|
|
161
220
|
# https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/cosh
|
|
162
221
|
return (Math.exp(x) + Math.exp(-x)) / 2
|
|
222
|
+
|
|
223
|
+
|
|
163
224
|
def sinh(x):
|
|
164
225
|
# NOTE: will be replaced with official, when it becomes mainstream
|
|
165
226
|
# https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sinh
|
|
166
227
|
return (Math.exp(x) - Math.exp(-x)) / 2
|
|
228
|
+
|
|
229
|
+
|
|
167
230
|
def tanh(x):
|
|
168
231
|
# NOTE: will be replaced with official, when it becomes mainstream
|
|
169
232
|
# https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/tanh
|
|
170
233
|
return (Math.exp(x) - Math.exp(-x)) / (Math.exp(x) + Math.exp(-x))
|
|
171
234
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
#
|
|
175
|
-
#print(math.
|
|
176
|
-
#print(math.
|
|
177
|
-
#print(math.
|
|
178
|
-
#print(math.
|
|
179
|
-
#
|
|
180
|
-
#
|
|
181
|
-
#
|
|
182
|
-
#
|
|
183
|
-
#print(
|
|
184
|
-
#print(math.
|
|
185
|
-
#print(math.
|
|
186
|
-
#print(math.
|
|
187
|
-
#print(math.
|
|
188
|
-
#print(math.
|
|
189
|
-
#print(math.
|
|
190
|
-
#print(math.
|
|
191
|
-
#print(math.
|
|
192
|
-
#print(math.hypot(3,4))
|
|
193
|
-
#print(math.acosh(2), math.asinh(1), math.atanh(0.5), math.cosh(1), math.cosh(-1), math.sinh(1), math.tanh(1))
|
|
235
|
+
# import stdlib
|
|
236
|
+
# print(math.ceil(4.2))
|
|
237
|
+
# print(math.floor(4.2))
|
|
238
|
+
# print(math.fabs(-6))
|
|
239
|
+
# print(math.copysign(-5, 7))
|
|
240
|
+
# print(math.factorial(4))
|
|
241
|
+
# print(math.fmod(-1e100, 1e100))
|
|
242
|
+
#
|
|
243
|
+
# d = [0.9999999, 1, 2, 3]
|
|
244
|
+
# print(sum(d), math.fsum(d))
|
|
245
|
+
# print(math.isinf(5), math.isinf(Infinity))
|
|
246
|
+
# print(math.modf(5.5))
|
|
247
|
+
# print(math.trunc(2.6), math.trunc(-2.6))
|
|
248
|
+
# print(math.exp(1e-5), math.expm1(1e-5))
|
|
249
|
+
# print(math.log(10), math.log(10, 1000))
|
|
250
|
+
# print(math.log1p(1e-15), math.log1p(1))
|
|
251
|
+
# print(math.log10(1000), math.log(1000, 10))
|
|
252
|
+
# print(math.pow(1, 0), math.pow(1, NaN), math.pow(0, 0), math.pow(NaN, 0), math.pow(4,3), math.pow(100, -2))
|
|
253
|
+
# print(math.hypot(3,4))
|
|
254
|
+
# print(math.acosh(2), math.asinh(1), math.atanh(0.5), math.cosh(1), math.cosh(-1), math.sinh(1), math.tanh(1))
|
package/src/lib/operator.pyj
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
add = __add__ = def(x, y): return x + y
|
|
2
|
-
sub = __sub__ = def(x, y): return x - y
|
|
3
|
-
mul = __mul__ = def(x, y): return x * y
|
|
4
|
-
div = __div__ = def(x, y): return x / y
|
|
1
|
+
add = __add__ = def (x, y): return x + y
|
|
2
|
+
sub = __sub__ = def (x, y): return x - y
|
|
3
|
+
mul = __mul__ = def (x, y): return x * y
|
|
4
|
+
div = __div__ = def (x, y): return x / y
|
|
5
5
|
|
|
6
|
-
lt = __lt__ = def(x, y): return x < y
|
|
7
|
-
le = __le__ = def(x, y): return x <= y
|
|
8
|
-
eq = __eq__ = def(x, y): return x is y
|
|
9
|
-
ne = __ne__ = def(x, y): return x is not y
|
|
10
|
-
ge = __ge__ = def(x, y): return x >= y
|
|
11
|
-
gt = __gt__ = def(x, y): return x > y
|
|
6
|
+
lt = __lt__ = def (x, y): return x < y
|
|
7
|
+
le = __le__ = def (x, y): return x <= y
|
|
8
|
+
eq = __eq__ = def (x, y): return x is y
|
|
9
|
+
ne = __ne__ = def (x, y): return x is not y
|
|
10
|
+
ge = __ge__ = def (x, y): return x >= y
|
|
11
|
+
gt = __gt__ = def (x, y): return x > y
|
package/src/lib/pythonize.pyj
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
# License: BSD Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
|
|
3
3
|
# globals: ρσ_str
|
|
4
4
|
|
|
5
|
+
|
|
5
6
|
def strings():
|
|
6
7
|
string_funcs = set((
|
|
7
8
|
'capitalize strip lstrip rstrip islower isupper isspace lower upper swapcase'
|
|
@@ -11,7 +12,7 @@ def strings():
|
|
|
11
12
|
if not arguments.length:
|
|
12
13
|
exclude = {'split', 'replace'}
|
|
13
14
|
elif arguments[0]:
|
|
14
|
-
exclude = Array.
|
|
15
|
+
exclude = Array.from (arguments)
|
|
15
16
|
else:
|
|
16
17
|
exclude = None
|
|
17
18
|
if exclude:
|
package/src/lib/random.pyj
CHANGED
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
'key_j': 0
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
ρσ_get_random_byte = def():
|
|
24
|
+
ρσ_get_random_byte = def ():
|
|
25
25
|
ρσ_seed_state.key_i = (ρσ_seed_state.key_i + 1) % 256
|
|
26
26
|
ρσ_seed_state.key_j = (ρσ_seed_state.key_j + ρσ_seed_state.key[ρσ_seed_state.key_i]) % 256
|
|
27
27
|
ρσ_seed_state.key[ρσ_seed_state.key_i], ρσ_seed_state.key[ρσ_seed_state.key_j] = \
|
|
@@ -29,6 +29,7 @@
|
|
|
29
29
|
return ρσ_seed_state.key[(ρσ_seed_state.key[ρσ_seed_state.key_i] + \
|
|
30
30
|
ρσ_seed_state.key[ρσ_seed_state.key_j]) % 256]
|
|
31
31
|
|
|
32
|
+
|
|
32
33
|
def seed(x=Date().getTime()):
|
|
33
34
|
ρσ_seed_state.key_i = ρσ_seed_state.key_j = 0
|
|
34
35
|
if jstype(x) is 'number':
|
|
@@ -43,6 +44,7 @@ def seed(x=Date().getTime()):
|
|
|
43
44
|
ρσ_seed_state.key[i], ρσ_seed_state.key[j] = ρσ_seed_state.key[j], ρσ_seed_state.key[i]
|
|
44
45
|
seed()
|
|
45
46
|
|
|
47
|
+
|
|
46
48
|
def random():
|
|
47
49
|
n = 0
|
|
48
50
|
m = 1
|
|
@@ -51,6 +53,7 @@ def random():
|
|
|
51
53
|
m *= 256
|
|
52
54
|
return v'n / 0x10000000000000000'
|
|
53
55
|
|
|
56
|
+
|
|
54
57
|
def randrange():
|
|
55
58
|
if arguments.length is 1:
|
|
56
59
|
return randbelow(int(arguments[0]))
|
|
@@ -64,26 +67,29 @@ def randrange():
|
|
|
64
67
|
if step is 1:
|
|
65
68
|
if width > 0:
|
|
66
69
|
return start + randbelow(width)
|
|
67
|
-
raise ValueError(
|
|
70
|
+
raise ValueError('empty range for randrange()')
|
|
68
71
|
if step > 0:
|
|
69
72
|
n = (width + step - 1) // step
|
|
70
73
|
elif step < 0:
|
|
71
74
|
n = (width + step + 1) // step
|
|
72
75
|
else:
|
|
73
|
-
raise ValueError(
|
|
76
|
+
raise ValueError('zero step for randrange()')
|
|
74
77
|
if n <= 0:
|
|
75
78
|
raise ValueError(f"empty range in randrange({start}, {stop}, {step})")
|
|
76
79
|
return start + step * randbelow(n)
|
|
77
80
|
|
|
78
81
|
|
|
79
82
|
def randint(a, b):
|
|
80
|
-
return int(random()*(b-a+1) + a)
|
|
83
|
+
return int(random() * (b - a + 1) + a)
|
|
84
|
+
|
|
81
85
|
|
|
82
86
|
def uniform(a, b):
|
|
83
|
-
return random()*(b-a) + a
|
|
87
|
+
return random() * (b - a) + a
|
|
88
|
+
|
|
84
89
|
|
|
85
90
|
def randbelow(n):
|
|
86
|
-
return Math.floor(random()*n)
|
|
91
|
+
return Math.floor(random() * n)
|
|
92
|
+
|
|
87
93
|
|
|
88
94
|
def choice(seq):
|
|
89
95
|
if seq.length > 0:
|
|
@@ -91,28 +97,29 @@ def choice(seq):
|
|
|
91
97
|
else:
|
|
92
98
|
raise IndexError()
|
|
93
99
|
|
|
100
|
+
|
|
94
101
|
# uses Fisher-Yates algorithm to shuffle an array
|
|
95
102
|
def shuffle(x, random_f=random):
|
|
96
103
|
for i in range(x.length):
|
|
97
|
-
j = Math.floor(random_f() * (i+1))
|
|
104
|
+
j = Math.floor(random_f() * (i + 1))
|
|
98
105
|
x[i], x[j] = x[j], x[i]
|
|
99
106
|
return x
|
|
100
107
|
|
|
108
|
+
|
|
101
109
|
# similar to shuffle, but only shuffles a subset and creates a copy
|
|
102
110
|
def sample(population, k):
|
|
103
111
|
x = population.slice()
|
|
104
|
-
for i in range(population.length-1, population.length-k-1, -1):
|
|
105
|
-
j = Math.floor(random() * (i+1))
|
|
112
|
+
for i in range(population.length - 1, population.length - k - 1, -1):
|
|
113
|
+
j = Math.floor(random() * (i + 1))
|
|
106
114
|
x[i], x[j] = x[j], x[i]
|
|
107
|
-
return x.slice(population.length-k)
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
#
|
|
111
|
-
#
|
|
112
|
-
#random.
|
|
113
|
-
#print(random.
|
|
114
|
-
#print(random.
|
|
115
|
-
#print(random.
|
|
116
|
-
#print(random.
|
|
117
|
-
#print(random.
|
|
118
|
-
#print(random.sample(range(20),5))
|
|
115
|
+
return x.slice(population.length - k)
|
|
116
|
+
|
|
117
|
+
# import stdlib
|
|
118
|
+
# a = range(50)
|
|
119
|
+
# random.seed(5)
|
|
120
|
+
# print(random.choice(a))
|
|
121
|
+
# print(random.shuffle(a))
|
|
122
|
+
# print(random.randrange(10))
|
|
123
|
+
# print(random.randint(1,5))
|
|
124
|
+
# print(random.uniform(1,5))
|
|
125
|
+
# print(random.sample(range(20),5))
|