mardi 31 juillet 2018

A better way than looping and calling functions that loop and call another functions

I have a message (string) that is composed of transactions that is composed of groups that is composed of elements.

I there a better way to parse such message than looping and calling functions that loop and call another functions that loop and call another functions because I find the following is silly:

class Parser:
  def parse_msg(self, msg):
    trans = re.findall(trans_pattern, msg)
    for t in trans:
      self.parse_trans(t)

  def parse_trans(self, trans):
    groups = re.findall(groups_pattern, trans)
    for g in groups:
      self.parse_group(g)

  def parse_group(self, group):
    elements = re.findall(element_pattern, group)
    for e in elements:
      self.parse_element(e)

  def parse_element(self, e):
    pass

Is there a better way/design-pattern that I can approach this with?

Aucun commentaire:

Enregistrer un commentaire