Using dictionaries

sorular sorular at netscape.net
Wed Dec 6 00:27:01 EST 2000


>> and in the "main.py" these will be all renamed with the local names
>> ie
>> Val1 = dictionary.dict['key1']
>> Val2 = dictionary.dict['key2']
>> ....
>> ValN = dictionary.dict['keyN']
>> etc....
>
>First, I assume you mean configuration.dict, unless for some
>peculiar reason you've chosen to "import configuration as dictionary"
>(but why would you do that?).

Some silly mistake... as you said it should have been like
	Val1 = configuration.dict['key1']
	etc...

......

>As you can see, Python offers a lot of similar but not
>identical approaches for the task you've set yourself.
>It's not part of the party line, but, in Python, too,
>there's more than one way to do it.  How to choose?  A
>golden rule of programming is...:
>
>    *Do the simplest thing that could possibly work*
.......
>
>    from configuration import *

This is what I decided to do.. it does what I want to do pretty well
without any complication...  nor any dictionaries.!

One slight problem :

Another dictionary look up where necessary values are received from a
single-item dictionary (later on I will open this up to a larger
number of items..!) (I saw a similar one in Mr Guido's site I
think!).....

The dictionary would be {'UserXXXX':'123123'}

----Configuration.py (start)----
...
UserName=UserXXXX
UserNumb="123123"
...
----Configuration.py (ends)----

----Main.py (start)----
...
from configuration import *
class Authenticator:
    def __init__(self):
        try:
	 somethingelse()
        except:

self.authenticate=Dict_lookup(UserName=function(UserNumb))

    def lookup_key (self,*args):
        return apply(self.authenticate.lookup_key,args)

class Dict_lookup:
    def __init__(self,**kw):
        self.dict=kw

    def lookup_key (self,sn):
        return self.dict[sn]
...
----Main.py (ends)----

The whole point is to call up the dictionary lookup for the values in
single item dictionary from configuration.py (which is user entered
parameters) when somethingelse() is not available...

However, python gives me error when I run it like this as :
           Exceptions.KeyError : UserName

but still if I change the above line with :
    def __init__(self):
        self.authenticate=Dict_lookup(UserXXXX=function(UserNumb))

then it works fine...!!!

I should be able to the call UserName (which I tried with single,
double and reverse quotes 
	ie
	'UserName'  or  "UserName" or `UserName`

or is there something I am not seing here??

thanks

HK



More information about the Python-list mailing list