Nov-01-2023, 03:45 PM
(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()