def read_section(stream, classtype): """read one section of Matlab file, assuming the header is done. and assuming that each line is one row ended by a semicolon. In addition to the matlab spec a compoent can have an ID specified by adding %XX to the end of its line where XX is the id. """ items = [] for _, line in enumerate(stream): line = line.strip() # check if we are done if re.match("\A *\] *; *\Z", line): # if line == "];" break # skip comments if len(line) == 0 or line.startswith("%"): continue # everything should be lowercase, we don't care about the ';' cols = [x.lower() for x in line.replace(";", " ").split()] # strip comment delimiter from component ID # e.g. change '%a1' to 'a1' if it exists if cols[-1].startswith("%"): cols[-1] = cols[-1][1:] items.append(read_struct(classtype, cols)) return items
def read(self, stream): for line in stream: cols = [x.lower() for x in line.split()] if len(cols) == 0 or cols[0].startswith("#"): continue elif cols[0] == "bus": self.busses.append(read_struct(self.Bus, cols[1:])) self.busses[-1].setup() elif cols[0] == "line": self.lines.append(read_struct(self.Line, cols[1:])) self.lines[-1].setup() elif cols[0] == "generator": self.generators.append(read_struct(self.Generator, cols[1:])) self.generators[-1].setup() elif cols[0] == "crow": self.crows.append(read_struct(self.Crow, cols[1:])) else: raise Error("expected (bus, line, generator, crow) got " + cols[0])