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

2008-03-22 Thread sgharvey
... 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.

What am I not understanding about re.search?

Doesn't match properly:

  # Iterate through each pattern for each line
  for line in lines:
 for pattern in patterns:
# Match each pattern to the current line
match = patterns[pattern].search(line)
if match:
   "%s: %s" % (pattern, str(match.groups()) )


_Does_ match properly:

  # Let's iterate through all the lines for each pattern
  for pattern in pattern:
 for line in lines:
# Match each pattern to the current line
match = patterns[pattern].search(line)
if match:
   "%s: %s" % (pattern, str(match.groups()) )


Related code:
The whole src
http://pastebin.com/f63298772
regexen and delimiters (imported into whole src)
http://pastebin.com/f485ac180

-- 
http://mail.python.org/mailman/listinfo/python-list


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

2008-03-23 Thread sgharvey
On Mar 22, 5:03 pm, "Gabriel Genellina" <[EMAIL PROTECTED]>
wrote:
> En Sat, 22 Mar 2008 17:27:49 -0300, sgharvey <[EMAIL PROTECTED]>
> escribi�:
> Take a look at ConfigObjhttp://pypi.python.org/pypi/ConfigObj/

Thanks for the pointer; I'll check it out.

> I'm not sure you can process a config file in this unstructured way; looks
> a lot easier if you look for [sections] and process sequentially lines
> inside sections.

It works though... now that I've fixed up all my ugly stuff, and a
dumb logic error or two.

> The regular expressions look strange too. A comment may be empty. A
> setting too. There may be spaces around the = sign. Don't try to catch all
> in one go.

I didn't think about empty comments/settings... fixed now.
It also seemed simpler to handle surrounding spaces after the match
was found.

New version of the problematic part:

  self.contents = []
  content = {}
  # Get the content in each line
  for line in lines:
 for name in patterns:
# Match each pattern to the current line
match = patterns[name].search(line)
if match:
   content[name] = match.group(0).strip()
 self.contents.append(content)
 content = {}


new iniparsing.py
http://pastebin.com/f445701d4

new ini_regexen_dicts.py
http://pastebin.com/f1e41cd3d

> --
> Gabriel Genellina


Much thanks to all for the constructive criticism.

Samuel Harvey
-- 
http://mail.python.org/mailman/listinfo/python-list