def flatten_sequences()

in lucid/misc/graph_analysis/parse_overlay.py [0:0]


def flatten_sequences(structure):
  """Flatten nested sequences into a single sequence."""
  if isinstance(structure, str) or (isinstance(structure, dict) and structure["type"] == "Node") or structure is None:
    return structure
  else:
    structure = structure.copy()
    if "children" in structure:
      structure["children"] = [flatten_sequences(sub) for sub in structure["children"]]

  if structure["type"] == "Sequence":
    new_seq = []
    for sub in structure["children"]:
      if isinstance(sub, dict) and sub["type"] == "Sequence":
        new_seq += sub["children"]
      else:
        new_seq.append(sub)
    structure["children"] = new_seq
  return structure