2

I'm using Jinja2 to generate a form with a variable number of inputs, labelled input_1, input_2, etc. Using Google App Engine (python), I'm then trying to access the values of these inputs in my request handler using self.request.args.get().

However, depending on the number of inputs generated by the form, the script needs to read multiple variables. The script knows how many there will be, so the question is just how to use some kind of variable variable in a for loop to read them all efficiently.

The kind of thing I'm after is conceptually like this:

for x in total_inputs:
  list.append(input_x)

I could of course just use if statements for different numbers of inputs and do the variable names manually, but that seems awfully clunky - surely there is a better way?

Many thanks

Nick Johnson
  • 100,655
  • 16
  • 128
  • 198
Cerzi
  • 768
  • 5
  • 19
  • Can you clarify your question? I see somebody downvoted @Hyperboreus answer, but I too would have understood your question the way she/he did... ? – mac Jul 07 '11 at 05:22
  • Also, from the title you say "unknown variable names", but what does `self.request.args.get()` returns? Can you post a [SSCCE](http://sscce.org/) example of the code that troubles you? – mac Jul 07 '11 at 05:24
  • @mac 20kb? I think your link is off by orders of magnitude for Stack Overflow. And what he's asking seems perfectly clear to me, contextually. – Nick Johnson Jul 07 '11 at 05:39
  • @Nick - SO is not a usenet forum though! :) As for clarity, I'm happy you understood, but the entire point about posting on SO is to reach out a wide audience, so it would be in the interest of the OP if his/her question would be clear for everybody (or at least clear to as many people as possible). – mac Jul 07 '11 at 05:44
  • @mac It should be clear to anyone using a webapp-like framework on App Engine (or off it, really) - he's trying to get sequentially numbered fields from a form post. I'm not sure how he could elaborate to make that clearer. – Nick Johnson Jul 07 '11 at 06:15
  • @Nick - Like you did it? :o Or with an example? The question is tagged `python` so it shows up also to people who do not have expertise in `google-app-engine`... Anyhow, I don't think the OP made a mortal sin. I just think the clearer a question is, the best for all users. Cheers! :) – mac Jul 07 '11 at 06:24
  • Sorry, I somehow missed the confusion over the title - it is rather misleading. – Nick Johnson Jul 08 '11 at 00:14

2 Answers2

4

You can do this with string formatting, like so:

num = 5
tag = 'input_%d' % num

So to assemble a list of the values of all the input_x tags, you could do something like this:

input_values = []
for i in range(1, number_of_values + 1):
  input_values.append(self.request.get('input_%d' % i))

This assumes you know how many values to retrieve. Your question presupposes you do, but in the case that you didn't, you can iterate over the results until you get an empty string (the default return value from request.get for a nonexistent label).

Edit: The above comment assumed you're using webapp. Other frameworks may throw a KeyError if you try and call get for an argument that doesn't exist, and with no default specified.

Nick Johnson
  • 100,655
  • 16
  • 128
  • 198
  • OP stated that the params are labelled input_1, input_2, etc. You iterate over range (number_of_values). Wouldn't this lead to a off-by-one-error? – Hyperboreus Jul 07 '11 at 05:59
  • @Hyperboreus Yup, fixed. It's a bit rude to vote the whole answer down over a fencepost error, though. – Nick Johnson Jul 07 '11 at 06:15
  • Thanks, I had actually made the problem much more complicated in my head. Your solution is of course correct - for some reason I had decided in my head that self.request.form.get() somehow created a new variable within python, rather than simply being accessed by a string parameter. Stupid, because I've used the function many, many times before! – Cerzi Jul 09 '11 at 09:02
1
for x in total_inputs: list.append (locals () ['input_%d' % x] )

or if you want a dictionary with variable name as key and its value as value:

vars = {}    
for x in total_inputs: vars ['input_%d' % x] = locals () ['input_%d' % x]

Here some working example in python2.6:

>>> input_0 = 'a'
>>> input_1 = 4
>>> input_2 = None
>>> for x in range (3): print locals () ['input_%d' % x]
...
a
4
None
>>>
Hyperboreus
  • 31,997
  • 9
  • 47
  • 87
  • Sorry, I just realised the name of the question is "accessing unknown variable names in Python"; now I understand the source of the confusion. I've edited the title to something more suitable. – Nick Johnson Jul 08 '11 at 00:14