Re: [Tutor] Reference a variable from a string whose value is the name of the variable

2006-06-06 Thread Patrick Wheeler
>data = [ ]>for i in xrange(1,101):> data = "" %i  _n, f %i_v))

The function locals() will return a dictionary of variables in the
current scope.  This can then be used to reference variables by
name.
x=1
xname = 'x'
print locals()['x']
print locals()[xname]
This prints:
1
1

or for your example.
data = [ ]
for i in xrange(1,101):
    data = "">

Hope this helps.

pjw

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Reference a variable from a string whose value is the name of the variable

2006-06-06 Thread Kent Johnson
Peter Jessop wrote:
> Kent
> 
> Thanks for your reply.
> The structure is for sending form variables and values to a web server
> with POST.
> I am using urllib.urlencode which accepts a list or dictionary as argument.
> 
> The idea is to look for airline tickets.
> The airline I buy from only lets me search by data but I want to
> automate the process by POSTing the form for a range of dates so that
> effectively I can search by price not by date.
> I will then parse the returned HTML using HTMLParser or BeautifulSoup.
> 
> The form has a large number of variables and I just want to find a
> tidy way to organise the information.

It sounds like you should use a dictionary directly as your primary data 
structure. For data that is fixed just use a literal dictionary, for example
args = dict(origin='SJO', destination='MHO')

For dates, you presumably have some kind of loop to generate dates in a 
range, then insert them into the dict:
for date in :
   args['date'] = date
   # fetch and parse the URL with args as the POST data

Kent


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Reference a variable from a string whose value is the name of the variable

2006-06-06 Thread Peter Jessop
Kent

Thanks for your reply.
The structure is for sending form variables and values to a web server
with POST.
I am using urllib.urlencode which accepts a list or dictionary as argument.

The idea is to look for airline tickets.
The airline I buy from only lets me search by data but I want to
automate the process by POSTing the form for a range of dates so that
effectively I can search by price not by date.
I will then parse the returned HTML using HTMLParser or BeautifulSoup.

The form has a large number of variables and I just want to find a
tidy way to organise the information.

Peter
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Reference a variable from a string whose value is the name of the variable

2006-06-05 Thread Kent Johnson
Peter Jessop wrote:
> Thanks Andre
> 
> I realise now I did not make it too clear.
> Basically the variable names are predictable but the values aren't.
> 
> Maybe I should have expressed it like this
> 
> f0_n = "arbitrayValue"
> f0_v ="another valule"
> f1_n="something else"
> f1_v="etc.."
> ...
> 
> f100_n = "another value"
> f100_v = "nexvalue"

Generally the best way to do something like this is not to use named
variables at all, rather to use some kind of data structure to store the
values. In this case, you seem to have 101 pairs of values. Perhaps your
primary data structure should be the list of (fx_n, fx_v) values, rather
than trying to access the named variables.

Alternately, it looks like your data pairs are actually (name, value)
pairs. If so, and if the order of pairs is not important, you could
store them in a dictionary using the name as the key and the value as,
well, the value ;)

> I then wish to pass the list of 2ples as an argument to a function and
> thus I need the variable themselves and not the string values.

Hmm, I don't know what you mean by "the variable themselves". Do you 
mean you want the called function to change the value of the variable? 
That is hard to do in Python. But it's easy for a function to modify a 
list or dictionary passed to it.

A little more context might be helpful here. Where do all these values 
come from? What are you trying to do with them?

Kent




___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Reference a variable from a string whose value is the name of the variable

2006-06-05 Thread Peter Jessop
André

Thanks a lot for clearing this up for me.


Regards

Peter Jessop
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Reference a variable from a string whose value is the name of the variable

2006-06-05 Thread Andre Roberge
On 6/5/06, Peter Jessop <[EMAIL PROTECTED]> wrote:
Thanks AndreI realise now I did not make it too clear.Basically the variable names are predictable but the values aren't.I assumed that.  I just recreated the list as you gave it because it was easy :-)
Maybe I should have expressed it like thisf0_n = "arbitrayValue"
f0_v ="another valule"f1_n="something else"f1_v="etc.."...f100_n = "another value"f100_v = "nexvalue"I then wish to pass the list of 2ples as an argument to a function and
thus I need the variable themselves and not the string values.Ok, the approach I gave you would produce instead:data = [ ("arbitraryValue", "another valule"), ("something else",  ]
Why couldn't you pass this to your function? Ok, here's another experiment...1. I will first generate a whole bunch of arbitrary numerical values:import randomfor i in range(101):    print 'f%d_n = %d'%(i, 
random.randint(0, 100))    print 'f%d_v = %d'%(i, random.randint(0, 100))===f0_n = 40f0_v = 28f1_n = 49f1_v = 5...2. Next, I will use the approach I gave you before, but with these values, and I will do it with a one-liner:
data = '' + ','.join(["(f%d_n, f%d_v)"%(i, i) for i in range(101)]) + ']'big_list = eval(data)3. Next, I will define a function that works on these valuesdef find_special(data):    for item in data:
    if item[0] < 10:    print item[1], find_special(big_list)=The result in my case turns out to be:49 50 57 96 98 23 69 16 4Of course this result on its own is meaningless :-)
Nonetheless, does this help?  Or do you really, absolutely need to pass the named variables, not their values?André
___Tutor maillist  -  Tutor@python.orghttp://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Reference a variable from a string whose value is the name of the variable

2006-06-05 Thread Peter Jessop
Thanks Andre

I realise now I did not make it too clear.
Basically the variable names are predictable but the values aren't.

Maybe I should have expressed it like this

f0_n = "arbitrayValue"
f0_v ="another valule"
f1_n="something else"
f1_v="etc.."
...

f100_n = "another value"
f100_v = "nexvalue"

I then wish to pass the list of 2ples as an argument to a function and
thus I need the variable themselves and not the string values.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Reference a variable from a string whose value is the name of the variable

2006-06-05 Thread Andre Roberge
I'm not sure if it's exactly what you need, but here's something that may come close.On 6/5/06, Peter Jessop <[EMAIL PROTECTED]
> wrote:The best way to explain my problem is with an examplef0_n = "field0"
f0_v ="value0"f1_n="field1"f1_v="value1"...f100_n = "field100"f100_v = "value100"Ok, I'm going to recreate this fake example, rather than typing it all out :-)
for i in range(101):    print 'f%d_n = "field%d"'%(i, i)    print 'f%d_v = "value%d"'%(i, i)I then cut-and-paste the result in the editor window and start again.data1 = ["(f%d_n, f%d_v)"%(i, i) for i in range(101)]
print data1# This shows: ['(f0_n, f0_v)', '(f1_n, f1_v)', '(f2_n, f2_v)' ... '(f100_n, f100_v)']data2 = ','.join(data1)data2 = '[' + data2 + ']'print eval(data2)The result is: [('field0', 'value0'), ('field1', 'value1'), ..., ('field100', 'value100')]
i.e. it is a list of 2ples that contains the values of the variable, rather than the variables themselves.  For most applications, this should be the same thing, right?André 
I now want to define a list of 2ples of the form[(f0_n,f0_v),(f1_n,f1_v),...,(f100_n,f100_v)]I wish to define the list using a for loop, i.e.data = [ ]for i in xrange(1,101):  data = ""
((f %i  _n, f %i_v))I have put the % sign above. Obviously it is not like that but howdoes one do it?The aim is to reference the s variable whose name is the string that Icreate concatenating "f" + str(i)+"_n" and "f"+str(i)+"_v"
ThanksPeter Jessop___Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Reference a variable from a string whose value is the name of the variable

2006-06-05 Thread Peter Jessop
The best way to explain my problem is with an example

f0_n = "field0"
f0_v ="value0"
f1_n="field1"
f1_v="value1"
...

f100_n = "field100"
f100_v = "value100"

I now want to define a list of 2ples of the form

[(f0_n,f0_v),(f1_n,f1_v),...,(f100_n,f100_v)]

I wish to define the list using a for loop, i.e.

data = [ ]
for i in xrange(1,101):
  data = data.append((f %i  _n, f %i_v))

I have put the % sign above. Obviously it is not like that but how
does one do it?
The aim is to reference the s variable whose name is the string that I
create concatenating "f" + str(i)+"_n" and "f"+str(i)+"_v"

Thanks

Peter Jessop
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor