def test_from_str(): s = "[(11^N); (2^L); (3^C)]" res = Segment.from_str(s) exp = Segment( [TaggedValue(11, "N"), TaggedValue(2, "L"), TaggedValue(3, "C")]) assert res == exp
def init_from_file(filename, parser=int): """Instantiate a distributed tree from a file Parameters ---------- filename : str The name of the file that contains the PTree to instantiate parser : callable, optional A function that transforms a string into a specific type. By default, string to int """ filename = filename + "." + str(PID) def __parser_couple(s): s = s.replace("(", "") s = s.replace(")", "") ss = s.split(",") return int(ss[0]), int(ss[1]) p = PTree() content = SList([]) with open(filename, "r") as f: count_line = 0 for line in f: if line.strip()[0] == '#': continue # __distribution: PID -> nb of segments # __global_index: num seg -> (start, offset) if count_line == 0: # Get the distribution p.distribution = SList.from_str(line) p.start_index = p.distribution.scanl( lambda x, y: x + y, 0)[PID] p.nb_segs = p.distribution[PID] elif count_line == 1: # Get the global_index p.global_index = SList.from_str(line, parser=__parser_couple) else: # Get the content content.extend(Segment.from_str(line, parser=parser)) count_line = count_line + 1 p.content = content return p