示例#1
0
文件: graph.py 项目: marcinz/nx3k
 def __init__(self, data=None, **attr):
     # the init could be
     # graph = Graph(g) where g is a Graph
     # graph = Graph((v,e)) where v is Graph.v like and e is Graph.e like
     # graph = Graph(({},e)) for an edgelist with no specified nodes
     # others with classmethods like
     # Graph.from_adjacency_matrix(m)
     # Graph.from_adjacency_list(l)
     #
     # should abstract the data here
     self._nodedata = {}  # empty node attribute dict
     self._adjacency = {}  # empty adjacency dict
     # the interface is n,e,a,data
     self.n = Nodes(self._nodedata, self._adjacency) # rename to self.nodes
     self.e = Edges(self._nodedata, self._adjacency) # rename to self.edges
     self.a = Adjacency(self._adjacency) # rename to self.adjacency
     self.data = {}   # dictionary for graph attributes
     # load with data
     if hasattr(data,'n') and not hasattr(data,'name'): # it is a new graph
         self.n.update(data.n)
         self.e.update(data.e)
         self.data.update(data.data)
         data = None
     try:
         nodes,edges = data # containers of edges and nodes
         self.n.update(nodes)
         self.e.update(edges)
     except: # old style
         if data is not None:
             convert.to_networkx_graph(data, create_using=self)
     # load __init__ graph attribute keywords
     self.data.update(attr)
示例#2
0
文件: graph.py 项目: hagberg/nx3k
 def __init__(self, data=None, **attr):
     # the init could be
     # graph = Graph(g) where g is a Graph
     # graph = Graph((v,e)) where v is Graph.v like and e is Graph.e like
     # graph = Graph(({},e)) for an edgelist with no specified nodes
     # others with classmethods like
     # Graph.from_adjacency_matrix(m)
     # Graph.from_adjacency_list(l)
     #
     # should abstract the data here
     self._nodedata = {}  # empty node attribute dict
     self._adjacency = {}  # empty adjacency dict
     # the interface is n,e,a,data
     self.n = Nodes(self._nodedata, self._adjacency)  # rename to self.nodes
     self.e = Edges(self._nodedata, self._adjacency)  # rename to self.edges
     self.a = Adjacency(self._adjacency)  # rename to self.adjacency
     self.data = {}  # dictionary for graph attributes
     # load with data
     if hasattr(data, "n") and not hasattr(data, "name"):  # it is a new graph
         self.n.update(data.n)
         self.e.update(data.e)
         self.data.update(data.data)
         data = None
     try:
         nodes, edges = data  # containers of edges and nodes
         self.n.update(nodes)
         self.e.update(edges)
     except:  # old style
         if data is not None:
             convert.to_networkx_graph(data, create_using=self)
     # load __init__ graph attribute keywords
     self.data.update(attr)
示例#3
0
 def __init__(self, data=None, **attr):
     self._nodes = nd = {}  # fixme factory
     self._mapping = nd
     self._succ = succ = {}  # fixme factory
     self._pred = pred = {}  # fixme factory
     self._directed = attr.pop("directed", False)
     self._multigraph = attr.pop("multigraph", False)
     if self._multigraph:
         myEdges = MultiEdges
         myAdjacency = MultiAdjacency
         myAtlasUnion = MultiAtlasUnion
     else:
         myEdges = Edges
         myAdjacency = Adjacency
         myAtlasUnion = AtlasUnion
     # Interface
     self.n = Nodes(self)
     self.e = myEdges(self)
     self.a = myAdjacency(myAtlasUnion(self._succ, self._pred))
     if self._directed:
         self.su = myAdjacency(self._succ)
         self.pr = myAdjacency(self._pred)
     else:
         self.su = self.a
         self.pr = self.a
     # graph data attributes
     self.data = attr
     # Handle input
     if data is None:
         return
     if hasattr(data, "_nodes"):
         # new datadicts but with same info (containers in datadicts same)
         self.data.update(data.data)
         for n in data.n:
             self.n.update(data.n.items())
         self.e.update(data.e.items())
     elif len(data) == 2:
         try:
             V,E = data
             self.n.update(V)
             self.e.update(E)
         except:  # something else
             d=self.data.copy()
             convert.to_networkx_graph(data, create_using=self)
             self.data.update(d)
     else:
         msg = ("Graph argument must be a graph "
                "or (V, E)-tuple of nodes and edges.")
         raise NetworkXError(msg)
示例#4
0
 def __init__(self, data=None, **attr):
     self._nodes = nd = {}  # fixme factory
     self._mapping = nd
     self._succ = succ = {}  # fixme factory
     self._pred = pred = {}  # fixme factory
     self._directed = attr.pop("directed", False)
     self._multigraph = attr.pop("multigraph", False)
     if self._multigraph:
         myEdges = MultiEdges
         myAdjacency = MultiAdjacency
         myAtlasUnion = MultiAtlasUnion
     else:
         myEdges = Edges
         myAdjacency = Adjacency
         myAtlasUnion = AtlasUnion
     # Interface
     self.n = Nodes(self)
     self.e = myEdges(self)
     self.a = myAdjacency(myAtlasUnion(self._succ, self._pred))
     if self._directed:
         self.su = myAdjacency(self._succ)
         self.pr = myAdjacency(self._pred)
     else:
         self.su = self.a
         self.pr = self.a
     # graph data attributes
     self.data = attr
     # Handle input
     if data is None:
         return
     if hasattr(data, "_nodes"):
         # new datadicts but with same info (containers in datadicts same)
         self.data.update(data.data)
         for n in data.n:
             self.n.update(data.n.items())
         self.e.update(data.e.items())
     elif len(data) == 2:
         try:
             V, E = data
             self.n.update(V)
             self.e.update(E)
         except:  # something else
             d = self.data.copy()
             convert.to_networkx_graph(data, create_using=self)
             self.data.update(d)
     else:
         msg = "Graph argument must be a graph " "or (V, E)-tuple of nodes and edges."
         raise NetworkXError(msg)