[Tutor] search function

orbitz orbitz at ezabel.com
Fri Jul 30 09:08:57 CEST 2004


Why not just use a python file as your config? That is what I do, then I 
just import it.

You can also use wat Steve suggested, but IMO just using python is more 
powerful since you can do things like tuples and what not trivially


Conrad wrote:

>I'm writing a script that extracts values from config files in the form
>of:
>
>"variable"="value"
>"variable1"="value"
>
>I wrote this function, which takes a comma delimited list of variables
>to search for. (variable1,variable2,....), and then searches through
>each line of the config file, checking if the variable is there, and
>then printing out the variable found and the value:
>
>def display_var(variables):
>        var_disp = re.compile(r'%s' % (variables.replace(',','|')))
>        line = config_file.readline()
>        var_value = re.compile(r'"[^"]*?"$')
>        var_name = re.compile(r'".*?"')
>        while line != '':
>                if var_disp.search(line):
>                        value = var_value.search(line)
>                        var = var_name.search(line)
>                        print "%s: %s" % (var.group()[1:-1],
>value.group()[1:-1])
>                else:
>                        pass
>                line = config_file.readline()
>
>There are a few things that are bugging me about this. One is the heavy
>use of regular expressions, and two is that im using [1.-1] to strip the
>qoutes. Can anyone point out how to make this more pythonic?
>
>Your time is appreciated,
>	Conrad
>
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor
>
>  
>



More information about the Tutor mailing list