I'm playing with graphs and coded a mixin module for creating graphs. I want to have in it some alternative constructors. This is what I have:
class Graph(GraphDegree, GraphDegreePlot, GraphGeneration, object): def __init__(self): self.nodes = set([]) self.edges = {} def get_nodes(self): """ get nodes in graph """ return self.nodes def get_number_of_nodes(self): """ get number of nodes in the graph """ return len(self.nodes) def get_edges(self): """ get edges in graph """ return self.edges def get_heads_in_edges(self): """ get heads in edges present on the graph """ return self.edges.values() def add_node(self, node): """ add new node to graph """ if node in self.get_nodes(): raise ValueError('Duplicate Node') else: self.nodes.add(node) self.edges[node] = [] def add_connection(self, edge): """ adds edge to graph """ origin = edge.get_origin() destination = edge.get_destination() if origin not in self.get_nodes() or destination not in self.get_nodes(): raise ValueError('Nodes need to be in the graph') self.get_edges()[origin].append(destination) self.get_edges()[destination].append(origin) def get_children(self, node): """ Returns the list of nodes node node is connected to """ return self.get_edges()[node] class GraphGeneration(object): @classmethod def gen_graph_from_text(cls, file): ''' Generate a graph from a txt. Each line of the txt begins with the source node and then the destination nodes follow ''' cls.__init__() file = open(file, 'r') for line in file: origin = line[0] destinations = line[1:-1] cls.add_node(origin) for destination in destinations: cls.add_node(destination) edge = Edge(origin, destination) cls.add_connection(edge) graph = Graph.gen_graph_from_text(file) I want that to return a graph where nodes and edges are generated from the file. The method I wrote doesn't work, I don't know if it even makes sense. What I want to do is inside that method to use the __init__ method of Graph, but then add edges and nodes from the file. I could just write an instance level method to do this, but I have other altertive initializers in mind.
Thanks !
GraphGenerationclass is only inheriting from Object, so itscls.__init__()won't have any of the graph stuff you're defining. Any reason you couldn't just make that a function?