Checking the existence of parsed variables

Gavin Lusby gavinlusby at googlemail.com
Wed Jan 23 12:21:08 EST 2008


Hello all,
I am regularly writing programs that parses ever changing lists of
variables.
The method I do it is by loading it in to a dictionary of the form:
d['variable_name']=variable_value

So when it comes to a method that uses a required set of variables I have to
make tens of the following statements
if d.has_key('swr'): 
        swr = float(d['swr'])
    else: 
        print 'ERROR: COREYTAB variable swr not specified'; iErr += 1

When I thought a pythonic way of doing it would be to use a variable
variable.(sorry that sounds ridiculous) but it would work like this:

_start_code_________________________________________________________________
___
    # make all input parameters equal the string of themselves
    type = 'type'; tabnum = 'tabnum';  coreyw = 'coreyw'; coreyow =
'coreyow'
    swc = 'swc'; swr = 'swr'; kro_swc = 'kro_swc'; krw_swr = 'krw_swr'
    coreyg = 'coreyg'; coreygo = 'coreygo'; sgi = 'sgi'; sgc = 'sgc'
    sgr = 'sgr'; kro_sgc = 'kro_sgc'; krg_swc = 'krg_swc'
    # error check parameters existence and if there, over-write their info
    # if there are integer values, then must remember to re-typecast them
later
    for var in (type, tabnum, coreyw, coreyow, swc, swr, kro_swc, krw_swr,
coreyg, coreygo, sgi, sgc, sgr, kro_sgc, krg_swc):
        if d.has_key(var):
            if isnumeric(d[var]):
                var = float(d[var])
            else:
                var = d[var]
        else:
            print 'ERROR: getcorey() parameter '+str(var)+' not specified'
            iErr += 1
_end_code___________________________________________________________________
___

Since this doesn't work, any other ideas out there?

Many thanks in advance, Gavin

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


More information about the Python-list mailing list