编程语句
Python Parser 中的编程语句 if, for, while, break, continue, pass 与 Python 保持一致。
if 语句
Python Parser 支持 if-else, if-elif-else。
x = 10
if x == 0:
print("Zero")
elif x > 0:
print("Positive")
else:
print("Negative")for 语句
for 语句用来遍历可迭代的序列,例如:字符串、list、元组、字典(dict.items())、集合。
使用
for语句遍历range对象:for i in range(3): print(i) // output: 0 1 2使用
for语句遍历字符串:for item in "ddb": print(item) // output: d d b使用
for语句遍历列表:words = ['cat', 'window', 'defenestrate'] for w in words: print(w, len(w)) // output: cat 3 window 6 defenestrate 12使用
for语句遍历字典:dict_1 = {'Name': 'Zara', 'Age': 7, 'Class': 'First','Address':'Beijing'} for key in dict_1: print(key, ":", dict_1[key]) // output: Name : Zara Age : 7 Class : First Address : Beijing使用
for语句遍历集合:A = {'1','2','star'} for item in A: print(item,end='') // output: 1 2 star
while 语句
i = 1
while i<=10:
if i == 5:
break
print(i)
i+=1
// output:
1
2
3
4break 和 continue 语句
break 和 continue 作为关键字,只能用在循环中。break 用于终止循环,而 continue 是结束本次循环,继续下一次循环。
for i in range(1, 6):
if i == 4:
print('finish')
break # 终止循环的执行
print('I am ' + str(i) + ' years old')
// output:
I am 1 years old
I am 2 years old
I am 3 years old
finishfor i in range(1, 6):
if i == 4:
print('finish')
continue # 结束本次循环,继续下一次循环
print('I am ' + str(i) + ' years old')
// output:
I am 1 years old
I am 2 years old
I am 3 years old
finish
I am 5 years oldpass 语句
pass 语句通常用于占位,保持格式完整。
for letter in 'pear':
if letter == 'a':
pass
print ('This is pass block')
print ('Current Letter :', letter)
print ("Good bye!")
// output:
Current Letter : p
Current Letter : e
This is pass block
Current Letter : a
Current Letter : r
Good bye!循环语句嵌套
first = [2, 3, 4]
second = [20, 30, 40]
final = []
for i in first:
for j in second:
final.append(i+j)
print(final)
// output: [22, 32, 42, 23, 33, 43, 24, 34, 44]assert 语句
DolphinDB 的 assert 语句支持 assert expression 或 assert string, expression,但 Python Parser 的 assert 语句支持 assert expression 和 assert expression [, arguments]。
assert 1==1
assert 1==2, '1 不等于 2'
// output: Testing case adhocTesting_"1 不等于 2" failedtry-catch 语句
当前版本的 Python Parser 仅支持捕捉所有异常,暂不支持捕捉具体异常。
def this_fails():
x = 1/`7
try:
this_fails()
except Exception as e:
print('error occur:' + e)
// output: "error occur:SYSTEM_Operator" : "error occur:Both arguments for ratio must be numbers."raise 语句
DolphinDB 使用 throw 抛出一个用户自定义异常,而 Python Parser 使用 raise 语句抛出用户自定义异常。其语法为:raise expression。
def test_exception():
try:
raise "asdf"
except Exception as ex:
assert ex[0]=="USER"
assert ex[1]=="asdf"
assert typestr(ex) == "STRING PAIR"
try:
assert 1 == 112
1 + 2
except Exception as ex:
pass
a = 123
pass
pass
assert ex[0]=="C++"
assert ex[1]=="Testing case adhocTesting failed"
print("test_exception test pass")
test_exception()
// output: test_exception test pass注释
Python Session 中代码注释方式与 DolphinDB 不同,但与同 Python 保持一致。
单行注释以 # 开头,例如:
# This is a comment
print("Hello, World!")多行注释用三个单引号 ''' 或者三个双引号 """ 将注释括起来,例如:
'''
This is the first comment
This is the second comment
This is the third comment
'''
print("Hello, World!")模块
DolphinDB 的模块文件以 .dos 作为后缀,位于“<DolphinDB目录>/modules”下。DolphinDB Session 使用 use 引用模块文件。
Python Parser 的模块文件以 .py 作为后缀,位于“<DolphinDB目录>/python”或用户自定义的路径(需使用 sys.path 设置路径)下。若使用自定义路径,则需要重启 server 后,才可在 Python Session 中通过 import 引用这些模块。
首先,在 "<DolphinDB目录>/python" 下创建一个 mod_test.py 的模块文件;
def f():
a = 10
b = 15
return a + b通过 import 导入模块:
import mod_test
mod_test.f()
// output: 16或通过 from..import.. 导入模块内的某个函数:
from mod_test f
f()
// output: 16若模块放在其它路径,则需通过 sys 设置模块的搜索路径:
import sys
sys.path.append("/path/to/module")清除模块:
undef("mod_test")