Cannot Access Dictionary

sblakey at freei.net sblakey at freei.net
Fri Mar 24 19:26:12 EST 2000


On 25 Mar, apjewell at bigfoot.com wrote:
> Well, I tried everything I could think of and couldn't figure this one
> out.  I'm a new user so maybe it's something easy - any help would be
> greatly appreciated.
> 
> I have a class that reads in a configuration file.  The configuration
> file is separated into sections, each section becomes an element in the
> AppInfo array.
> 
> Each element in a section is entered as a key:value dictionary entry.
> So all of my juicy data is contained in the AppInfo array (E.G.,
> self.AppInfo[0]["KeyName"] = "Value").  I have an access function called
> GetEnvList which looks like this:
> 
>    def GetEnvList(self, *ReqEnvName):
>       """
>       Name: GetEnvList
>       Desc: An access method to return a list[] describing all the
>             environments listed in the configuration file.  These
> <SNIP>
> """
>       # If ReqEnvName exists
>       if ReqEnvName:
>          print "Requested Name is: %s--" % (ReqEnvName)
>          EnvData = self.AppInfo[self.secEnv].get(ReqEnvName,
>             "That environment was not found in this configuration
> file.")
> 
>       else:
>          EnvData = self.AppInfo[self.secEnv]
> 
>       return EnvData
> 
> My class is called AppInCM.  There's the setup - NOW, if I import this
> class interactively I CAN successfully access a specific environment by
> this:
> 
> myClass.AppInfo[self.secEnv].get("KeyName", "Couldn't get it")
> or, for simplicity, myClass.AppInfo[1]["KeyName"].
> 
> But whenever I use my handydandy GetEnvList("KeyName") function, the
> entry can't be found, e.g.:
>>>> myC.GetEnvList("Development")
> Requested Name is: Development
> 'That environment was not found in this configuration file.'
> 
> Does anyone have any idea what this might be?  The function works fine
> without the parameter, i.e., myC.GetEnvList() will return the entire
> array of dictionary entries.  I can access my entries WITHOUT the
> function so I know they're load OK.  What gives??  Thanks for your time!
> 
> Andy Jewell
> 
> 
> Sent via Deja.com http://www.deja.com/
> Before you buy.

Keep in mind that the *argv form collects remaining arguments in a
tuple, even if there is ony one remaining argument.  Inside the
function, the dictionary is failing to lookup the key ('Development', ).
For example:
>>>def f(*argv):
...    print 'argv is %s' % argv
...    print argv
>>>f('spam')
argv is spam
('spam',)

Is this really what you want?

As an aside, your description of your problem sounds like you are trying
to parse something like windows .ini files.  You are aware of the
ConfigParser module, correct?






More information about the Python-list mailing list