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

sgharvey KephnosAnagennao at gmail.com
Sun Mar 23 03:32:28 EDT 2008


On Mar 22, 5:03 pm, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
wrote:
> En Sat, 22 Mar 2008 17:27:49 -0300, sgharvey <KephnosAnagen... at gmail.com>
> 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:
<code>
      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 = {}
</code>

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



More information about the Python-list mailing list