2015-06-23 17 views
5

Gram z wykresami i koduję moduł mixin do tworzenia wykresów. Chcę mieć w nim kilka alternatywnych konstruktorów. To jest to, co mam:Alternatywne konstruktory w Pythonie

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) 

Chcę, aby powrócić wykres gdzie węzły i krawędzie są generowane z pliku. Metoda, którą napisałem, nie działa, nie wiem, czy to ma sens. Wewnątrz tej metody chcę użyć metody Graph, ale następnie dodać krawędzie i węzły z pliku. Mogę po prostu napisać metodę na poziomie instancji, aby to zrobić, ale mam na myśli inne alternatywne inicjatory.

Dzięki!

+1

Twój 'klasa GraphGeneration' dziedziczy tylko z Object, więc jego CLS .__' startowych __() 'nie ma żadnej z tych rzeczy milimetrowym jesteś definiującej. Każdy powód, dla którego nie można po prostu wykonać tej funkcji? –

+0

Mam również inne konstruktory, które chcę, jak tworzenie wykresu m węzłów, a następnie łączenie ich na podstawie prawdopodobieństw. Jak powiedziałeś, mogłem wdrożyć wszystkie te metody instancji, po prostu pomyślałem, że fajnie byłoby mieć je w konstruktorze i mogłem nauczyć się w tym samym czasie, jak wykonywać alternatywne konstruktory w Pythonie. – FranGoitia

Odpowiedz

5

Wewnątrz swoich alternatywnych konstruktorów użyj cls, aby utworzyć nową instancję klasy. Następnie po prostu użyj self, tak jak zwykle, i zwróć go na końcu.

UWAGA: cls to odniesienie do samej klasy, a nie do instancji takiej, jakiej oczekujesz. Zastąpienie wszystkich wystąpień cls za pomocą self, z wyjątkiem instancji, powinno dać oczekiwany wynik. Np

@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