为什么在函数内用parseExpr执行会报SQL context is not initialized yet

Nicole
  • 670

在DolphinDB GUI内执行下面代码:

def yoy_continuous_validation(X, threshold, validation_way, continuous_len){
 if (not(string(validation_way) in ['>','>=','<','<=','='])){
  return NULL
 }
 m = parseExpr("iif(msum(iif(yoy(X)" + iif(string(validation_way) == "=","==",validation_way) + "threshold,1,0),continuous_len) == continuous_len,1,0)").eval()
 return m
}

报错SQL context is not initialized yet,parseExpr拼接的语句为

iif(msum(iif(yoy(X)>threshold,1,0),continuous_len) == continuous_len,1,0)

直接执行不会报错,是什么原因?

回复
阅读 351
1 个回答
✓ 已被采纳

如DolphinDB用户手册中所述,parseExpr函数解析变量时,首先搜索会话中的局部变量,再搜索共享变量,但不会搜索函数体内定义的局部变量。这里是因为parseExpr中用到了函数参数变量X的缘故。代码优化可如下所示:

def yoy_continuous_validation(X, threshold, validation_way, continuous_len){
 cmps = [gt, ge, lt, le, eq]
 index = ['>','>=','<','<=','='].find(string(validation_way))
 if(index < 0)  return NULL
 return  int(msum(cmps[index](yoy(X), threshold), continuous_len) == continuous_len)
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
你知道吗?

宣传栏