Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with nested maps
#11
(Oct-31-2023, 03:57 PM)Unkovic Wrote: The only thing I now need a help with is (nested) lists.

How could we parse this?

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
        ---
        int -> code1 = 16755251
    ---
    int -> code2 = 16755251
    list -> list_name:
        string -> shit
    ---
"""
As you can see I added list to the data

list_pattern = re.compile(r'^\s*list\s*->\s*(\w+)\s*[:]\s*$')

I even have made the regex. But since lists only contains values, I'm unsure how to implement so it doesn't possibly affect with keys regex

key_value_pattern = re.compile(r"^\s*(\w+)\s*->\s*(\w+)\s*=(.*)$")
value_pattern_list = re.compile(r"^\s*(\w+)\s*->(.*)$")

Okay so the next question would be how could I parse homogenic map/list?

list(float) -> some_name :
			- 3.3
			- 2.3
			- 3.33
		---
Like

here is the regex modified for array_id_pattern

^\s*(\w+\(?\w+\)?)\s*->\s*(\w+)\s*([:=])(.*)$'

I wanted to check if map has optional data type after map in parentheses. but if i don't put the parentheses a 'p' letter of map would be in the matching group for it.

Also as you can see, homogenic list / maps items doesn't have data types before them. We automatically know they are float.

this is the function i use for processing the elements

def __process_element(self, element):
        elem_type, number, value, line = element
        if elem_type == 'DICT_ITEM':
            tp, key, rest = value
            self.current_dict[key] = self.__proccess_value(tp, rest)
        elif elem_type == 'MAP':
            new_dict = {}
            if isinstance(self.current_dict, list):
                self.current_dict.append(new_dict)
            else:
                self.current_dict[value] = new_dict
            self.stack.append(self.current_dict)
            self.current_dict = new_dict
        elif elem_type == 'LIST':
            new_list = []
            if isinstance(self.current_dict, list):
                self.current_dict.append(new_list)
            else:
                self.current_dict[value] = new_list
            self.stack.append(self.current_dict)
            self.current_dict = new_list
        elif elem_type == 'LIST_ITEM':
            tp, rest = value
            self.current_dict.append(self.__proccess_value(tp, rest))
        elif elem_type == 'END':
            if self.stack:
                self.current_dict = self.stack.pop()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Best way to map trajectory data on Google Maps Gduffley 1 2,679 Feb-05-2020, 12:36 AM
Last Post: scidam
  Can't visualize maps using Gmaps mPlummers 0 3,559 Sep-11-2019, 02:38 PM
Last Post: mPlummers
  Search "Places near by me" or "where am I" in google maps barry76 1 2,692 Feb-07-2019, 04:10 PM
Last Post: snippsat
  Non-Geographic Heat Maps JackValadez 0 2,099 Oct-17-2018, 06:03 PM
Last Post: JackValadez
  How get attributes of maps from loop statement LB_994 3 3,176 Aug-21-2018, 03:24 PM
Last Post: LB_994
  How to retrieve locality from google maps API Prince_Bhatia 0 3,348 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,167 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