bruno at modulix wrote: [...] > Suppose you have to match a line against a list of regexp and log if it > doesn't match. You could of course repeat the whole code for each > regexp, ie: > > if not re.match(r'a/regexp/here', line): > log('a first message') > > if not re.match(r'another/regexp/here', line): > log('another message') > > (... 150 regexps later ...) > > if not re.match(r'150/regexps/later', line): > log('pfww, getting tired of copy/pasting') > > etc... > > But you could also factor much of it: > > def checkMatch(line, regexp, msg): > if not re.match(regexp, line): > log(msg) > > then have a list of regexps/messages pairs and: > for exp, msg in regexps: > checkMatch(line, exp, msg) > > And now, you can add as many thousands regexps you want, you still have > one (and only one) if in the code (well, in this snippet at least...).
If your checks are this complicated, I think you should consider writing a parser for your configuration file. If you use a parser generator it's not that difficult. Moreover a lexical analyzer could be enough if your syntax is simple. I found Dave Beazley's PLY reasonably easy to use: http://www.dabeaz.com/ply/ Cheers, Nicola Musatti -- http://mail.python.org/mailman/listinfo/python-list