from csv import reader as csv_reader from pprint import pp as print, pformat from sys import argv, stderr class System: """Represents an L-System""" def __init__( self, name: str, base: set[str], axiom: str, substitutions: list[tuple[str, str]], interpretations: list[tuple[str, str]], ): self.name = name """System name""" self.base = base """Set used""" self.axiom = axiom """Axiom used at the start of iterations""" self.substitutions = substitutions """ Substitution for each member of the base, represented as a couple (member, substitution) """ self.interpretations = interpretations """ Interpretation for each member of the base, represented as a couple (member, interpretation) """ # TODO: Est-ce que on garde ça ?? # Interpretation of extra symbols added if necessary extra_symbols = ["[", "]"] for _, substitution in substitutions: if any(symbol in substitution for symbol in extra_symbols): self.interpretations.extend( [(extra_symbols[0], "STORE"), (extra_symbols[1], "RESTORE")] ) break def __repr__(self): return pformat(self.__dict__, compact=True, width=120, sort_dicts=False) def data_reader(path: str, delimiter: str = ","): """Read a CSV file and returns a list of L-System""" res: list[System] = [] with open(path) as csv_file: data = csv_reader(csv_file, delimiter=delimiter) for system in data: name = system[0] base = set(system[1]) axiom = system[2] substitutions = [(v, system[3 + i]) for i, v in enumerate(base)] interpretations = [ (v, system[3 + i + len(substitutions)]) for i, v in enumerate(base) ] res.append(System(name, base, axiom, substitutions, interpretations)) return res if __name__ == "__main__": if len(argv) != 2: print(f"Syntax error:\n\tpython {argv[0]} file.csv", stream=stderr) exit(1) # TODO: Pour les arguments, utilisé `argparse` print(data_reader(argv[1]))