Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with nested maps
#4
Here is an alternate way you could parse this data
import re

data = """
    map -> map_name:
        string -> name = John
        int -> age = 30
        string -> city = New York
        int -> code = 16755251
        map -> map_name1:
            string -> name1 = John
            int -> age1 = 30
            string -> city1 = New York
            int -> code1 = 16755251
            float -> floater1 = 3.33
        ---
    ---
"""

from collections import namedtuple
Element = namedtuple('Element', 'type number data line')
map_pattern = re.compile(r'^\s*map\s*->\s*(\w+)\s*[:]\s*$')
key_value_pattern = re.compile(r"^\s*(\w+)\s*->\s*(\w+)\s*=(.*)$")
end_pattern = re.compile('^\s*---\s*$')
empty_pattern = re.compile('^\s*$')

def flat_parse(data):
    for i, line in enumerate(data.splitlines(), 1):
        if match := key_value_pattern.match(line):
            yield Element('KEY', i, (match.group(1), match.group(2), match.group(3)), line)
        elif match := map_pattern.match(line):
            yield Element('MAP', i, match.group(1), line)
        elif match := end_pattern.match(line):
            yield Element('END', i, None, line)
        elif match := empty_pattern.match(line):
            yield Element('EMPTY', i, None, line)
        else:
            yield Element('ERROR', i, None, line)

def convert(tp, value):
    value = value.strip()
    match tp:
        case 'int':
            return int(value)
        case 'float':
            return float(value)
        case 'string':
            return str(value)

def parse(data):
    current = {}
    stack = []
    for elt in flat_parse(data):
        match elt.type:
            case 'KEY':
                tp, name, value = elt.data
                current[name] = convert(tp, value)
            case 'MAP':
                stack.append(current)
                current[elt.data] = current = {}
            case 'END':
                if not stack:
                    raise RuntimeError('Too many ends of map', elt)
                current = stack.pop()
            case 'EMPTY':
                pass
            case 'ERROR':
                raise RuntimeError('Parsing error', elt)
    if stack:
        raise RuntimeError('End of map missing at end of data')
    return current

print(parse(data))
Output:
λ python paillasse/pf/parsemap.py {'map_name': {'name': 'John', 'age': 30, 'city': 'New York', 'code': 16755251, 'map_name1': {'name1': 'John', 'age1': 30, 'city1': 'New York', 'code1': 16755251, 'floater1': 3.33}}}
Reply


Messages In This Thread
Help with nested maps - by Unkovic - Oct-30-2023, 10:29 PM
RE: Help with nested maps - by Unkovic - Oct-30-2023, 10:30 PM
RE: Help with nested maps - by Gribouillis - Oct-31-2023, 06:53 AM
RE: Help with nested maps - by Unkovic - Oct-31-2023, 10:02 AM
RE: Help with nested maps - by Gribouillis - Oct-31-2023, 08:01 AM
RE: Help with nested maps - by Gribouillis - Oct-31-2023, 10:14 AM
RE: Help with nested maps - by Unkovic - Oct-31-2023, 03:28 PM
RE: Help with nested maps - by Unkovic - Oct-31-2023, 03:57 PM
RE: Help with nested maps - by Unkovic - Nov-01-2023, 01:07 AM
RE: Help with nested maps - by Unkovic - Nov-01-2023, 03:45 PM
RE: Help with nested maps - by Gribouillis - Oct-31-2023, 05:50 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Best way to map trajectory data on Google Maps Gduffley 1 2,735 Feb-05-2020, 12:36 AM
Last Post: scidam
  Can't visualize maps using Gmaps mPlummers 0 3,609 Sep-11-2019, 02:38 PM
Last Post: mPlummers
  Search "Places near by me" or "where am I" in google maps barry76 1 2,748 Feb-07-2019, 04:10 PM
Last Post: snippsat
  Non-Geographic Heat Maps JackValadez 0 2,144 Oct-17-2018, 06:03 PM
Last Post: JackValadez
  How get attributes of maps from loop statement LB_994 3 3,234 Aug-21-2018, 03:24 PM
Last Post: LB_994
  How to retrieve locality from google maps API Prince_Bhatia 0 3,384 Jul-23-2018, 07:57 AM
Last Post: Prince_Bhatia
  python charmap codec can't decode byte X in position Y character maps to < undefined> owais 9 39,430 Apr-28-2018, 10:52 PM
Last Post: abadawi

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020