Kdb 验证输入参数的功能

Kdb 验证输入参数的功能,kdb,Kdb,定义了验证输入参数的方法: `validate_paramaters{[Date;Code;CodeType;Param] if[ not (14h=abs[type[Date]]); [ .log.info["\tError - Exiting function - Date is not of the correct type"]; : `$"Error - Date is not of the correct type"

定义了验证输入参数的方法:

`validate_paramaters{[Date;Code;CodeType;Param]
 if[ not (14h=abs[type[Date]]);
      [
           .log.info["\tError - Exiting function - Date is not of the correct type"];
           : `$"Error - Date is not of the correct type"
         ]
       ];
if[ not (11h=abs[type[Code]]);
         [
           .log.info["\tError - Exiting function - Code is not of the correct type"];
           : `$"Error - Code is not of the correct type"
         ]
       ];
if[ not (-11h=type[CodeType]);
         [
           .log.info["\tError - Exiting function - CodeType is not of the correct type"];
           : $"Error - CodeType is not of the correct type"
         ]
       ];
:`validated

}`
但是现在代码类型的类型添加了更多的附加内容:

 -11h=type[CodeType] `also can be` 99h=type[CodeType]
请给我一个指导,我现在如何验证代码类型

将您的
not(-11h=type[CodeType])
替换为
not any-1199h in type CodeType

中的关键字
允许您将代码类型的类型与数字类型列表进行比较。这将返回一个布尔列表。例如:

q)CodeType:`sample`dictionary!`a`b
q)-11 99h in type CodeType
01b
如果列表中的任何成员为True,则前面的
any
返回
1b

q)any -11 99h in type CodeType
1b
最后,如果代码类型不是上述类型之一,您似乎会抛出错误,因此我们将用
NOT
替换
NOT(-11h=type[CodeType])
NOT any-1199h类型代码类型

中的关键字
允许您将代码类型的类型与数字类型列表进行比较。这将返回一个布尔列表。例如:

q)CodeType:`sample`dictionary!`a`b
q)-11 99h in type CodeType
01b
如果列表中的任何成员为True,则前面的
any
返回
1b

q)any -11 99h in type CodeType
1b

最后,如果代码类型不是上述类型之一,您似乎会抛出错误,因此我们将使用
NOT

来预先说明这一切。除了Jemma的回答之外,您还可以尝试更简洁的方法:

validate:{[d;names;types]
  if[count fails:key[d] where not any each types=type each d[names];
  :"error - exiting - ",("," sv string fails)," are not of correct type"];
 };
此函数的第一个参数是一个字典(
d
),其中键是参数的名称,值是它们对应的值。此参数可以与正在查询的变量的名称以及它们应该是的类型一起传递。如果要查找多个要符合的类型,则应使用嵌套列表

例如,这是一本包含有效条目的词典

q)d:`date`code`codetype!(.z.d;`some`code`to`test;`thetypeofmycode)
q)validate[d;`date`code`codetype;(-14;11;(-11 99h))]
q)
但是,如果使用无效的数据类型更新字典,您可以看到它将返回一个错误

q)d[`code]:"wrongtype"
q)d[`date]:.z.p
q)validate[d;`date`code`codetype;(-14;11;(-11 99h))]
"error - exiting - date,code are not of correct type"
请注意,类型列表必须与字典的顺序相同

要合并.log.info,您可以调整上述功能:

validate:{[d;names;types]if[count fails:key[d] where not any each types=type each d key d;
  .lg.info"error - exiting - ",("," sv string[fails])," are not of correct type";
  :"error: ",("," sv string[fails])," - type incorrect"]}

参考上面的示例,这将返回相同的结果,但这次也使用.log.info记录错误。

除了Jemma的响应之外,您还可以尝试更简洁的方法:

validate:{[d;names;types]
  if[count fails:key[d] where not any each types=type each d[names];
  :"error - exiting - ",("," sv string fails)," are not of correct type"];
 };
此函数的第一个参数是一个字典(
d
),其中键是参数的名称,值是它们对应的值。此参数可以与正在查询的变量的名称以及它们应该是的类型一起传递。如果要查找多个要符合的类型,则应使用嵌套列表

例如,这是一本包含有效条目的词典

q)d:`date`code`codetype!(.z.d;`some`code`to`test;`thetypeofmycode)
q)validate[d;`date`code`codetype;(-14;11;(-11 99h))]
q)
但是,如果使用无效的数据类型更新字典,您可以看到它将返回一个错误

q)d[`code]:"wrongtype"
q)d[`date]:.z.p
q)validate[d;`date`code`codetype;(-14;11;(-11 99h))]
"error - exiting - date,code are not of correct type"
请注意,类型列表必须与字典的顺序相同

要合并.log.info,您可以调整上述功能:

validate:{[d;names;types]if[count fails:key[d] where not any each types=type each d key d;
  .lg.info"error - exiting - ",("," sv string[fails])," are not of correct type";
  :"error: ",("," sv string[fails])," - type incorrect"]}

参考上面的示例,这将返回相同的结果,但这次也使用.log.info记录错误。

可能会更干净一点,而不是在-11 99h中键入[CodeType]。更容易阅读并且不需要使用
任何
可能会更干净一点
而不是在-11 99h中键入[CodeType]。易于阅读,无需使用
any