Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/python-wasm
Path: blob/main/python/pylang/test/jsage.py
1396 views
1
# Test jsage language modifications
2
3
# ^ is xor in python by default
4
assrt.equal(2^3, 1)
5
assrt.equal(2**3, 8)
6
# note the precedence, despite how I wrote this!
7
assrt.equal(2^3 + 1, 2^4)
8
9
from __python__ import exponent
10
# now ^ is exponent and ^^ is xor
11
assrt.equal(2^3, 8)
12
assrt.equal(2^^3, 1)
13
assrt.equal(2**3, 8)
14
15
# ^ really **is** exponentiation, not xor, since the tokenizer does it.
16
# This means the precedence is correct (i.e., very high).
17
assrt.equal(2^3 + 1, 9)
18
19
# note that eval is not changed. Maybe this is bad?
20
assrt.equal(eval('2^3'), 1)
21
22
23
from __python__ import no_exponent
24
# now ^ is back (and ^^ would be a syntax error - can't test this)
25
assrt.equal(2^3, 1)
26
27
# Ellipses range parsing
28
# Enable it:
29
from __python__ import ellipses
30
31
# With numerical literals
32
assrt.equal(str([1..5]), 'range(1, 6)')
33
34
# With expressions
35
a = 2; b = 7
36
assrt.equal(str([a+a..b+2]), 'range(4, 10)')
37
38
# With a function call
39
def f(n):
40
return n+1
41
assrt.equal(str([f(10)..f(1000)]), 'range(11, 1002)')
42
43
# With a floating point literal
44
assrt.equal(str([1.5..5]), 'range(1.5, 6)')
45
46
# Numerical literals
47
from __python__ import numbers
48
# will parse all numbers as one less!
49
def Number(s):
50
return parseFloat(s) - parseFloat('1.0')
51
assrt.equal(2.5, parseFloat('1.5'))
52
53