[Tutor] Looking up a value in a dictionary from user input problem
Dave Angel
davea at ieee.org
Thu Aug 6 23:56:21 CEST 2009
chase pettet wrote:
> I am trying to write a script to work our LVS implementation. I want to be
> able to have user do something like this "./script SITE SERVER" and have
> the script look up the ip value of the site on that server and issue the
> command to pull the status from LVS. I am almost there but for some reason
> when I try to pass the site parameter dynamically (the name of the
> dictionary) I keep getting errors about value type. I have two example that
> work where the dictionary name is hard coded to to speak in the script, and
> the value I want to pull is dynamic using sys.argv[1], and one where I'm
> trying to pass the dictionary name and it does not work. I tried to slim
> thse examples down, so hopefully they are helpful:
>
> ----<snip first two versions>-----
>
>
> Not working...
>
> ./script.py t site
>
> #!/usr/bin/env python
> site = {"l":"10.1.1.1", "t":"10.1.1.2", "x":"10.1.1.3", "s1":"10.1.1.4",
> "s2":"10.1.1.5", "s3":"10.1.1.6"}
>
> def runBash(cmd):
> p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
> out = p.stdout.read().strip()
> return out
>
> class LVS_Site:
> def show(self, site):
> showsite = "ipvsadm -L -t %s:443" % (site)
> showsiteresult = runBash(showsite)
> return showsiteresult
>
> a = LVS_Site()
> z = sys.argv[2]
> b = b["%s" % (sys.argv[1])]
> c = a.show(b)
> print ""
> print ""
> print ""
> print c
>
> Error:
>
> Traceback (most recent call last):
> File "./python2.py", line 22, in ?
> b = z["%s" % (sys.argv[1])]
> TypeError: string indices must be integers
>
>
> I don't understand why the third one does not work. Thanks for any help!
>
>
You didn't post the exact code, as the line that gives the error is
quoted differently than it is. I suspect that the error message shows
the version you actually tested.
a = LVS_Site()
z = sys.argv[2]
at this point, z is a string, containing the characters
"site"
b = z["%s" % (sys.argv[1])]
at this point, sys.argv[1] is a string, containing the
character "t"
So "s" % "t" is a string, containing the character "t"
So you're trying to evaluate
"site"["t"]
And thus the error. Strings can be indexed only by integers, not by
characters.
It appears you're confusing the variable site with the string "site".
The former is a dictionary, the latter is a string.
Now, you could lookup that string in the current global namespace, and
get the dictionary with that name. Something like (untested):
b = globals().get(z, None) [ "%s" % (sys.argv[1])]
But this is pretty dangerous territory. Perhaps if you tell us what
you're actually trying to achieve, we might be able to help. For
example, you might have a limited list of dictionaries, and want the
user to be able to choose among them by keyword.
site_HQ = {"l":"10.1.1.1", "t":"10.1.1.2", "x":"10.1.1.3", "s1":"10.1.1.4",
"s2":"10.1.1.5", "s3":"10.1.1.6"}
site_Alternate = {"l":"10.1.1.1", "t":"10.1.1.2", "x":"10.1.1.3", "s1":"10.1.1.4",
"s2":"10.1.1.5", "s3":"10.1.1.6"}
site_desperate = {"l":"10.1.1.1", "t":"10.1.1.2", "x":"10.1.1.3",
"s1":"10.1.1.4",
"s2":"10.1.1.5", "s3":"10.1.1.6"}
sitelist = {"HQ":site_HQ, "Alternate", site_Alternate,
"desperate":site_desperate}
b = sitelist[sys.argv[2]] ["%s" % (sys.argv[1])]
DaveA
More information about the Tutor
mailing list