rapydscript-ns 0.8.0
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 -0
- package/.gitattributes +4 -0
- package/.github/workflows/ci.yml +38 -0
- package/.github/workflows/web-repl-page-deploy.yml +42 -0
- package/=template.pyj +5 -0
- package/CHANGELOG.md +456 -0
- package/CONTRIBUTORS +13 -0
- package/HACKING.md +103 -0
- package/LICENSE +24 -0
- package/README.md +2512 -0
- package/TODO.md +327 -0
- package/add-toc-to-readme +2 -0
- package/bin/export +75 -0
- package/bin/rapydscript +70 -0
- package/bin/web-repl-export +102 -0
- package/build +3 -0
- package/package.json +46 -0
- package/publish.py +37 -0
- package/release/baselib-plain-pretty.js +4370 -0
- package/release/baselib-plain-ugly.js +3 -0
- package/release/compiler.js +18394 -0
- package/release/signatures.json +31 -0
- package/session.vim +4 -0
- package/setup.cfg +2 -0
- package/src/ast.pyj +1356 -0
- package/src/baselib-builtins.pyj +279 -0
- package/src/baselib-containers.pyj +723 -0
- package/src/baselib-errors.pyj +37 -0
- package/src/baselib-internal.pyj +421 -0
- package/src/baselib-itertools.pyj +97 -0
- package/src/baselib-str.pyj +798 -0
- package/src/compiler.pyj +36 -0
- package/src/errors.pyj +30 -0
- package/src/lib/aes.pyj +646 -0
- package/src/lib/collections.pyj +695 -0
- package/src/lib/elementmaker.pyj +83 -0
- package/src/lib/encodings.pyj +126 -0
- package/src/lib/functools.pyj +148 -0
- package/src/lib/gettext.pyj +569 -0
- package/src/lib/itertools.pyj +580 -0
- package/src/lib/math.pyj +193 -0
- package/src/lib/numpy.pyj +2101 -0
- package/src/lib/operator.pyj +11 -0
- package/src/lib/pythonize.pyj +20 -0
- package/src/lib/random.pyj +118 -0
- package/src/lib/re.pyj +470 -0
- package/src/lib/traceback.pyj +63 -0
- package/src/lib/uuid.pyj +77 -0
- package/src/monaco-language-service/analyzer.js +526 -0
- package/src/monaco-language-service/builtins.js +543 -0
- package/src/monaco-language-service/completions.js +498 -0
- package/src/monaco-language-service/diagnostics.js +643 -0
- package/src/monaco-language-service/dts.js +550 -0
- package/src/monaco-language-service/hover.js +121 -0
- package/src/monaco-language-service/index.js +386 -0
- package/src/monaco-language-service/scope.js +162 -0
- package/src/monaco-language-service/signature.js +144 -0
- package/src/output/__init__.pyj +0 -0
- package/src/output/classes.pyj +296 -0
- package/src/output/codegen.pyj +492 -0
- package/src/output/comments.pyj +45 -0
- package/src/output/exceptions.pyj +105 -0
- package/src/output/functions.pyj +491 -0
- package/src/output/literals.pyj +109 -0
- package/src/output/loops.pyj +444 -0
- package/src/output/modules.pyj +329 -0
- package/src/output/operators.pyj +429 -0
- package/src/output/statements.pyj +463 -0
- package/src/output/stream.pyj +309 -0
- package/src/output/treeshake.pyj +182 -0
- package/src/output/utils.pyj +72 -0
- package/src/parse.pyj +3106 -0
- package/src/string_interpolation.pyj +72 -0
- package/src/tokenizer.pyj +702 -0
- package/src/unicode_aliases.pyj +576 -0
- package/src/utils.pyj +192 -0
- package/test/_import_one.pyj +37 -0
- package/test/_import_two/__init__.pyj +11 -0
- package/test/_import_two/level2/__init__.pyj +0 -0
- package/test/_import_two/level2/deep.pyj +4 -0
- package/test/_import_two/other.pyj +6 -0
- package/test/_import_two/sub.pyj +13 -0
- package/test/aes_vectors.pyj +421 -0
- package/test/annotations.pyj +80 -0
- package/test/baselib.pyj +319 -0
- package/test/classes.pyj +452 -0
- package/test/collections.pyj +152 -0
- package/test/decorators.pyj +77 -0
- package/test/dict_spread.pyj +76 -0
- package/test/docstrings.pyj +39 -0
- package/test/elementmaker_test.pyj +45 -0
- package/test/ellipsis.pyj +49 -0
- package/test/functions.pyj +151 -0
- package/test/generators.pyj +41 -0
- package/test/generic.pyj +370 -0
- package/test/imports.pyj +72 -0
- package/test/internationalization.pyj +73 -0
- package/test/lint.pyj +164 -0
- package/test/loops.pyj +85 -0
- package/test/numpy.pyj +734 -0
- package/test/omit_function_metadata.pyj +20 -0
- package/test/regexp.pyj +55 -0
- package/test/repl.pyj +121 -0
- package/test/scoped_flags.pyj +76 -0
- package/test/starargs.pyj +506 -0
- package/test/starred_assign.pyj +104 -0
- package/test/str.pyj +198 -0
- package/test/subscript_tuple.pyj +53 -0
- package/test/unit/fixtures/fibonacci_expected.js +46 -0
- package/test/unit/index.js +2989 -0
- package/test/unit/language-service-builtins.js +815 -0
- package/test/unit/language-service-completions.js +1067 -0
- package/test/unit/language-service-dts.js +543 -0
- package/test/unit/language-service-hover.js +455 -0
- package/test/unit/language-service-scope.js +833 -0
- package/test/unit/language-service-signature.js +458 -0
- package/test/unit/language-service.js +705 -0
- package/test/unit/run-language-service.js +41 -0
- package/test/unit/web-repl.js +484 -0
- package/tools/build-language-service.js +190 -0
- package/tools/cli.js +547 -0
- package/tools/compile.js +219 -0
- package/tools/compiler.js +108 -0
- package/tools/completer.js +131 -0
- package/tools/embedded_compiler.js +251 -0
- package/tools/export.js +316 -0
- package/tools/gettext.js +185 -0
- package/tools/ini.js +65 -0
- package/tools/lint.js +705 -0
- package/tools/msgfmt.js +187 -0
- package/tools/repl.js +223 -0
- package/tools/self.js +162 -0
- package/tools/test.js +118 -0
- package/tools/utils.js +128 -0
- package/tools/web_repl.js +95 -0
- package/try +41 -0
- package/web-repl/env.js +74 -0
- package/web-repl/index.html +163 -0
- package/web-repl/language-service.js +4084 -0
- package/web-repl/main.js +254 -0
- package/web-repl/prism.css +139 -0
- package/web-repl/prism.js +113 -0
- package/web-repl/rapydscript.js +435 -0
- package/web-repl/sha1.js +25 -0
package/test/classes.pyj
ADDED
|
@@ -0,0 +1,452 @@
|
|
|
1
|
+
# globals: assrt
|
|
2
|
+
|
|
3
|
+
# empty classes are allowed
|
|
4
|
+
class Blank:
|
|
5
|
+
pass
|
|
6
|
+
blank = Blank()
|
|
7
|
+
assrt.ok(isinstance(blank, Blank))
|
|
8
|
+
|
|
9
|
+
# basic class
|
|
10
|
+
class Human:
|
|
11
|
+
|
|
12
|
+
HAIRS = 23
|
|
13
|
+
|
|
14
|
+
def __init__(self, name):
|
|
15
|
+
self.name = name
|
|
16
|
+
|
|
17
|
+
def greet(self):
|
|
18
|
+
return "Hello, I'm " + self.name
|
|
19
|
+
|
|
20
|
+
@staticmethod
|
|
21
|
+
def getTypicalWeight():
|
|
22
|
+
return "150"
|
|
23
|
+
|
|
24
|
+
@staticmethod
|
|
25
|
+
def with_arg(x):
|
|
26
|
+
return x
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
# don't have to declare __init__, it either gets inherited or stays empty
|
|
30
|
+
# isinstance correctly sees inheritance
|
|
31
|
+
class Friend(Human):
|
|
32
|
+
def greet(self):
|
|
33
|
+
return "Yo, it's me, " + self.name
|
|
34
|
+
|
|
35
|
+
def nickname(self, name):
|
|
36
|
+
self.name = name
|
|
37
|
+
|
|
38
|
+
# __init__ doesn't have to come first
|
|
39
|
+
# can call methods of other classes
|
|
40
|
+
class OldFriend(Friend):
|
|
41
|
+
def how_long(self):
|
|
42
|
+
return "I've known you for " + self.duration + " years"
|
|
43
|
+
def get_bound_method(self):
|
|
44
|
+
return self.how_long.bind(self)
|
|
45
|
+
def __init__(self, name, duration):
|
|
46
|
+
self.duration = duration
|
|
47
|
+
Friend.__init__(self, name)
|
|
48
|
+
|
|
49
|
+
bob = Human("Bob")
|
|
50
|
+
assrt.equal(bob.greet(), "Hello, I'm Bob")
|
|
51
|
+
assrt.equal(Human.greet(bob), "Hello, I'm Bob")
|
|
52
|
+
assrt.equal(Human.getTypicalWeight(), "150") # static method recognition
|
|
53
|
+
assrt.equal(Human.with_arg(3), 3) # static method with arg
|
|
54
|
+
assrt.equal(Human.HAIRS, 23)
|
|
55
|
+
|
|
56
|
+
joe = Friend("Joe")
|
|
57
|
+
assrt.equal(joe.greet(), "Yo, it's me, Joe")
|
|
58
|
+
assrt.ok(isinstance(joe, Friend))
|
|
59
|
+
assrt.ok(isinstance(joe, Human))
|
|
60
|
+
|
|
61
|
+
angela = OldFriend("Angela", 8)
|
|
62
|
+
assrt.equal(angela.greet(), "Yo, it's me, Angela")
|
|
63
|
+
assrt.equal(angela.how_long(), "I've known you for 8 years")
|
|
64
|
+
|
|
65
|
+
# test that function stays bound
|
|
66
|
+
bound = angela.get_bound_method()
|
|
67
|
+
assrt.equal(bound(), angela.how_long())
|
|
68
|
+
|
|
69
|
+
# function methods
|
|
70
|
+
assrt.deepEqual(dir(angela).sort(), [
|
|
71
|
+
'HAIRS',
|
|
72
|
+
"__init__",
|
|
73
|
+
'__repr__',
|
|
74
|
+
'__str__',
|
|
75
|
+
"constructor",
|
|
76
|
+
"duration",
|
|
77
|
+
"get_bound_method",
|
|
78
|
+
"greet",
|
|
79
|
+
"how_long",
|
|
80
|
+
"name",
|
|
81
|
+
"nickname",
|
|
82
|
+
])
|
|
83
|
+
|
|
84
|
+
# test that binding works in relation to the actual class, not the parent
|
|
85
|
+
angela.nickname("Angie")
|
|
86
|
+
assrt.equal(angela.greet(), "Yo, it's me, Angie")
|
|
87
|
+
|
|
88
|
+
dude = None
|
|
89
|
+
(def fake_module():
|
|
90
|
+
# test that we can declare classes inside other blocks
|
|
91
|
+
# test that we can call methods of classes we didn't inherit from
|
|
92
|
+
nonlocal dude
|
|
93
|
+
class Stranger(Human):
|
|
94
|
+
def greet(self):
|
|
95
|
+
return Friend.greet(self)
|
|
96
|
+
dude = Stranger("some guy")
|
|
97
|
+
)()
|
|
98
|
+
assrt.equal(dude.greet(), "Yo, it's me, some guy")
|
|
99
|
+
# also test that classes declared this way are not globally scoped (while normal ones are)
|
|
100
|
+
assrt.throws(
|
|
101
|
+
def():
|
|
102
|
+
Friend("another friend")
|
|
103
|
+
Stranger("another guy") # noqa:undef
|
|
104
|
+
,
|
|
105
|
+
/Stranger is not defined/
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
# attributes
|
|
109
|
+
assrt.ok(hasattr(dude, "greet"))
|
|
110
|
+
assrt.equal(getattr(dude, "greet").bind(dude)(), "Yo, it's me, some guy") # function stays bound after binding
|
|
111
|
+
assrt.equal(hasattr(dude, "stuff"), False)
|
|
112
|
+
setattr(dude, "stuff", True)
|
|
113
|
+
assrt.ok(hasattr(dude, "stuff"))
|
|
114
|
+
|
|
115
|
+
# native classes and methods
|
|
116
|
+
st = String("test")
|
|
117
|
+
assrt.equal(st, "test")
|
|
118
|
+
assrt.equal(st.toUpperCase(), "TEST")
|
|
119
|
+
assrt.equal(String.toUpperCase(st), "TEST")
|
|
120
|
+
assrt.equal(String.fromCharCode(65), "A") # static method recognition
|
|
121
|
+
|
|
122
|
+
# now we test RapydScript's ability to insert 'new' operator correctly
|
|
123
|
+
assrt.ok(String('a') != 'a') # string literal vs string object
|
|
124
|
+
assrt.ok((String)('a') == 'a') # string literal vs string literal
|
|
125
|
+
assrt.ok(String.call(this, 'a') == 'a') # string literal via static method on string
|
|
126
|
+
|
|
127
|
+
# self consistency
|
|
128
|
+
class Counter:
|
|
129
|
+
def __init__(s, n=0):
|
|
130
|
+
s.count = n # first arg becomes 'self'
|
|
131
|
+
def getIncrementer(self):
|
|
132
|
+
return def():
|
|
133
|
+
self.count += 1
|
|
134
|
+
c = Counter(5)
|
|
135
|
+
inc = c.getIncrementer()
|
|
136
|
+
inc()
|
|
137
|
+
assrt.equal(c.count, 6)
|
|
138
|
+
|
|
139
|
+
# nested classes
|
|
140
|
+
# not yet fully implemented
|
|
141
|
+
#class Molecule:
|
|
142
|
+
# class Atom:
|
|
143
|
+
# def __init__(self, element):
|
|
144
|
+
# self.element = element
|
|
145
|
+
#
|
|
146
|
+
# def __init__(self, elements):
|
|
147
|
+
# self.structure = []
|
|
148
|
+
# for e in elements:
|
|
149
|
+
# self.structure.push(Molecule.Atom(e))
|
|
150
|
+
#
|
|
151
|
+
#water = Molecule(['H', "H", 'O'])
|
|
152
|
+
#assrt.equal(len(water.structure), 3)
|
|
153
|
+
#assrt.equal(water.structure[0].element, 'H')
|
|
154
|
+
#for atom in water.structure:
|
|
155
|
+
# assrt.ok(isinstance(atom, Molecule.Atom))
|
|
156
|
+
|
|
157
|
+
# starargs and method decorators
|
|
158
|
+
def negate(fn):
|
|
159
|
+
def wrapped(*args):
|
|
160
|
+
return -fn(*args)
|
|
161
|
+
return wrapped
|
|
162
|
+
|
|
163
|
+
def add_pi(cls):
|
|
164
|
+
cls.prototype.pi = 3.14
|
|
165
|
+
return cls
|
|
166
|
+
|
|
167
|
+
@add_pi
|
|
168
|
+
class Math:
|
|
169
|
+
def sum(s, *args):
|
|
170
|
+
# fakearg simply tests that offsets work correctly
|
|
171
|
+
ttl = 0
|
|
172
|
+
for i in args:
|
|
173
|
+
ttl += i
|
|
174
|
+
return ttl
|
|
175
|
+
def concatSum(s, string, *nums):
|
|
176
|
+
return string + s.sum(*nums)
|
|
177
|
+
@negate
|
|
178
|
+
def plus(s, a, b):
|
|
179
|
+
return a+b
|
|
180
|
+
|
|
181
|
+
m = Math()
|
|
182
|
+
assrt.equal(m.sum(1,2,3), 6)
|
|
183
|
+
assrt.equal(m.sum(1,*[2,3]), 6)
|
|
184
|
+
assrt.equal(m.concatSum("foo", 1, 2, 5), "foo8")
|
|
185
|
+
assrt.equal(m.plus(2, 5), -7)
|
|
186
|
+
assrt.equal(m.pi, 3.14)
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
class CV():
|
|
190
|
+
|
|
191
|
+
a = b = 1
|
|
192
|
+
c = a + b
|
|
193
|
+
if True:
|
|
194
|
+
d = 1
|
|
195
|
+
else:
|
|
196
|
+
d = 2
|
|
197
|
+
|
|
198
|
+
def one(self):
|
|
199
|
+
return 1
|
|
200
|
+
two = one
|
|
201
|
+
|
|
202
|
+
c = CV()
|
|
203
|
+
assrt.deepEqual([c.a, c.b, c.c, c.d], [1, 1, 2, 1])
|
|
204
|
+
assrt.equal(c.one(), c.two())
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
class Properties:
|
|
208
|
+
|
|
209
|
+
def __init__(self):
|
|
210
|
+
self._a = 19
|
|
211
|
+
assrt.equal(self.a, 19)
|
|
212
|
+
|
|
213
|
+
@property
|
|
214
|
+
def a(self):
|
|
215
|
+
return self._a
|
|
216
|
+
|
|
217
|
+
@a.setter
|
|
218
|
+
def a(self, val):
|
|
219
|
+
self._a = val
|
|
220
|
+
|
|
221
|
+
@property
|
|
222
|
+
def b(self):
|
|
223
|
+
return 1
|
|
224
|
+
|
|
225
|
+
def c(self):
|
|
226
|
+
return self.a
|
|
227
|
+
|
|
228
|
+
class A:
|
|
229
|
+
@property
|
|
230
|
+
def val(self):
|
|
231
|
+
return 'a'
|
|
232
|
+
|
|
233
|
+
@property
|
|
234
|
+
def parent(self):
|
|
235
|
+
return 'parent'
|
|
236
|
+
|
|
237
|
+
class B(A):
|
|
238
|
+
@property
|
|
239
|
+
def val(self):
|
|
240
|
+
return 'b'
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
p = Properties()
|
|
244
|
+
assrt.equal(p._a, p.a)
|
|
245
|
+
p.a = 11
|
|
246
|
+
assrt.equal(11, p.a)
|
|
247
|
+
assrt.equal(p.c(), p.a)
|
|
248
|
+
assrt.equal(p.b, 1)
|
|
249
|
+
assrt.throws(
|
|
250
|
+
def():
|
|
251
|
+
p.b = 2
|
|
252
|
+
)
|
|
253
|
+
p = B()
|
|
254
|
+
assrt.equal(p.val, 'b')
|
|
255
|
+
assrt.equal(p.parent, 'parent')
|
|
256
|
+
assrt.equal(id(p), id(p))
|
|
257
|
+
assrt.notEqual(id(p), id(B()))
|
|
258
|
+
|
|
259
|
+
class Context:
|
|
260
|
+
|
|
261
|
+
val = 1
|
|
262
|
+
|
|
263
|
+
def __enter__(self):
|
|
264
|
+
self.val = 2
|
|
265
|
+
return self
|
|
266
|
+
|
|
267
|
+
def __exit__(self):
|
|
268
|
+
self.val = 3
|
|
269
|
+
return True
|
|
270
|
+
|
|
271
|
+
with Context() as c:
|
|
272
|
+
assrt.eq(c.val, 2)
|
|
273
|
+
assrt.equal(c.val, 3)
|
|
274
|
+
|
|
275
|
+
with Context() as d:
|
|
276
|
+
assrt.equal(d.val, 2)
|
|
277
|
+
raise Exception('error')
|
|
278
|
+
assrt.equal(d.val, 3)
|
|
279
|
+
|
|
280
|
+
class Throws:
|
|
281
|
+
|
|
282
|
+
def __enter__(self):
|
|
283
|
+
pass
|
|
284
|
+
|
|
285
|
+
def __exit__(self):
|
|
286
|
+
pass
|
|
287
|
+
|
|
288
|
+
assrt.throws(
|
|
289
|
+
def ():
|
|
290
|
+
with Throws():
|
|
291
|
+
raise Exception('error')
|
|
292
|
+
, Exception
|
|
293
|
+
)
|
|
294
|
+
|
|
295
|
+
class X:
|
|
296
|
+
|
|
297
|
+
def __init__(self):
|
|
298
|
+
self.a = 3
|
|
299
|
+
|
|
300
|
+
class Y(X):
|
|
301
|
+
pass
|
|
302
|
+
|
|
303
|
+
class Z(Y):
|
|
304
|
+
pass
|
|
305
|
+
|
|
306
|
+
assrt.equal(Z().a, X().a)
|
|
307
|
+
assrt.ok(repr(Z()).indexOf('<__main__.Z') == 0)
|
|
308
|
+
assrt.ok(str(Z()).indexOf('<__main__.Z') == 0)
|
|
309
|
+
|
|
310
|
+
from __python__ import bound_methods
|
|
311
|
+
|
|
312
|
+
class U:
|
|
313
|
+
|
|
314
|
+
def __init__(self, a):
|
|
315
|
+
self.a = a
|
|
316
|
+
|
|
317
|
+
def val(self):
|
|
318
|
+
return self.a
|
|
319
|
+
|
|
320
|
+
u = U(3)
|
|
321
|
+
f = u.val
|
|
322
|
+
assrt.equal(f(), u.val())
|
|
323
|
+
|
|
324
|
+
class U2(U):
|
|
325
|
+
|
|
326
|
+
def val(self):
|
|
327
|
+
return self.a * 2
|
|
328
|
+
|
|
329
|
+
u = U2(3)
|
|
330
|
+
f = u.val
|
|
331
|
+
assrt.equal(f(), 6)
|
|
332
|
+
|
|
333
|
+
class M1:
|
|
334
|
+
|
|
335
|
+
def f1(self):
|
|
336
|
+
return 'M1-f1'
|
|
337
|
+
|
|
338
|
+
def f3(self):
|
|
339
|
+
return 'M1-f3'
|
|
340
|
+
|
|
341
|
+
class M0:
|
|
342
|
+
|
|
343
|
+
def f0(self):
|
|
344
|
+
return 'M0-f0'
|
|
345
|
+
|
|
346
|
+
class M2(M0):
|
|
347
|
+
|
|
348
|
+
@property
|
|
349
|
+
def prop(self):
|
|
350
|
+
return self._p or 'prop'
|
|
351
|
+
|
|
352
|
+
@prop.setter
|
|
353
|
+
def prop(self, val):
|
|
354
|
+
self._p = val
|
|
355
|
+
|
|
356
|
+
def f1(self):
|
|
357
|
+
return 'M2-f1'
|
|
358
|
+
|
|
359
|
+
def f2(self):
|
|
360
|
+
return 'M2-f2'
|
|
361
|
+
|
|
362
|
+
def f3(self):
|
|
363
|
+
return 'M2-f3'
|
|
364
|
+
|
|
365
|
+
class Child(M1, M2):
|
|
366
|
+
|
|
367
|
+
def f3(self):
|
|
368
|
+
return 'Child-f3'
|
|
369
|
+
|
|
370
|
+
c = Child()
|
|
371
|
+
assrt.equal(c.f0(), 'M0-f0')
|
|
372
|
+
assrt.equal(c.f1(), 'M1-f1')
|
|
373
|
+
assrt.equal(c.f2(), 'M2-f2')
|
|
374
|
+
assrt.equal(c.f3(), 'Child-f3')
|
|
375
|
+
assrt.equal(c.prop, 'prop')
|
|
376
|
+
c.prop = 1
|
|
377
|
+
assrt.equal(c.prop, 1)
|
|
378
|
+
assrt.ok(isinstance(c, Child))
|
|
379
|
+
assrt.ok(isinstance(c, M1))
|
|
380
|
+
assrt.ok(isinstance(c, M2))
|
|
381
|
+
assrt.ok(isinstance(c, M0))
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
class B1:
|
|
385
|
+
|
|
386
|
+
def __init__(self):
|
|
387
|
+
self.b1 = 1
|
|
388
|
+
|
|
389
|
+
def one(self):
|
|
390
|
+
return self.two()
|
|
391
|
+
|
|
392
|
+
def two(self):
|
|
393
|
+
return self.b1
|
|
394
|
+
|
|
395
|
+
class B2(B1):
|
|
396
|
+
|
|
397
|
+
def __init__(self):
|
|
398
|
+
self.b2 = 2
|
|
399
|
+
B1.__init__(self)
|
|
400
|
+
|
|
401
|
+
def two(self):
|
|
402
|
+
return self.b2
|
|
403
|
+
assrt.equal(B2().two(), 2)
|
|
404
|
+
assrt.equal(B2().b1, 1)
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
class Cvar:
|
|
408
|
+
a = b = 0
|
|
409
|
+
def __init__(self):
|
|
410
|
+
Cvar.a += 1
|
|
411
|
+
|
|
412
|
+
def inc(self):
|
|
413
|
+
Cvar.a += 1
|
|
414
|
+
|
|
415
|
+
def val(self):
|
|
416
|
+
return Cvar.a
|
|
417
|
+
|
|
418
|
+
def incb(self):
|
|
419
|
+
Cvar.b += 1
|
|
420
|
+
return Cvar.b
|
|
421
|
+
|
|
422
|
+
assrt.equal(Cvar.a, 0)
|
|
423
|
+
c = Cvar()
|
|
424
|
+
assrt.equal(Cvar.a, 1)
|
|
425
|
+
c.inc()
|
|
426
|
+
assrt.equal(Cvar.a, 2)
|
|
427
|
+
assrt.equal(Cvar().val(), 3)
|
|
428
|
+
assrt.equal(Cvar().incb(), 1)
|
|
429
|
+
assrt.equal(Cvar().incb(), 2)
|
|
430
|
+
|
|
431
|
+
class anon_func_in_class:
|
|
432
|
+
|
|
433
|
+
f = def():
|
|
434
|
+
func_var = 1
|
|
435
|
+
return func_var
|
|
436
|
+
|
|
437
|
+
def func_var(self):
|
|
438
|
+
pass
|
|
439
|
+
|
|
440
|
+
assrt.equal(jstype(anon_func_in_class.prototype.func_var), 'function')
|
|
441
|
+
anon_func_in_class.prototype.f()
|
|
442
|
+
assrt.equal(jstype(anon_func_in_class.prototype.func_var), 'function')
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
def decorate(cls):
|
|
446
|
+
assrt.equal(cls.prototype.somevar, 1)
|
|
447
|
+
return cls
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
@decorate
|
|
451
|
+
class decorated:
|
|
452
|
+
somevar = 1
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
# globals: assrt
|
|
2
|
+
# ARRAYS
|
|
3
|
+
|
|
4
|
+
# immutables
|
|
5
|
+
a = [4,5,6,7]
|
|
6
|
+
|
|
7
|
+
# mutables
|
|
8
|
+
class Item:
|
|
9
|
+
pass
|
|
10
|
+
i0 = Item()
|
|
11
|
+
i1 = Item()
|
|
12
|
+
i2 = Item()
|
|
13
|
+
b = [i0, i1]
|
|
14
|
+
assrt.ok(i0 != i1)
|
|
15
|
+
|
|
16
|
+
# access and slicing
|
|
17
|
+
i = -1
|
|
18
|
+
assrt.equal(len(a), 4)
|
|
19
|
+
assrt.equal(a[-1], 7)
|
|
20
|
+
assrt.equal(a[i], a[-1])
|
|
21
|
+
assrt.equal(a[-2], 6)
|
|
22
|
+
assrt.deepEqual(a[1:3], [5,6])
|
|
23
|
+
assrt.deepEqual(a[:3], [4,5,6])
|
|
24
|
+
assrt.deepEqual(a[2:], [6,7])
|
|
25
|
+
assrt.ok(7 in a)
|
|
26
|
+
assrt.ok(i1 in b)
|
|
27
|
+
assrt.ok(i2 not in b)
|
|
28
|
+
assrt.ok(b == b.copy())
|
|
29
|
+
assrt.ok([1] in [[1], [2]])
|
|
30
|
+
assrt.ok([3] not in [[1], [2]])
|
|
31
|
+
|
|
32
|
+
if jstype(Symbol) is 'function':
|
|
33
|
+
ss = Symbol('s')
|
|
34
|
+
sym = {ss:1}
|
|
35
|
+
assrt.equal(sym[ss], 1)
|
|
36
|
+
def keyf():
|
|
37
|
+
return 1
|
|
38
|
+
assrt.equal({keyf():2}[keyf()], 2)
|
|
39
|
+
|
|
40
|
+
# assignment
|
|
41
|
+
a[-1] = 9
|
|
42
|
+
assrt.equal(a[-1], 9)
|
|
43
|
+
s = [0,1,2,3,4]
|
|
44
|
+
s[:2] = [8, 9]
|
|
45
|
+
assrt.deepEqual(s, [8,9,2,3,4])
|
|
46
|
+
s[1:2] = [5]
|
|
47
|
+
assrt.deepEqual(s, [8,5,2,3,4])
|
|
48
|
+
s[-2:] = [1,2]
|
|
49
|
+
assrt.deepEqual(s, [8,5,2,1,2])
|
|
50
|
+
|
|
51
|
+
# extended slices
|
|
52
|
+
b = list(range(11))
|
|
53
|
+
s = 'abcde'
|
|
54
|
+
assrt.deepEqual(b[::2], [0, 2, 4, 6, 8, 10])
|
|
55
|
+
assrt.deepEqual(b[::-1], list(range(10,-1,-1)))
|
|
56
|
+
assrt.deepEqual(b[7:0:-1], [7, 6, 5, 4, 3, 2, 1])
|
|
57
|
+
assrt.deepEqual(b[7:1:-2], [7, 5, 3])
|
|
58
|
+
assrt.equal(s[::2], 'ace')
|
|
59
|
+
assrt.equal(s[::-1], 'edcba')
|
|
60
|
+
assrt.equal(s[4:0:-1], 'edcb')
|
|
61
|
+
assrt.equal(s[4:1:-2], 'ec')
|
|
62
|
+
|
|
63
|
+
# sorting
|
|
64
|
+
a = [2,1,3]
|
|
65
|
+
a.pysort(key=def(x):return 0;)
|
|
66
|
+
assrt.deepEqual(a, [2,1,3]) # stable sort
|
|
67
|
+
a.pysort(reverse=True)
|
|
68
|
+
assrt.deepEqual(a, [3,2,1])
|
|
69
|
+
a.pysort()
|
|
70
|
+
assrt.deepEqual(a, [1,2,3])
|
|
71
|
+
|
|
72
|
+
# misc interface
|
|
73
|
+
a = [1,2,3]
|
|
74
|
+
assrt.equal(a.pypop(), 3)
|
|
75
|
+
assrt.deepEqual(a, [1,2])
|
|
76
|
+
assrt.equal(a.pypop(0), 1)
|
|
77
|
+
assrt.deepEqual(a, [2])
|
|
78
|
+
|
|
79
|
+
# strings
|
|
80
|
+
assrt.ok("tes" in "this is a test")
|
|
81
|
+
|
|
82
|
+
one = "one"
|
|
83
|
+
two = "two"
|
|
84
|
+
one, two = two, one
|
|
85
|
+
[x, y, z] = 'x', 'y', 'z'
|
|
86
|
+
assrt.equal(one, "two")
|
|
87
|
+
assrt.equal(two, "one")
|
|
88
|
+
assrt.equal(x, 'x')
|
|
89
|
+
assrt.equal(y, 'y')
|
|
90
|
+
assrt.equal(z, 'z')
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
# DICTIONARIES
|
|
94
|
+
d0 = {'a':'b','c':2, 'd': 'd',}
|
|
95
|
+
d1 = {
|
|
96
|
+
'foo': 1,
|
|
97
|
+
"bar": "baz",
|
|
98
|
+
"fun1": def():
|
|
99
|
+
return 5
|
|
100
|
+
,
|
|
101
|
+
'fun2': def(c):
|
|
102
|
+
return c+1
|
|
103
|
+
}
|
|
104
|
+
d2 = dict([[1,2], [2,3]], a='b')
|
|
105
|
+
|
|
106
|
+
# access
|
|
107
|
+
assrt.ok('foo' in d1)
|
|
108
|
+
assrt.equal(d0.a, d0['a'])
|
|
109
|
+
assrt.equal(d1['fun1'](), 5)
|
|
110
|
+
assrt.equal(d1.fun2(3), 4)
|
|
111
|
+
assrt.equal(len(d0), 3)
|
|
112
|
+
assrt.equal(d2.get(1), 2)
|
|
113
|
+
assrt.equal(d2.get('a'), 'b')
|
|
114
|
+
assrt.deepEqual(d2.popitem(), ['a', 'b'])
|
|
115
|
+
|
|
116
|
+
# assignment
|
|
117
|
+
d1["bar"] += "!"
|
|
118
|
+
assrt.equal(d1.bar, "baz!")
|
|
119
|
+
|
|
120
|
+
# nested comparisons
|
|
121
|
+
x = 3
|
|
122
|
+
assrt.ok(1 < x <= 3)
|
|
123
|
+
assrt.ok(1 < x*x > 3)
|
|
124
|
+
assrt.ok(1 < (x+=1) < 5) # check that only one increment occurs
|
|
125
|
+
assrt.equal(x, 4)
|
|
126
|
+
|
|
127
|
+
# list comprehensions
|
|
128
|
+
e0 = [i*i for i in [0,1,2,3]]
|
|
129
|
+
e1 = [x+y for x, y in enumerate(range(5,0,-1))]
|
|
130
|
+
e2 = [e0+1 for e0 in range(6) if e0%3]
|
|
131
|
+
assrt.deepEqual(e0, [0,1,4,9])
|
|
132
|
+
assrt.deepEqual(e1, [5,5,5,5,5])
|
|
133
|
+
assrt.deepEqual(e2, [2,3,5,6])
|
|
134
|
+
hash = {
|
|
135
|
+
"foo": 1,
|
|
136
|
+
"bar": 1,
|
|
137
|
+
"baz": 1,
|
|
138
|
+
}
|
|
139
|
+
assrt.deepEqual(Object.keys(hash), [k for k in hash])
|
|
140
|
+
|
|
141
|
+
a = {1:2}
|
|
142
|
+
b = {2:1}
|
|
143
|
+
assrt.ok(a != b)
|
|
144
|
+
b = a
|
|
145
|
+
assrt.ok(a == b)
|
|
146
|
+
b = {1:2}
|
|
147
|
+
assrt.ok(a == b)
|
|
148
|
+
assrt.ok(v'{1:2}' == v'{1:2}')
|
|
149
|
+
|
|
150
|
+
# iterating over TypedArrays
|
|
151
|
+
assrt.deepEqual(list(Uint8Array(2)), [0, 0])
|
|
152
|
+
assrt.deepEqual(list(Int32Array([1,-1])), [1,-1])
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# vim:fileencoding=utf-8
|
|
2
|
+
# License: BSD Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net>
|
|
3
|
+
|
|
4
|
+
def double(f):
|
|
5
|
+
return def (x):
|
|
6
|
+
return 2 * f(x)
|
|
7
|
+
|
|
8
|
+
def triple(f):
|
|
9
|
+
return def(x):
|
|
10
|
+
return 3 * f(x)
|
|
11
|
+
|
|
12
|
+
@double
|
|
13
|
+
def half(x):
|
|
14
|
+
return x // 2
|
|
15
|
+
|
|
16
|
+
@double
|
|
17
|
+
def f1(x):
|
|
18
|
+
return x
|
|
19
|
+
|
|
20
|
+
@double
|
|
21
|
+
@triple
|
|
22
|
+
def f2(x):
|
|
23
|
+
return x
|
|
24
|
+
|
|
25
|
+
def append_one(f):
|
|
26
|
+
return def(x):
|
|
27
|
+
ans = f(x)
|
|
28
|
+
ans.push(1)
|
|
29
|
+
return ans
|
|
30
|
+
|
|
31
|
+
def append_two(f):
|
|
32
|
+
return def(x):
|
|
33
|
+
ans = f(x)
|
|
34
|
+
ans.push(2)
|
|
35
|
+
return ans
|
|
36
|
+
|
|
37
|
+
@append_two
|
|
38
|
+
@append_one
|
|
39
|
+
def f3():
|
|
40
|
+
return []
|
|
41
|
+
|
|
42
|
+
o = {'double':double}
|
|
43
|
+
|
|
44
|
+
@o.double
|
|
45
|
+
def f4():
|
|
46
|
+
return 1
|
|
47
|
+
|
|
48
|
+
assrt.equal(2, half(2))
|
|
49
|
+
assrt.equal(4, f1(2))
|
|
50
|
+
assrt.equal(12, f2(2))
|
|
51
|
+
assrt.equal(2, f4())
|
|
52
|
+
assrt.deepEqual([1, 2], f3())
|
|
53
|
+
|
|
54
|
+
def multiply(amt):
|
|
55
|
+
|
|
56
|
+
def wrapper(f):
|
|
57
|
+
return def ():
|
|
58
|
+
return amt * f()
|
|
59
|
+
|
|
60
|
+
return wrapper
|
|
61
|
+
|
|
62
|
+
@multiply(2)
|
|
63
|
+
def two():
|
|
64
|
+
return 1
|
|
65
|
+
|
|
66
|
+
@multiply(3)
|
|
67
|
+
def three():
|
|
68
|
+
return 1
|
|
69
|
+
|
|
70
|
+
@multiply(2)
|
|
71
|
+
@multiply(2)
|
|
72
|
+
def four():
|
|
73
|
+
return 1
|
|
74
|
+
|
|
75
|
+
assrt.equal(2, two())
|
|
76
|
+
assrt.equal(3, three())
|
|
77
|
+
assrt.equal(4, four())
|