类似的功能 - 一个有效,其余的无效 - 也许一双新的眼睛可以看到问题? (Python CGI)
正在开发低强度的用户注册脚本。在脚本的错误检查部分(实际上只是检查以确保字段不留空),“addressError”函数完全可以正常工作。
但是,如果我尝试在任何其他字段中创建错误,脚本就会超时。希望有人能看到我错过了什么?
什么有效:
def check_address(user, formdata):
if formdata.has_key("streetAddress") and formdata["streetAddress"].value != "":
streetAddress = formdata["streetAddress"].value
return streetAddress
else:
addressError(user, formdata)
def addressError(user, formdata):
feedback = {"blank_address" : "Please enter a valid street address! We won't mail you anything you don't order. Promise."}
stepTwoError(user, formdata, "blank_address", feedback)
什么无效:
def check_city(user, formdata):
if formdata.has_key("city") and formdata["city"].value != "":
city = formdata["city"].value
return city
else:
cityError(user, formdata)
def cityError(user, formdata):
feedback = {"blank_city" : "Please enter a city!"}
stepTwoError(user, formdata, "blank_city", feedback)
def check_state(user, formdata):
if formdata.has_key("state") and formdata["state"].value != "":
state = formdata["state"].value
return state
else:
stateError(user, formdata)
def stateError(user, formdata):
feedback = {"blank_state" : "Please enter a state!"}
stepTwoError(user, formdata, "blank_state", feedback)
编辑: 调用这些方法的代码:
def get_stepTwoData(user, formdata):
firstName, lastName = check_name(user, formdata)
streetAddress = check_address(user, formdata)
city = check_city(user, formdata)
state = check_state(user, formdata)
zipCode = check_zipCode(user, formdata)
return firstName, lastName, streetAddress, city, state, zipCode
就
if regStep == "2":
firstName, lastName, streetAddress, city, state, zipCode = get_stepTwoData(user, formdata)
make_step3(user, regStep, firstName, lastName, streetAddress, city, state, zipCode, formdata)
我而言,这三个块完全相同,“地址”、“州”和“城市”可以互换使用。事实上,我可能可以编写一个 for 函数来同时涵盖所有这三个功能。那么为什么其中两个不起作用呢?
(注意:我不包括 stepTwoError() 函数,因为我知道它可以工作,因为它在被 addressError 调用后可以工作 - 因为该函数不会改变,所以我知道它可以工作。因此我只能假设问题出在发布的块中。)
如果我生成的数据没有错误,则脚本执行得很好,所以我知道从表单读取数据时没有错误。所有变量都会在下一页上返回,没有任何问题。
这已经困扰我好几天了。我就是不明白。我希望无论问题是什么,都会因为它的简单而让我感到非常愚蠢。
谢谢!
编辑:好的,错误似乎出现在我的 HTML 中。我还没有弄清楚脚本中到底是什么导致了错误,但是在 html 中,除了地址字段之外,所有表单都是“input type='text' name='[name]'”,即“input type = ' text'” - 那些额外的空格显然把事情弄乱了。
感谢那些耐心回答的人,是的,我确实重新编写了代码,以更加考虑并减少转录错误的可能性。
-汤姆
Working on a low intensity user registration script. In the error checking portion of the script (which effectively just checks to make sure the fields aren't left blank), the "addressError" function works without any problems at all.
But if I try to create errors in any other field, the script times out. Hopefully someone can see what it is that I'm missing?
What Works:
def check_address(user, formdata):
if formdata.has_key("streetAddress") and formdata["streetAddress"].value != "":
streetAddress = formdata["streetAddress"].value
return streetAddress
else:
addressError(user, formdata)
def addressError(user, formdata):
feedback = {"blank_address" : "Please enter a valid street address! We won't mail you anything you don't order. Promise."}
stepTwoError(user, formdata, "blank_address", feedback)
What Doesn't Work:
def check_city(user, formdata):
if formdata.has_key("city") and formdata["city"].value != "":
city = formdata["city"].value
return city
else:
cityError(user, formdata)
def cityError(user, formdata):
feedback = {"blank_city" : "Please enter a city!"}
stepTwoError(user, formdata, "blank_city", feedback)
def check_state(user, formdata):
if formdata.has_key("state") and formdata["state"].value != "":
state = formdata["state"].value
return state
else:
stateError(user, formdata)
def stateError(user, formdata):
feedback = {"blank_state" : "Please enter a state!"}
stepTwoError(user, formdata, "blank_state", feedback)
EDIT:
Code that Calls these methods:
def get_stepTwoData(user, formdata):
firstName, lastName = check_name(user, formdata)
streetAddress = check_address(user, formdata)
city = check_city(user, formdata)
state = check_state(user, formdata)
zipCode = check_zipCode(user, formdata)
return firstName, lastName, streetAddress, city, state, zipCode
and in main:
if regStep == "2":
firstName, lastName, streetAddress, city, state, zipCode = get_stepTwoData(user, formdata)
make_step3(user, regStep, firstName, lastName, streetAddress, city, state, zipCode, formdata)
As far as I'm concerned these three blocks are exactly the same, with "address", "state", and "city" used interchangeably. In fact, I could probably write a for function to cover all three of these at once. So why do two of them not work?
(Note: I'm not including the stepTwoError() function because I know that it works, since it works after being called by addressError - since that function doesn't change, i know it works. Therefore I can only assume the problem lies in the blocks posted.)
If I generate data with no errors, the script executes just fine, so I know there's no error in reading the data from the form. All the variables get returned on the next page with no issues.
This has been plaguing me for days. I just don't get it. I hope whatever the problem is will make me feel really stupid due to its simplicity.
Thanks!
EDIT: Okay, the error appears to be in my HTML. I haven't figured out exactly what in the script is causing the error, but in the html all forms are "input type='text' name='[name]'" except the address field, which is "input type = 'text'" - those extra spaces are messing something up, clearly.
Thanks to those who answered patiently, and yes, I did re-write the code to be more conside with less transcription error possibilities.
-tom
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Python 具有动态创建函数的机制,在这种情况下很有用。一种是元类,它需要一些双重间接思考,并且思考起来可能很曲折。一个更简单的函数(也是最终学习元类的垫脚石)是闭包,它是一种通过在本地定义一个函数然后返回该本地函数来创建函数的函数。以下是如何使用闭包生成所有 check_xxx 函数:
闭包非常适合消除复制粘贴编码,以及在粘贴早期函数的副本后,一个或多个变量名称引用保持不变的常见错误从原来的。使用闭包,您可以更加确信您的单独函数确实是原始模板的有效创建。
但我同意@JohnMachin,我也怀疑你的错误确实存在于已知良好且有效的方法中,
stepTwoError
。尝试以不同的顺序评估您的输入字段。 stepTwoError 是否有一个可变对象或列表的默认参数?也许这被假定为空,但保留了先前调用的值?或者它是否会更新全局变量并将其置于脏状态,从而导致第二次调用错误?Python has mechanisms for creating functions dynamically that are useful in situations like this. One is the metaclass, which requires some double-indirect thinking and can be tortuous to think thru. A simpler one (and a stepping stone to eventually learning metaclasses) is the closure, a function that creates a function by defining one locally and then returning that local function. Here is how you can use a closure to generate all of your check_xxx functions:
Closures are great for eliminating copy-paste coding, and the frequent bugs where, after pasting a copy of an earlier function, one or more variable name references is left unchanged from the original. Using a closure, you can be much more confident that your separate functions are really valid creations from the original template.
But I agree with @JohnMachin, I too suspect that your bug really is in the known-good-and-working method,
stepTwoError
. Try evaluating your input fields in a different order. Does stepTwoError have a default argument that is a mutable object or list? Perhaps this is assumed to be empty, but retains values from a previous call? Or does it update a global variable and leave it in a dirty state that leads to second-time-call errors?这个“逻辑”是有缺陷的:“”“(注意:我不包括stepTwoError()函数,因为我知道它可以工作,因为它在被addressError调用后可以工作 - 因为该函数不会改变,我知道它 。
您所知道的是,当将“blank_address”作为第三个参数传递时,它会起作用。假设它包含
This "logic" is flawed: """(Note: I'm not including the stepTwoError() function because I know that it works, since it works after being called by addressError - since that function doesn't change, i know it works""
All you know is that it works when passed "blank_address" as the 3rd arg. Suppose it contains