6

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 !

2
  • 1
    Your GraphGeneration class is only inheriting from Object, so its cls.__init__() won't have any of the graph stuff you're defining. Any reason you couldn't just make that a function? Commented Jun 23, 2015 at 18:57
  • I also have other constructors I want to make like creating a graph of m nodes and then connecting them based on probabilities. As you say, I could implement all these in instance methods, I just thought it would be cool to have it in constructors and I could learn at the same time how to do alternative constructors in Python. Commented Jun 23, 2015 at 19:10

1 Answer 1

8

Inside of your alternate constructors, use cls to create the new instance of the class. Then, just use self like you normally would and return it at the end.

NOTE: cls is a reference to the class itself, not the instance like you're expecting. Replacing all occurrences of cls with self except for the instantiation should give you the result you want. E.g.,

@classmethod def gen_graph_from_text(cls, file): self = cls() file = open(file, 'r') for line in file: origin = line[0] destinations = line[1:-1] self.add_node(origin) for destination in destinations: self.add_node(destination) edge = Edge(origin, destination) self.add_connection(edge) return self 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.