[Tutor] Looking up a value in a dictionary from user input problem

Νικόλαος Ράπτης airscorp at otenet.gr
Thu Aug 6 23:43:34 CEST 2009


Hmmm.. First of all..
In the non working code:
....
a = LVS_Site()
z = sys.argv[2]
b = b["%s" % (sys.argv[1])]
c = a.show(b)
.....

Did you really mean
b = b["%s" % (sys.argv[1])]

or perhaps
b = z["%s" % (sys.argv[1])]


Also, I can't see why your doing the string formatting in there:
b = z[sys.argv[1]]       should work. Perhaps that's because you need it 
in the 'real' program?

Nick


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:
>
> works #1...
>
> ./script.py t
>
> #!/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()
> b = site["%s" % (sys.argv[1])]
> c = a.show(b)
> print ""
> print ""
> print ""
> print c
>
> works #2...
>
> ./script.py t
>
> #!/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 = site
> b = z["%s" % (sys.argv[1])]
> c = a.show(b)
> print ""
> print ""
> print ""
> print c
>
>
> 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!
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>   


More information about the Tutor mailing list