On 1/21/11 5:33 PM, Ed Connell wrote:
Hi,

Consider the following please:  (re_section, re_name, etc are previously
compiled patterns)

                        result1 = re_section.search(line);
                        result2 = re_name.search(line);
                        result3 = re_data1.search(line);
                        result4 = re_data2.search(line);

                        if result1:
                                last_section = result1.group()[18:-5]
                        elif result2:
                                last_name = result2.group(0)[6:-1]
                        elif result3:
                                data[last_section] = {last_name:
result3.group()[13:-5]}
                        elif result4:
                                data[last_section] = {last_name:
result4.group()[17:-5]}

It gets my goat to have to obtain all resultx when I just want the first that is
not None.  (In theory, the number of results can be much longer.)  I can think
of alternatives (raising exceptions), but they all use deep indenting.

parsers = [
  ('section', re_section, lambda r: r.group()[18:-5]),
  ('name', re_name, lambda r: r.group()[6:-1]),
  ('data1', re_data1, lambda r: r.group()[13:-5]),
  ('data2', re_data2, lambda r: r.group()[17:-5]),
]

data = {}

for line in lines:
    values = {}
    for key, regex, extract in parsers:
        m = regex.search(line)
        if m is not None:
            values[key] = extract(m)
            break
    if 'data1' in values:
        data[values['section']] = {values['name']: values['data1']}
    elif 'data2' in values:
        data[values['section']] = {values['name']: values['data2']}

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

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

Reply via email to