简体   繁体   English

Python动态输入验证

[英]Python dynamic input validation

I have a control structure that looks something like this: 我有一个看起来像这样的控件结构:

entries = [
    {'sql_col' : 'name', 'display_text' : 'Enter your name: '},
    {'sql_col' : 'email', 'display_text' : 'Enter your email: '},
    {'sql_col' : 'zip', 'display_text' : 'Enter your Zip Code: '},
]

Each entry in the entries list represents a text box that a user will fill in a dialog box. entries列表中的每个条目都代表一个文本框,用户将在其中填写一个对话框。 I would like to be able to specify a validation function for each input within that structure, ie: 我希望能够为该结构内的每个输入指定一个验证函数,即:

entries = [
    {'sql_col' : 'name', ... , 'validate' : lambda x: return is_name_valid(x)},
    {'sql_col' : 'email', ... , 'validate' : lambda x: return is_email_valid(x)},
    ...
]

Problem is I can't figure out the right syntax to use for something like this. 问题是我无法弄清楚用于此类内容的正确语法。 Is my entries syntax correct? 我的entries语法正确吗? How would I call the validation function later in the code and pass it the user's input as a parameter? 我该如何在代码的后面部分调用验证函数,并将用户输入作为参数传递给它?

Also, best answer is one where I don't need to install any new modules -- I don't have sufficient privileges on the system i'm using to do any installations. 另外,最好的答案是我不需要安装任何新模块的答案-我在用于进行任何安装的系统上没有足够的特权。

Problem is I can't figure out the right syntax to use for something like this. 问题是我无法弄清楚用于此类内容的正确语法。 Is my entries syntax correct? 我的输入语法正确吗?

Have you tried it? 你试过了吗? The error message you get should immediately tell you that it's not, and point to where it goes wrong, and give any reader who wants to help you a clue to solving it: 您收到的错误消息应立即告诉您它不是,并指出出现问题的地方,并向希望帮助您解决问题的读者提供提示:

{'sql_col' : 'name', 'validate' : lambda x: return is_name_valid(x)},
                                                 ^
SyntaxError: invalid syntax

The problem is that lambda doesn't allow statements, just expressions, and return is a statement. 问题在于, lambda不允许语句,仅允许表达式,而return是语句。 You don't need return in lambda , because it automatically returns whatever the expression evaluates to. 您不需要在lambda return ,因为它会自动返回表达式计算得出的结果。 So: 所以:

{'sql_col' : 'name', 'validate' : lambda x: is_name_valid(x)},

However, it's worth pointing out that lambda x: foo(x) does the exact same thing as just passing foo itself, except more verbosely and more slowly. 但是,值得指出的是, lambda x: foo(x)作用与传递foo本身完全相同,只是更加冗长和缓慢。 So, better: 因此,更好:

{'sql_col' : 'name', 'validate' : is_name_valid},

How would I call the validation function later in the code and pass it the user's input as a parameter? 我该如何在代码的后面部分调用验证函数,并将用户输入作为参数传递给它?

You access it the same way you access any other member of a dict—it doesn't matter that it happens to be a function, because functions are perfectly normal values as far as Python is concerned. 您可以像访问dict的其他任何成员一样来访问它-它并不偶然是函数,因为就Python而言,函数完全是正常值。

And you call it the same you as you call any other function—it doesn't matter that it's an expression rather than a literal name, because expressions that evaluate to a function (or something else callable) are perfectly normal callables as far as Python is concerned. 而且您将其称为与调用任何其他函数相同—没关系,它是一个表达式,而不是文字名称,因为就Python而言,求值函数(或其他可调用对象)的表达式是完全正常的可调用对象被关注到。

So: 所以:

if entry['validate'](value):
    print('Good value!')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM