def load(graph: Graph, file_name: str): graph.clear() # получаем данные из файла # vertexes, v_coordinates = LoadGraph.__split_file(file_name) with open(file_name, 'r') as file: data = json.load(file) try: graph.vertexes = data['vertexes'] except KeyError: return False # загружем данные о координатах или генерируем, если их нет if 'coordinates' in data: coordinates = data['coordinates'] for d in coordinates: graph.vertexes_coordinates[d['name']] = Vertex( d['name'], d['x'], d['y']) else: for v in graph.vertexes: graph.vertexes_coordinates[v] = Vertex( v, random.randint(0, 100), random.randint(0, 100)) if 'oriented' in data: graph.oriented = data['oriented'] if 'weighted' in data: graph.weighted = data['weighted'] if 'path' in data: graph.path = data['path'] graph.update() return True
def load_from_ribs_list(graph: Graph, file_name: str): graph.clear() with open(file_name, mode='r') as file: for line in file: name = '' params = [] for index in line: if index == '{': value = '' if index.isdigit(): value += index if index == '(': name = value value = '' if index == ',': params.append(value) value = '' if index == ')': params.append(value) print(params, name) value = '' graph.add_vertex(params[1], random.randint(0, 100), random.randint(0, 100)) graph.add_vertex(params[2], random.randint(0, 100), random.randint(0, 100)) graph.add_edge(params[2], params[1], int(params[0])) graph.oriented = bool(int(params[3])) params = []
def load_from_arc_list(graph: Graph, file_name: str): graph.clear() with open(file_name, mode='r') as file: for line in file: name = '' x = '' for index in line: if index == '{': value = '' if index.isdigit() or index == '.' or index == '-': value += index if index == ',': x = value value = '' if index == '(': name = value value = '' if index == ')': graph.add_vertex(name, float(x), float(value)) value = ''