Getting a value from a nested dictionary

Cunningham, Dan DanQ.cunningham at landesk.com
Mon Apr 7 10:11:23 EDT 2008


Hi, My name is Dan and I'm a newb to python (and programming. Please
forgive)

 

I am trying to get a value from a nested dictionary. I would like to
pass in a parameter from a conf file, then compare it to a value in the
dictionary, and verify that it is a valid value. (The SSL_MODE Portion
of the below listed dictionary)

 

I have d1{key1: val1,

            key2: val_2{key_a: val_a, key_b: val_b},

            key3: val_3}

 

And In comes the value from the conf file key2: val_a

 

Any Ideas?

 

Dan C

 

#!/usr/local/bin/python2.5

# -*- coding: UTF-8 -*-

# File extractconf.py

# The file authn.conf must exist

 

import os

import sys

import re

 

CONF_FILE = "authn.conf"

 

#Use these defaults if none are provided in the .conf file

PARAMS_VALUES = {"INSTANCE_NAME": "",

                 "CHASE_REFERRALS": False,

                 "DOMAIN_NAME": "",

                 "GROUP_CONTAINER": "",

                 "PASSWORD": "",

                 "SSL_MODE": {"ssl1": "NO_SSL", "ssl2": "SSL_TRUST_ALL",
"ssl3": "SSL_CERTIFICATE_MODE"},

                 "USE_GC": False,

                 "USE_KERBEROS": False,

                 "USER_CONTAINER": "",

                 "USER_NAME": "",

                 "USER_NAME_TYPE": {'unt1': "FULL_SAM", 'unt2':
"PARTIAL_SAM", 'unt3': "FULL_UPN", 'unt4': "PARTIAL_UPN"}}

 

def extractParams():

        thedir = os.getcwd() #Parse the dir and get the file listing

        filelist = os.listdir(thedir)

        if CONF_FILE in filelist:

                thefile = open("authn.conf").readlines()

                thefile = [l.strip() for l in thefile] #Strip out the
whitespace

                thefile = [l for l in thefile if l.find('=') >= 0] #
Filters out non-conf lines

                thefile = [re.split(r"\s*=\s*", l) for l in thefile]
#Use regex and get out spaces

                thefile = [(l[0], eval(l[1])) for l in thefile]
#Evaluate the str and bool values

                thefile = dict(thefile) #Turn the file into a dictionary

                for k in thefile.keys(): #For each entry in the file

                        if k == 'SSL_MODE': #If the key is equal to the
SSL_MODE key

 

##THIS IS WHERE I'M STUCK

 

                                print PARAMS_VALUES['SSL_MODE']

                                

##                        else:

 

## THIS PART IS OK

                for k in PARAMS_VALUES.keys(): #Replace values in
default dict with the values from the conf file

                        PARAMS_VALUES[k] = thefile[k]

        else:

                sys.exit("File authn.conf must exist")

                                        

if __name__ == "__main__":

    extractParams()

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080407/3e727e73/attachment.html>


More information about the Python-list mailing list