I have another newbie Python question. I have the following piece of code that I have a feeling is not written as pythonic as it should be:
rowindex = 0
while params.getfirst('myfield'+rowindex):
myid = params.getfirst('myfield'+rowindex)
# do stuff with myid
rowindex+=1
The input to this script is an HTML page that can have any number of input fields named “myfield#” where # starts at 0 and increases sequentially. In Perl, I would do something more like this:
rowindex = 0
while myid = params.getfirst('myfield'+rowindex):
#do stuff with myid
rowindex+=1
But that is not valid syntax in Python. I know what I have will work, but is there a better way? Thank you.
The “unbounded” nature of a simple counter has some appeal. However, it’s also an opportunity for someone to attempt a Denial of Service attack by spoofing a form with billions of fields. Simply counting through the fields could stall your web server as you attempt to process those billions of fields.
Since Python converts to long automatically, the usual 2 billion integer overflow doesn’t apply. Someone could really punish your site with 10’s of billions of fields.