def bounds(self, struct_name): i = self.structs.index(Where(lambda x: x.name == struct_name)) return self.struct_bounds[i]
def update(self, structure): idx = self.structs.index(Where(lambda x: x.name == structure.name)) left, right = self.struct_bounds[idx] self.text = self.text[:left] + str(structure) + self.text[right:]
def get(self, struct_name): return Where(lambda x: x.name == struct_name).find(self.structs)
def split_var(self, field_name, new_var_str, arr_index=0): """ :param field_name: str :param new_var_str: str :param arr_index: int """ try: i = self.fields.index(Where(lambda x: x.name == field_name)) except ValueError: raise_error("field %s not found" % field_name) of = self.fields[i] nf = StructField(new_var_str) if nf.name in self.names(): raise_error("Var %s is already in %s" % (nf.name, self.name)) if nf.size() % of.divisible_size(): raise_error("New size = %s should be divisible of %s" % (nf.size(), of.divisible_size())) if nf.size() > of.size(): raise_error( "New field size(%s) is bigger than old field size(%d)" % (nf.size(), of.size())) end_index = arr_index + nf.size() / of.divisible_size() if end_index > of.divisible_count(): raise_error("Array index is out of bounds") self.fields.pop(i) if arr_index: pre_f = copy.deepcopy(of) shape = pre_f.array_shape() shape[0] = arr_index pre_f.set_array_shape(shape) self.fields.insert(i, pre_f) i += 1 self.fields.insert(i, nf) i += 1 rest_count = of.divisible_count() - end_index if rest_count: post_f = copy.deepcopy(of) shape = post_f.array_shape() shape[0] = rest_count post_f.set_array_shape(shape) m = re.match("(.*)?_(\d+)", post_f.name) if m: end_index += eval(m.group(2)) post_f.name = m.group(1) + "_%s" % end_index else: post_f.name += "_%s" % end_index self.fields.insert(i, post_f) i += 1 pass