re.search (works)|(doesn't work) depending on for loop order

Brian Lane bcl at brianlane.com
Sat Mar 22 19:10:19 EDT 2008


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

sgharvey wrote:
> ... and by works, I mean works like I expect it to.
> 
> I'm writing my own cheesy config.ini parser because ConfigParser
> doesn't preserve case or order of sections, or order of options w/in
> sections.
> 
> What's confusing me is this:
>    If I try matching every line to one pattern at a time, all the
> patterns that are supposed to match, actually match.
>    If I try to match every pattern to one line at a time, only one
> pattern will match.

I don't see that behavior when I try your code. I had to fix your
pattern loading:

patterns[pattern] = re.compile(pattern_strings[pattern], re.VERBOSE)

I would also recommend against using both the plural and singular
variable names, its bound to cause confusion eventually.

I also changed contents to self.contents so that it would be accessible
outside the class.

The correct way to do it is run each pattern against each line. This
will maintain the order of the config.ini file. If you do it the other
way you will end up with everything ordered based on the patterns
instead of the file.

I tried it with Python2.5 on OSX from within TextMate and it ran as
expected.

Brian

- --
- ---[Office 70.9F]--[Outside 54.5F]--[Server 103.3F]--[Coaster 68.0F]---
- ---[      KLAHOWYA WSF (366773110) @ 47 31.2076 -122 27.2249      ]---
Software, Linux, Microcontrollers             http://www.brianlane.com
AIS Parser SDK                                http://www.aisparser.com
Movie Landmarks Search Engine            http://www.movielandmarks.com

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (Darwin)
Comment: Remember Lexington Green!

iD8DBQFH5ZHaIftj/pcSws0RAigtAJsE+NWTxwV5kO797P6AXhNTEp8dmQCfXL9I
y0nD/oOfNw6ZR6UZIOvwkkE=
=U+Zo
-----END PGP SIGNATURE-----



More information about the Python-list mailing list