web_python_editor/syntax_test.py

229 lines
4.2 KiB
Python

# Python Syntax Test
# Segments written by Kyler or (and mostly at that) from python docs
def func():
if 1900 < year < 2100 and 1 <= month <= 12 \
and 1 <= day <= 31 and 0 <= hour < 24 \
and 0 <= minute < 60 and 0 <= second < 60: # Looks like a valid date
return 1
month_names = ['Januari', 'Februari', 'Maart', # These are the
'April', 'Mei', 'Juni', # Dutch names
'Juli', 'Augustus', 'September', # for the months
'Oktober', 'November', 'December'] # of the year
# Here is an example of a correctly (though confusingly)
# indented piece of Python code:
def perm(l):
# Compute the list of all permutations of l
if len(l) <= 1:
return [l]
r = []
for i in range(len(l)):
s = l[:i] + l[i+1:]
p = perm(s)
for x in p:
r.append(l[i:i+1] + x)
return r
def perm2(l):
if len(l) <= 1:
return [l]
r = []
for i in range(len(l)):
s = l[:i] + l[i+1:]
p = perm(l[:i] + l[i+1:])
for x in p:
r.append(l[i:i+1] + x)
return r
'This string will not include \
backslashes or newline characters.'
re.compile("[A-Za-z_]" # letter or underscore
"[A-Za-z0-9_]*" # letter, digit or underscore
)
# Some examples of formatted string literals:
name = "Fred"
f"He said his name is {name!r}."
f"He said his name is {repr(name)}." # repr() is equivalent to !r
width = 10
precision = 4
value = decimal.Decimal("12.34567")
f"result: {value:{width}.{precision}}" # nested fields
today = datetime(year=2017, month=1, day=27)
f"{today:%B %d, %Y}" # using date format specifier
f"{today=:%B %d, %Y}" # using date format specifier and debugging
number = 1024
f"{number:#0x}" # using integer format specifier
foo = "bar"
f"{ foo = }" # preserves whitespace
line = "The mill's closed"
f"{line = }"
f"{line = :20}"
f"{line = !r:20}"
newline = ord('\n')
f"newline: {newline}"
def foo():
f"Not a docstring"
foo.__doc__ is None
# Triple and Multiline
"""Test" test"""
"""test
# test
test"""
r"hello\" world"
rb"hello\" world"
b"test"
"hello\" world"
# Some examples of integer literals:
7
2147483647
0o177
0b100110111
3
79228162514264337593543950336
0o377
0xdeadbeef
100_000_000_000
0b_1110_0101
# Some examples of floating point literals:
3.14
10.
.001
1e100
3.14e-10
0e0
3.14_15_93
# Some examples of imaginary literals:
3.14j
10.j
10j
.001j
1e100j
3.14e-10j
3.14_15_93j
if x < y < z: print(x); print(y); print(z)
print(sys.exception())
try:
raise TypeError
except:
print(repr(sys.exception()))
try:
raise ValueError
except:
print(repr(sys.exception()))
print(repr(sys.exception()))
try:
raise ExceptionGroup("eg",
[ValueError(1), TypeError(2), OSError(3), OSError(4)])
except* TypeError as e:
print(f'caught {type(e)} with nested {e.exceptions}')
except* OSError as e:
print(f'caught {type(e)} with nested {e.exceptions}')
try:
raise BlockingIOError
except* BlockingIOError as e:
print(repr(e))
# The return value of a function is determined by the last return statement executed
def foo():
try:
return 'try'
finally:
return 'finally'
with EXPRESSION as TARGET:
SUITE
with A() as a, B() as b:
SUITE
with A() as a:
with B() as b:
SUITE
with (
A() as a,
B() as b,
):
SUITE
match = 1
case = 1
flag = False
match (100, 200):
case (100, 300): # Mismatch: 200 != 300
print('Case 1')
case (100, 200) if flag: # Successful match, but guard fails
print('Case 2')
case (100, y): # Matches and binds y to 200
print(f'Case 3, y: {y}')
case _: # Pattern not attempted
print('Case 4, I match anything!')
@f1(arg)
@f2
def func(): pass
def whats_on_the_telly(penguin=None):
if penguin is None:
penguin = []
penguin.append("property of the zoo")
return penguin
async def func(param1, param2):
do_stuff()
await some_coroutine()
async for TARGET in ITER:
SUITE
else:
SUITE2
async with EXPRESSION as TARGET:
SUITE
class Foo:
pass
class Foo(object):
pass
@f1(arg)
@f2
class Foo: pass