K i42dZddlmZddlmZmZddlZgdZGddeeZ Gdd eZ Gd d Z Gd d e Z Gdde Z Gdde ZGdde ZGdde ZGdde ZGdde ZGddeZGddeZGddeZGd d!eZGd"d#eZGd$d%eZGd&d'eZGd(d)eeeZGd*d+eZGd,d-eZGd.d/eZGd0d1eZGd2d3eZy)4a View Classes provide node, edge and degree "views" of a graph. Views for nodes, edges and degree are provided for all base graph classes. A view means a read-only object that is quick to create, automatically updated when the graph changes, and provides basic access like `n in V`, `for n in V`, `V[n]` and sometimes set operations. The views are read-only iterable containers that are updated as the graph is updated. As with dicts, the graph should not be updated while iterating through the view. Views can be iterated multiple times. Edge and Node views also allow data attribute lookup. The resulting attribute dict is writable as `G.edges[3, 4]['color']='red'` Degree views allow lookup of degree values for single nodes. Weighted degree is supported with the `weight` argument. NodeView ======== `V = G.nodes` (or `V = G.nodes()`) allows `len(V)`, `n in V`, set operations e.g. "G.nodes & H.nodes", and `dd = G.nodes[n]`, where `dd` is the node data dict. Iteration is over the nodes by default. NodeDataView ============ To iterate over (node, data) pairs, use arguments to `G.nodes()` to create a DataView e.g. `DV = G.nodes(data='color', default='red')`. The DataView iterates as `for n, color in DV` and allows `(n, 'red') in DV`. Using `DV = G.nodes(data=True)`, the DataViews use the full datadict in writeable form also allowing contain testing as `(n, {'color': 'red'}) in VD`. DataViews allow set operations when data attributes are hashable. DegreeView ========== `V = G.degree` allows iteration over (node, degree) pairs as well as lookup: `deg=V[n]`. There are many flavors of DegreeView for In/Out/Directed/Multi. For Directed Graphs, `G.degree` counts both in and out going edges. `G.out_degree` and `G.in_degree` count only specific directions. Weighted degree using edge data attributes is provide via `V = G.degree(weight='attr_name')` where any string with the attribute name can be used. `weight=None` is the default. No set operations are implemented for degrees, use NodeView. The argument `nbunch` restricts iteration to nodes in nbunch. The DegreeView can still lookup any node even if nbunch is specified. EdgeView ======== `V = G.edges` or `V = G.edges()` allows iteration over edges as well as `e in V`, set operations and edge data lookup `dd = G.edges[2, 3]`. Iteration is over 2-tuples `(u, v)` for Graph/DiGraph. For multigraphs edges 3-tuples `(u, v, key)` are the default but 2-tuples can be obtained via `V = G.edges(keys=False)`. Set operations for directed graphs treat the edges as a set of 2-tuples. For undirected graphs, 2-tuples are not a unique representation of edges. So long as the set being compared to contains unique representations of its edges, the set operations will act as expected. If the other set contains both `(0, 1)` and `(1, 0)` however, the result of set operations may contain both representations of the same edge. EdgeDataView ============ Edge data can be reported using an EdgeDataView typically created by calling an EdgeView: `DV = G.edges(data='weight', default=1)`. The EdgeDataView allows iteration over edge tuples, membership checking but no set operations. Iteration depends on `data` and `default` and for multigraph `keys` If `data is False` (the default) then iterate over 2-tuples `(u, v)`. If `data is True` iterate over 3-tuples `(u, v, datadict)`. Otherwise iterate over `(u, v, datadict.get(data, default))`. For Multigraphs, if `keys is True`, replace `u, v` with `u, v, key` to create 3-tuples and 4-tuples. The argument `nbunch` restricts edges to those incident to nodes in nbunch. )ABC)MappingSetN)NodeView NodeDataViewEdgeView OutEdgeView InEdgeView EdgeDataViewOutEdgeDataViewInEdgeDataView MultiEdgeViewOutMultiEdgeViewInMultiEdgeViewMultiEdgeDataViewOutMultiEdgeDataViewInMultiEdgeDataView DegreeView DiDegreeView InDegreeView OutDegreeViewMultiDegreeViewDiMultiDegreeViewInMultiDegreeViewOutMultiDegreeViewcjeZdZdZdZdZdZdZdZdZ dZ d Z e d Z dd Zdd ZdZdZy )raA NodeView class to act as G.nodes for a NetworkX Graph Set operations act on the nodes without considering data. Iteration is over nodes. Node data can be looked up like a dict. Use NodeDataView to iterate over node data or to specify a data attribute for lookup. NodeDataView is created by calling the NodeView. Parameters ---------- graph : NetworkX graph-like class Examples -------- >>> G = nx.path_graph(3) >>> NV = G.nodes() >>> 2 in NV True >>> for n in NV: ... print(n) 0 1 2 >>> assert NV & {1, 2, 3} == {1, 2} >>> G.add_node(2, color="blue") >>> NV[2] {'color': 'blue'} >>> G.add_node(8, color="red") >>> NDV = G.nodes(data=True) >>> (2, NV[2]) in NDV True >>> for n, dd in NDV: ... print((n, dd.get("color", "aqua"))) (0, 'aqua') (1, 'aqua') (2, 'blue') (8, 'red') >>> NDV[2] == NV[2] True >>> NVdata = G.nodes(data="color", default="aqua") >>> (2, NVdata[2]) in NVdata True >>> for n, dd in NVdata: ... print((n, dd)) (0, 'aqua') (1, 'aqua') (2, 'blue') (8, 'red') >>> NVdata[2] == NV[2] # NVdata gets 'color', NV gets datadict False _nodescd|jiSNrrselfs b/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/networkx/classes/reportviews.py __getstate__zNodeView.__getstate__s$++&&c|d|_yr rr"states r# __setstate__zNodeView.__setstate__s Ho r%c&|j|_yN)_noder)r"graphs r#__init__zNodeView.__init__s kk r%c,t|jSr+lenrr!s r#__len__zNodeView.__len__4;;r%c,t|jSr+)iterrr!s r#__iter__zNodeView.__iter__sDKK  r%c t|trRtjt |j d|j d|jd|jd|j|S)Nz- does not support slicing, try list(G.nodes)[:]) isinstanceslicenx NetworkXErrortype__name__startstopsteprr"ns r# __getitem__zNodeView.__getitem__si a "":&&'(%%&WWIQqvvhaxqB {{1~r%c||jvSr+rrCs r# __contains__zNodeView.__contains__sDKKr%ct|Sr+setclsits r#_from_iterablezNodeView._from_iterable 2wr%Nc<|dur|St|j||SNFrrr"datadefaults r#__call__zNodeView.__call__s! 5=KDKKw77r%c<|dur|St|j||S)a Return a read-only view of node data. Parameters ---------- data : bool or node data key, default=True If ``data=True`` (the default), return a `NodeDataView` object that maps each node to *all* of its attributes. `data` may also be an arbitrary key, in which case the `NodeDataView` maps each node to the value for the keyed attribute. In this case, if a node does not have the `data` attribute, the `default` value is used. default : object, default=None The value used when a node does not have a specific attribute. Returns ------- NodeDataView The layout of the returned NodeDataView depends on the value of the `data` parameter. Notes ----- If ``data=False``, returns a `NodeView` object without data. See Also -------- NodeDataView Examples -------- >>> G = nx.Graph() >>> G.add_nodes_from( ... [ ... (0, {"color": "red", "weight": 10}), ... (1, {"color": "blue"}), ... (2, {"color": "yellow", "weight": 2}), ... ] ... ) Accessing node data with ``data=True`` (the default) returns a NodeDataView mapping each node to all of its attributes: >>> G.nodes.data() NodeDataView({0: {'color': 'red', 'weight': 10}, 1: {'color': 'blue'}, 2: {'color': 'yellow', 'weight': 2}}) If `data` represents a key in the node attribute dict, a NodeDataView mapping the nodes to the value for that specific key is returned: >>> G.nodes.data("color") NodeDataView({0: 'red', 1: 'blue', 2: 'yellow'}, data='color') If a specific key is not found in an attribute dict, the value specified by `default` is returned: >>> G.nodes.data("weight", default=-999) NodeDataView({0: 10, 1: -999, 2: 2}, data='weight') Note that there is no check that the `data` key is in any of the node attribute dictionaries: >>> G.nodes.data("height") NodeDataView({0: None, 1: None, 2: None}, data='height') FrRrSs r#rTz NodeView.datas$@ 5=KDKKw77r%c*tt|Sr+strlistr!s r#__str__zNodeView.__str__4:r%cL|jjdt|dSN()) __class__r?tupler!s r#__repr__zNodeView.__repr__s$..))*!E$K=::r%FN)TN)r? __module__ __qualname____doc__ __slots__r$r)r.r2r6rErG classmethodrNrVrTr\rdr%r#rrvs\3jI'&" ! 8 B8H;r%rc\eZdZdZdZdZdZddZedZ dZ d Z d Z d Z d Zd Zy)raA DataView class for nodes of a NetworkX Graph The main use for this class is to iterate through node-data pairs. The data can be the entire data-dictionary for each node, or it can be a specific attribute (with default) for each node. Set operations are enabled with NodeDataView, but don't work in cases where the data is not hashable. Use with caution. Typically, set operations on nodes use NodeView, not NodeDataView. That is, they use `G.nodes` instead of `G.nodes(data='foo')`. Parameters ========== graph : NetworkX graph-like class data : bool or string (default=False) default : object (default=None) r_data_defaultcJ|j|j|jdS)Nrmrmr!s r#r$zNodeDataView.__getstate__3s++  VVr%c@|d|_|d|_|d|_y)Nrrnrormr's r#r)zNodeDataView.__setstate__6s$Ho 7^ j) r%Nc.||_||_||_yr+rm)r"nodedictrTrUs r#r.zNodeDataView.__init__;s   r%c t|S#t$r-}dt|vrd}tt||z|d}~wwxYw)N unhashablez7 : Could be b/c data=True or your values are unhashable)rJ TypeErrorrZ)rLrMerrmsgs r#rNzNodeDataView._from_iterable@sI r7N s3x'OC3/S8   s A(>Ac,t|jSr+r0r!s r#r2zNodeDataView.__len__Jr3r%cjdurtjSdur#tjjSfdjjDS)NFTc3RK|]\}}||vr|n jf ywr+)ro).0rDddrTr"s r# z(NodeDataView.__iter__..Ss2 2DBJ4DMM : $')rnr5ritems)r"rTs`@r#r6zNodeDataView.__iter__Ms`zz 5= $ $ 4< ))+, , **,  r%c ||jv}|dur|S |\}}||jvxr|||k(S#t$r |\}}||jvxr|||k(cYSwxYw#ttf$rYywxYw)NTF)rrv ValueError)r"rDnode_inds r#rGzNodeDataView.__contains__Xs 54;;&G d?N DAqDKK0DGqL0 5DAq #4Q1 4 5:&  s 4A &AA A21A2c :t|trRtjt |j d|j d|jd|jd|j|}|j}|dus|dur|S||vr||S|jS)Nz4 does not support slicing, try list(G.nodes.data())[r8r9FT) r:r;r<r=r>r?r@rArBrrnro)r"rDddictrTs r#rEzNodeDataView.__getitem__fs a "":&&'(,,-GG9AaffXQqvvhaI  Azz 5=DDLL"emuT{>>r%c*tt|Sr+rYr!s r#r\zNodeDataView.__str__rr]r%c|jj}|jdur|dt|dS|jdur|dt |dS|dt |d|jdS)NFr`raTz, data=)rbr?rnrcdict)r"names r#rdzNodeDataView.__repr__usx~~&& :: V1U4[M+ + :: V1T$ZL* *qd GDJJ>;;r%re)r?rfrgrhrir$r)r.rjrNr2r6rGrEr\rdrkr%r#rrsP"0IW*      1 ?eZdZdZd dZd dZdZdZdZdZ d Z y) raUA View class for degree of nodes in a NetworkX Graph The functionality is like dict.items() with (node, degree) pairs. Additional functionality includes read-only lookup of node degree, and calling with optional features nbunch (for only a subset of nodes) and weight (use edge weights to compute degree). Parameters ========== graph : NetworkX graph-like class nbunch : node, container of nodes, or None meaning all nodes (default=None) weight : bool or string (default=None) Notes ----- DegreeView can still lookup any node even if nbunch is specified. Examples -------- >>> G = nx.path_graph(3) >>> DV = G.degree() >>> assert DV[2] == 1 >>> assert sum(deg for n, deg in DV) == 4 >>> DVweight = G.degree(weight="span") >>> G.add_edge(1, 2, span=34) >>> DVweight[2] 34 >>> DVweight[0] # default edge weight is 1 1 >>> sum(span for n, span in DVweight) # sum weighted degrees 70 >>> DVnbunch = G.degree(nbunch=(1, 2)) >>> assert len(list(DVnbunch)) == 2 # iteration over nbunch only Nc||_t|dr |jn |j|_t|dr |jn |j|_| |jnt |j ||_||_y)N_succ_pred) _graphhasattrr_adjrr[ nbunch_iterr_weight)r"Gnbunchweights r#r.zDiDegreeView.__init__sc '7 3QWW '7 3QWW $*NdjjQ]]6=R8S  r%cB|.||jk(r|S|j|jd|S ||jvr4||jk(r||S|j|jd||S |j|j||S#t$rY(wxYwr+)rrbrrrv)r"rrs r#rVzDiDegreeView.__call__s >% >>$++tV< < $T\\)<'~~dkk4@HH% ~~dkk66::   s!BB BBc|j|j|}|j|}t|t|zSt fd|j Dt fd|j DzS)Nc3BK|]}|jdywNgetr|r}rs r#r~z+DiDegreeView.__getitem__..s>266&!$>c3BK|]}|jdywrrrs r#r~z+DiDegreeView.__getitem__..s#E "$BFF61 E r)rrrr1sumvalues)r"rDsuccspredsrs @r#rEzDiDegreeView.__getitem__sy 1  1  >u:E * *>u||~>>E (- E B   r%c#K|jK|jD];}|j|}|j|}|t |t |zf=y|jD]k}|j|}|j|}t fd|j Dt fd|j Dz}||fmyw)Nc3BK|]}|jdywrrrs r#r~z(DiDegreeView.__iter__..E"&&+Erc3BK|]}|jdywrrrs r#r~z(DiDegreeView.__iter__..s#L*,BFF61%Lr)rrrrr1rrr"rDrrdegrs @r#r6zDiDegreeView.__iter__s >[[ 3 1  1 #e*s5z122 3 [[  1  1 EellnEEL05 LI#h  sCCc,t|jSr+r0r!s r#r2zDiDegreeView.__len__r3r%c*tt|Sr+rYr!s r#r\zDiDegreeView.__str__r]r%cL|jjdt|dSr_)rbr?rr!s r#rdzDiDegreeView.__repr__$..))*!DJ>> G = nx.path_graph(3) >>> DV = G.degree() >>> assert DV[2] == 1 >>> assert G.degree[2] == 1 >>> assert sum(deg for n, deg in DV) == 4 >>> DVweight = G.degree(weight="span") >>> G.add_edge(1, 2, span=34) >>> DVweight[2] 34 >>> DVweight[0] # default edge weight is 1 1 >>> sum(span for n, span in DVweight) # sum weighted degrees 70 >>> DVnbunch = G.degree(nbunch=(1, 2)) >>> assert len(list(DVnbunch)) == 2 # iteration over nbunch only c|j|j|}t|||vzStfd|j D||vxr||j dzS)Nc3BK|]}|jdywrrrs r#r~z)DegreeView.__getitem__..=266&!$=rr)rrr1rrrr"rDnbrsrs @r#rEzDegreeView.__getitem__ sjzz!} >t9T * *=t{{}== I 0$q'++fa0  r%c#\K|j5|jD]%}|j|}|t|||vzf'y|jD]U}|j|}t fd|j D||vxr||j dz}||fWyw)Nc3BK|]}|jdywrrrs r#r~z&DegreeView.__iter__..sD"&&+Drr)rrrr1rrrr"rDrrrs @r#r6zDegreeView.__iter__s >[[ 3zz!}#d)qDy122 3[[ zz!}DdkkmDDI8$q'++fa"8#h  sB)B,Nr?rfrgrhrEr6rkr%r#rrs)V  r%rceZdZdZdZdZy)rzEA DegreeView class to report out_degree for a DiGraph; See DegreeViewcj}j|}j t|Stfd|j DS)Nc3VK|] }|jjd"ywr)rr)r|r}r"s r#r~z,OutDegreeView.__getitem__..*s Cr266$,,*Cs&))rrr1rr)r"rDrrs` r#rEzOutDegreeView.__getitem__%sDzz!} << t9 CT[[]CCCr%c#K|j0|jD] }|j|}|t|f"y|jD]9}|j|}t fd|j D}||f;yw)Nc3BK|]}|jdywrrrs r#r~z)OutDegreeView.__iter__..5rr)rrrr1rr)r"rDrrrs @r#r6zOutDegreeView.__iter__, >[[ & 1 #e*o% &[[  1 EellnEE#h BB Nrrkr%r#rr"sOD r%rceZdZdZdZdZy)rzDA DegreeView class to report in_degree for a DiGraph; See DegreeViewc|j|j|} t|Stfd|j DS)Nc3BK|]}|jdywrrrs r#r~z+InDegreeView.__getitem__..Arr)rrr1rrrs @r#rEzInDegreeView.__getitem__<s?zz!} >t9 =t{{}===r%c#K|j0|jD] }|j|}|t|f"y|jD]9}|j|}t fd|j D}||f;yw)Nc3BK|]}|jdywrrrs r#r~z(InDegreeView.__iter__..Lrr)rrrr1rr)r"rDrrrs @r#r6zInDegreeView.__iter__CrrNrrkr%r#rr9sN> r%rceZdZdZdZdZy)rz=A DegreeView class for undirected multigraphs; See DegreeViewcH|j|j|}5td|jD||vxrt ||zStfd|jD}||vr(|tfd||jDz }|S)Nc32K|]}t|ywr+r1r|keyss r#r~z.MultiDegreeView.__getitem__..W;Ts4y;c3lK|]+}|jD]}|jd-ywrrrr|key_dictrrs r#r~z.MultiDegreeView.__getitem__..[s: !)HOODU ?@AEE&!   14c3BK|]}|jdywrrr|rrs r#r~z.MultiDegreeView.__getitem__.._sBAquuVQ'Br)rrrrr1rs @r#rEzMultiDegreeView.__getitem__Sszz!} >;T[[];;T *c$q'l  -1[[]   9 3Ba1ABB BC r%c#K|j\|jD]L}|j|}td|j D||vxrt ||z}||fNy|jD]e}|j|}tfd|j D}||vr(|tfd||j Dz }||fgyw)Nc32K|]}t|ywr+rrs r#r~z+MultiDegreeView.__iter__..g>#d)>rc3lK|]+}|jD]}|jd-ywrrrs r#r~z+MultiDegreeView.__iter__..n@ %__.EE&!$$rc3BK|]}|jdywrrrs r#r~z+MultiDegreeView.__iter__..tsJAquuVQ/Jr)rrrrrr1rs @r#r6zMultiDegreeView.__iter__bs >[[ zz!}> >>I.#d1g,#h  [[ zz!}$(KKM 93Ja9IJJJC#h sC C#Nrrkr%r#rrPsG r%rceZdZdZdZdZy)rz3A DegreeView class for MultiDiGraph; See DegreeViewcl|j|j|}|j|}Atd|j Dtd|j DzStfd|j Dtfd|j Dz}|S)Nc32K|]}t|ywr+rrs r#r~z0DiMultiDegreeView.__getitem__..s.sC"D Crc3lK|]+}|jD]}|jd-ywrrrs r#r~z0DiMultiDegreeView.__getitem__..s: !)X__EV @AAEE&!   rc3lK|]+}|jD]}|jd-ywrrrs r#r~z0DiMultiDegreeView.__getitem__..s: !)X__EV @AAEE&!   r)rrrrrrs @r#rEzDiMultiDegreeView.__getitem__{s 1  1  >.s?#d)?rc32K|]}t|ywr+rrs r#r~z-DiMultiDegreeView.__iter__..sF"&CIFrc3lK|]+}|jD]}|jd-ywrrrs r#r~z-DiMultiDegreeView.__iter__..rrc3lK|]+}|jD]}|jd-ywrrrs r#r~z-DiMultiDegreeView.__iter__..s@ %__.EE&!$$r)rrrrrrrs @r#r6zDiMultiDegreeView.__iter__s >[[  1  1 ? ??#F*/,,.FC#h  [[  1  1 $)LLN$)LLN #h sDDNrrkr%r#rrxs= r%rceZdZdZdZdZy)rzDA DegreeView class for inward degree of MultiDiGraph; See DegreeViewc|j|j|} td|jDStfd|jDS)Nc32K|]}t|ywr+rr|rTs r#r~z0InMultiDegreeView.__getitem__..rrc3lK|]+}|jD]}|jd-ywrrrs r#r~z0InMultiDegreeView.__getitem__..: !)HOODU ?@AEE&!   r)rrrrrs @r#rEzInMultiDegreeView.__getitem__Uzz!} >;T[[];; ; -1[[]   r%c#HK|jG|jD]7}|j|}td|j D}||f9y|jD]9}|j|}tfd|j D}||f;yw)Nc32K|]}t|ywr+rrs r#r~z-InMultiDegreeView.__iter__..rrc3lK|]+}|jD]}|jd-ywrrrs r#r~z-InMultiDegreeView.__iter__..rr)rrrrrrs @r#r6zInMultiDegreeView.__iter__ >[[ zz!}> >>#h  [[ zz!}$(KKM #h BB"Nrrkr%r#rrsN r%rceZdZdZdZdZy)rzEA DegreeView class for outward degree of MultiDiGraph; See DegreeViewc|j|j|} td|jDStfd|jDS)Nc32K|]}t|ywr+rrs r#r~z1OutMultiDegreeView.__getitem__..rrc3lK|]+}|jD]}|jd-ywrrrs r#r~z1OutMultiDegreeView.__getitem__..rr)rrrrrs @r#rEzOutMultiDegreeView.__getitem__rr%c#HK|jG|jD]7}|j|}td|j D}||f9y|jD]9}|j|}tfd|j D}||f;yw)Nc32K|]}t|ywr+rrs r#r~z.OutMultiDegreeView.__iter__..rrc3lK|]+}|jD]}|jd-ywrrrs r#r~z.OutMultiDegreeView.__iter__..rr)rrrrrrs @r#r6zOutMultiDegreeView.__iter__rrNrrkr%r#rrsO r%rc eZdZy) EdgeViewABCN)r?rfrgrkr%r#rrsr%rcLeZdZdZdZdZdZd dddZdZd Z d Z d Z d Z y)r z;EdgeDataView for outward edges of DiGraph; See EdgeDataView)_viewer_nbunchrnro_adjdict _nodes_nbrs_reportc`|j|j|j|jdS)N)viewerrrTrU)rrrnror!s r#r$zOutEdgeDataView.__getstate__s(llllJJ}}   r%c(|jdi|yNrkr.r's r#r)zOutEdgeDataView.__setstate__ r%NrUcN||_|jx|_j|_n9tj |j jfd|_|_|_ |_ dur d|_ ydur d|_ yfd|_ y)Nc8Dcgc] }||f c}Scc}wr+rkrDadjdictrs r#z*OutEdgeDataView.__init__.. 'HAGAJ'H'HTc |||fSr+rkrDnbrr}s r#r z*OutEdgeDataView.__init__..s q#rlr%Fc ||fSr+rkrs r#r z*OutEdgeDataView.__init__..s q#hr%c&|vr|||fS||fSr+rk)rDrr}rTrUs r#r z*OutEdgeDataView.__init__..s+2:%&sBtH#5g&r%) rrrrrfromkeysrrrrnror)r"rrrTrUr s ```@r#r.zOutEdgeDataView.__init__s "(//1$- >&}}D ]]6==#<#.?D3t9?rrr!s r#r2zOutEdgeDataView.__len__?D,<,<,>???r%c6fdjDS)Nc3zK|]2\}}|jD]\}}j|||4ywr+rrr|rDrrr}r"s r#r~z+OutEdgeDataView.__iter__..sI 4::< R LLC $ $ 8;rr!s`r#r6zOutEdgeDataView.__iter__ ++-  r%c|dd\}}|j||jvry |j||}||j|||k(S#t$rYywxYwNFrrKeyErrorrr"euvrs r#rGzOutEdgeDataView.__contains__%o!u1 << #(= MM!$Q'EDLLAu---  A AAc*tt|Sr+rYr!s r#r\zOutEdgeDataView.__str__/r]r%cL|jjdt|dSr_rbr?r[r!s r#rdzOutEdgeDataView.__repr__2rr%rQ) r?rfrgrhrir$r)r.r2r6rGr\rdrkr%r#r r s<EI 40@ .:r%r c&eZdZdZdZdZdZdZy)r aWA EdgeDataView class for edges of Graph This view is primarily used to iterate over the edges reporting edges as node-tuples with edge data optionally reported. The argument `nbunch` allows restriction to edges incident to nodes in that container/singleton. The default (nbunch=None) reports all edges. The arguments `data` and `default` control what edge data is reported. The default `data is False` reports only node-tuples for each edge. If `data is True` the entire edge data dict is returned. Otherwise `data` is assumed to hold the name of the edge attribute to report with default `default` if that edge attribute is not present. Parameters ---------- nbunch : container of nodes, node or None (default None) data : False, True or string (default False) default : default value (default None) Examples -------- >>> G = nx.path_graph(3) >>> G.add_edge(1, 2, foo="bar") >>> list(G.edges(data="foo", default="biz")) [(0, 1, 'biz'), (1, 2, 'bar')] >>> assert (0, 1, "biz") in G.edges(data="foo", default="biz") rkc&td|DS)Nc3 K|]}dywrrkr|r(s r#r~z'EdgeDataView.__len__..V#1# rr!s r#r2zEdgeDataView.__len__U#d###r%c#Ki}|jD]<\}}|jD]\}}||vs |j|||!d||<>~ywNrrrr)r"seenrDrrr}s r#r6zEdgeDataView.__iter__Xsj'') GAt::< 3Rd?,,q#r22 3DG   s 3A Ac|dd\}}|j||jvr||jvry |j||}||j|||k(S#t$rYywxYwr#r%r's r#rGzEdgeDataView.__contains__asz!u1 << #(=!4<.rsI 4::< R LLa $ $ rr r!s`r#r6zInEdgeDataView.__iter__qr!r%c|dd\}}|j||jvry |j||}||j|||k(S#t$rYywxYwr#r%r's r#rGzInEdgeDataView.__contains__xr+r,Nr?rfrgrhrir6rGrkr%r#r r lsNI .r%r cBeZdZdZdZdZdZd ddddZd Zd Z d Z y) rzCAn EdgeDataView for outward edges of MultiDiGraph; See EdgeDataView)rcv|j|j|j|j|jdS)N)rrrrTrU)rrrrnror!s r#r$z!OutMultiEdgeDataView.__getstate__s/llllIIJJ}}   r%c(|jdi|yrrr's r#r)z!OutMultiEdgeDataView.__setstate__rr%NFrUrc||_|jx|_||_j|_n9t j |jjfd|_|_ |_ |_ dur|dur d|_ yd|_ ydur|dur d|_ yd|_ y|dur fd|_ yfd |_ y) Nc8Dcgc] }||f c}Scc}wr+rkr s r#r z/OutMultiEdgeDataView.__init__..r r Tc||||fSr+rkrDrkr}s r#r z/OutMultiEdgeDataView.__init__..saa_r%c |||fSr+rkrIs r#r z/OutMultiEdgeDataView.__init__..s ab\r%Fc |||fSr+rkrIs r#r z/OutMultiEdgeDataView.__init__..s aa[r%c ||fSr+rkrIs r#r z/OutMultiEdgeDataView.__init__..s aXr%c*|vr ||||fS|||fSr+rkrDrrJr}rTrUs r#r z/OutMultiEdgeDataView.__init__..s/rz,-c1bh*?S!W-r%c&|vr|||fS||fSr+rkrOs r#r z/OutMultiEdgeDataView.__init__..s+rz,-c2d8*<S'*r%) rrrrrrrrrrrnror)r"rrrTrUrr s ``` @r#r.zOutMultiEdgeDataView.__init__s "(//1$- >&}}D ]]6==#<#.r4r5r6r!s r#r2zOutMultiEdgeDataView.__len__r7r%c6fdjDS)Nc 3K|]K\}}|jD]3\}}|jD]\}}j||||5Mywr+rr|rDrrkdrJr}r"s r#r~z0OutMultiEdgeDataView.__iter__..sj 4::< R  2 LLCB ' ' ' AAr r!s`r#r6zOutMultiEdgeDataView.__iter__ ++-  r%c\dd\jjvry j}jdur"d} ||}j ||k(St fd|j DS#t$rYywxYw#t$rYywxYw)Nr$FTc3RK|]\}}j||k( ywr+rr|rJr}r(r"r)r*s r#r~z4OutMultiEdgeDataView.__contains__..)Mea1 Q1b11Mrrrr&rranyrr"r(kdictrJr}r)r*s`` @@r#rGz!OutMultiEdgeDataView.__contains__s!u1 << #(= MM!$Q'E 99 !A 1X Q1b11 1Mu{{}MMM     s#BB BB B+*B+rQ) r?rfrgrhrir$r)r.r2r6rGrkr%r#rrs3MI $4e$L$ Nr%rc eZdZdZdZdZdZy)rz?An EdgeDataView class for edges of MultiGraph; See EdgeDataViewrkc #Ki}|jD]U\}}|jD]8\}}||vs |jD]\}}|j||||:d||<W~ywr9r:r"r;rDrrrVrJr}s r#r6zMultiEdgeDataView.__iter__s'') GAt::< :Rd?!#:2"ll1c1b99: :DG   s 3A/9A/cdd\jjvrjvry j}jdur"d} ||}j ||k(St fd|j DS#t$r' j}n#t$rYYywxYwYwxYw#t$rYywxYw)Nr$FTc3RK|]\}}j||k( ywr+r[r\s r#r~z1MultiEdgeDataView.__contains__..r]rr^r`s`` @@r#rGzMultiEdgeDataView.__contains__s!u1 << #(=!4<.sj 4::< R  2 LLaB ' ' ' rWr r!s`r#r6zInMultiEdgeDataView.__iter__rXr%c<dd\jjvry j}jdur!d}||}j ||k(St fd|j DS#t$rYywxYw)Nr$FTc3RK|]\}}j||k( ywr+r[r\s r#r~z3InMultiEdgeDataView.__contains__..r]rr^r`s`` @@r#rGz InMultiEdgeDataView.__contains__s!u1 << #(= MM!$Q'E 99 !AqB Q1b11 1Mu{{}MMM   sB BBNrArkr%r#rrsLI  Nr%rcteZdZdZdZdZdZedZe Z dZ dZ dZ d Zd Zdd d d ZddZdZdZy )r z/A EdgeView class for outward edges of a DiGraph)rrrc4|j|jdS)Nrrrnr!s r#r$zOutEdgeView.__getstate__s++4==AAr%cb|d|_|d|_|jj|_yNrrrrrrr's r#r)zOutEdgeView.__setstate__ +Ho j) ==..r%ct|Sr+rIrKs r#rNzOutEdgeView._from_iterable%rOr%c||_t|dr |jn |j|_|jj |_y)Nsucc)rrrrrrrr"rs r#r.zOutEdgeView.__init__+5 #*1f#5166 ==..r%cBtd|jDS)Nc38K|]\}}t|ywr+rrs r#r~z&OutEdgeView.__len__..2rrrr!s r#r2zOutEdgeView.__len__1rr%c#VK|jD]\}}|D]}||f ywr+r r"rDrrs r#r6zOutEdgeView.__iter__4s:'') GAt #h  ')cN |\}}||j|vS#t$rYywxYwrQrr&r"r(r)r*s r#rGzOutEdgeView.__contains__96 DAq a(( (    $$c 6t|trRtjt |j d|j d|jd|jd|\}} |j||S#t$r}td|dd}~wwxYw)N- does not support slicing, try list(G.edges)[r8r9z The edge z is not in the graph.) r:r;r<r=r>r?r@rArBrr&)r"r(r)r*exs r#rEzOutEdgeView.__getitem__As a "":&&'(%%&WWIQqvvhaxqB 1 A==#A& & AYqc)>?@ @ As)A;; BBBNrc<||dur|S|j||||S)NFrdataview)r"rrTrUs r#rVzOutEdgeView.__call__Ns) >demK}}T64}AAr%c<||dur|S|j||||S)ab Return a read-only view of edge data. Parameters ---------- data : bool or edge attribute key If ``data=True``, then the data view maps each edge to a dictionary containing all of its attributes. If `data` is a key in the edge dictionary, then the data view maps each edge to its value for the keyed attribute. In this case, if the edge doesn't have the attribute, the `default` value is returned. default : object, default=None The value used when an edge does not have a specific attribute nbunch : container of nodes, optional (default=None) Allows restriction to edges only involving certain nodes. All edges are considered by default. Returns ------- dataview Returns an `EdgeDataView` for undirected Graphs, `OutEdgeDataView` for DiGraphs, `MultiEdgeDataView` for MultiGraphs and `OutMultiEdgeDataView` for MultiDiGraphs. Notes ----- If ``data=False``, returns an `EdgeView` without any edge data. See Also -------- EdgeDataView OutEdgeDataView MultiEdgeDataView OutMultiEdgeDataView Examples -------- >>> G = nx.Graph() >>> G.add_edges_from( ... [ ... (0, 1, {"dist": 3, "capacity": 20}), ... (1, 2, {"dist": 4}), ... (2, 0, {"dist": 5}), ... ] ... ) Accessing edge data with ``data=True`` (the default) returns an edge data view object listing each edge with all of its attributes: >>> G.edges.data() EdgeDataView([(0, 1, {'dist': 3, 'capacity': 20}), (0, 2, {'dist': 5}), (1, 2, {'dist': 4})]) If `data` represents a key in the edge attribute dict, a dataview listing each edge with its value for that specific key is returned: >>> G.edges.data("dist") EdgeDataView([(0, 1, 3), (0, 2, 5), (1, 2, 4)]) `nbunch` can be used to limit the edges: >>> G.edges.data("dist", nbunch=[0]) EdgeDataView([(0, 1, 3), (0, 2, 5)]) If a specific key is not found in an edge attribute dict, the value specified by `default` is used: >>> G.edges.data("capacity") EdgeDataView([(0, 1, 20), (0, 2, None), (1, 2, None)]) Note that there is no check that the `data` key is present in any of the edge attribute dictionaries: >>> G.edges.data("speed") EdgeDataView([(0, 1, None), (0, 2, None), (1, 2, None)]) Frr)r"rTrUrs r#rTzOutEdgeView.dataSs,X >demK}}T64}AAr%c*tt|Sr+rYr!s r#r\zOutEdgeView.__str__r]r%cL|jjdt|dSr_r/r!s r#rdzOutEdgeView.__repr__rr%rQ)TNN)r?rfrgrhrir$r)rjrNr rr.r2r6rGrErVrTr\rdrkr%r#r r sk95IB/ H/ @  AB4B NBb:r%r c*eZdZdZdZeZdZdZdZ y)ra A EdgeView class for edges of a Graph This densely packed View allows iteration over edges, data lookup like a dict and set operations on edges represented by node-tuples. In addition, edge data can be controlled by calling this object possibly creating an EdgeDataView. Typically edges are iterated over and reported as `(u, v)` node tuples or `(u, v, key)` node/key tuples for multigraphs. Those edge representations can also be using to lookup the data dict for any edge. Set operations also are available where those tuples are the elements of the set. Calling this object with optional arguments `data`, `default` and `keys` controls the form of the tuple (see EdgeDataView). Optional argument `nbunch` allows restriction to edges only involving certain nodes. If `data is False` (the default) then iterate over 2-tuples `(u, v)`. If `data is True` iterate over 3-tuples `(u, v, datadict)`. Otherwise iterate over `(u, v, datadict.get(data, default))`. For Multigraphs, if `keys is True`, replace `u, v` with `u, v, key` above. Parameters ========== graph : NetworkX graph-like class nbunch : (default= all nodes in graph) only report edges with these nodes keys : (only for MultiGraph. default=False) report edge key in tuple data : bool or string (default=False) see above default : object (default=None) Examples ======== >>> G = nx.path_graph(4) >>> EV = G.edges() >>> (2, 3) in EV True >>> for u, v in EV: ... print((u, v)) (0, 1) (1, 2) (2, 3) >>> assert EV & {(1, 2), (3, 4)} == {(1, 2)} >>> EVdata = G.edges(data="color", default="aqua") >>> G.add_edge(2, 3, color="blue") >>> assert (2, 3, "blue") in EVdata >>> for u, v, c in EVdata: ... print(f"({u}, {v}) has color: {c}") (0, 1) has color: aqua (1, 2) has color: aqua (2, 3) has color: blue >>> EVnbunch = G.edges(nbunch=2) >>> assert (2, 3) in EVnbunch >>> assert (0, 1) not in EVnbunch >>> for u, v in EVnbunch: ... assert u == 2 or v == 2 >>> MG = nx.path_graph(4, create_using=nx.MultiGraph) >>> EVmulti = MG.edges(keys=True) >>> (2, 3, 0) in EVmulti True >>> (2, 3) in EVmulti # 2-tuples work even when keys is True True >>> key = MG.add_edge(2, 3) >>> for u, v, k in EVmulti: ... print((u, v, k)) (0, 1, 0) (1, 2, 0) (2, 3, 0) (2, 3, 1) rkcLd|jD}t|dzS)Nc3BK|]\}}t|||vzywr+rrs r#r~z#EdgeView.__len__..s!N4CId+Nsr$)rr)r"num_nbrss r#r2zEdgeView.__len__s$N4;K;K;MN8}!!r%c#Ki}|jD]%\}}t|D] }||vs||fd||<'~ywr9)rr[)r"r;rDrrs r#r6zEdgeView.__iter__sZ'') GAtDz #d?c(N #DG   s+??c |dd\}}||j|vxs||j|vS#ttf$rYywxYwr#)rr&rrs r#rGzEdgeView.__contains__sT Ra5DAq a((AAq1A,A A*%  s+.AAN) r?rfrgrhrir rr2r6rGrkr%r#rrs$DLIH"r%rc6eZdZdZdZdZeZdZdZ dZ dZ y) r z.A EdgeView class for inward edges of a DiGraphrkcb|d|_|d|_|jj|_yrprqr's r#r)zInEdgeView.__setstate__rrr%c||_t|dr |jn |j|_|jj |_yNpredrrrrrrrrvs r#r.zInEdgeView.__init__rwr%c#VK|jD]\}}|D]}||f ywr+r r{s r#r6zInEdgeView.__iter__s:'') GAt Ah  r|cN |\}}||j|vS#t$rYywxYwrQr~rs r#rGzInEdgeView.__contains__!rrc t|trRtjt |j d|j d|jd|jd|\}}|j||SNz0 does not support slicing, try list(G.in_edges)[r8r9 r:r;r<r=r>r?r@rArBrrs r#rEzInEdgeView.__getitem__(sx a "":&&'((()y!&&166(!E 1}}Q""r%N) r?rfrgrhrir)r rr.r6rGrErkr%r#r r s*8I/ H/  #r%r cHeZdZdZdZeZdZdZdZ dZ d ddd d Z d d Z y)rz4A EdgeView class for outward edges of a MultiDiGraphrkcBtd|jDS)Nc3hK|]*\}}|jD]\}}t|,ywr+)rr1)r|rDrrras r#r~z+OutMultiEdgeView.__len__..:s9 "1d4::< =GS%CJ  s02rr!s r#r2zOutMultiEdgeView.__len__9s% &*&6&6&8   r%c#K|jD]+\}}|jD]\}}|D] }|||f -ywr+rrr"rDrrrakeys r#r6zOutMultiEdgeView.__iter__>sV'') (GAt"jjl ( U (Cc3-'( ( (AAct|}|dk(r|\}}}n|dk(r|\}}d}n td ||j||vS#t$rYywxYwNr$rz!MultiEdge must have length 2 or 3Fr1rrr&r"r(Nr)r*rJs r#rGzOutMultiEdgeView.__contains__Dn F 6GAq! !VDAqA@A A  a(++ +  A AAc t|trRtjt |j d|j d|jd|jd|\}}}|j|||S)Nrr8r9rr"r(r)r*rJs r#rEzOutMultiEdgeView.__getitem__Rs a "":&&'(%%&WWIQqvvhaxqB 1a}}Q"1%%r%NFrEcF| |dur|dur|S|j|||||SNFTrEr)r"rrTrUrs r#rVzOutMultiEdgeView.__call__[1 >dem K}}T64t}LLr%cF| |dur|dur|S|j|||||Srr)r"rTrUrrs r#rTzOutMultiEdgeView.data`rr%rQ)TNNF) r?rfrgrhrirrr2r6rGrErVrTrkr%r#rr2s9>I#H ( &M4eM Mr%rc$eZdZdZdZeZdZdZy)rz*A EdgeView class for edges of a MultiGraphrkc&td|DS)Nc3 K|]}dywrrkr3s r#r~z(MultiEdgeView.__len__..nr4r5r6r!s r#r2zMultiEdgeView.__len__mr7r%c#Ki}|jD]F\}}|jD])\}}||vs |jD] \}}|||f+d||<H~ywr9rrds r#r6zMultiEdgeView.__iter__ps|'') GAt::< *Rd?!#*2 #qk)* *DG   s 3A *A N) r?rfrgrhrirrr2r6rkr%r#rrfs4I H$r%rc6eZdZdZdZdZeZdZdZ dZ dZ y) rz3A EdgeView class for inward edges of a MultiDiGraphrkcb|d|_|d|_|jj|_yrprqr's r#r)zInMultiEdgeView.__setstate__rrr%c||_t|dr |jn |j|_|jj |_yrrrvs r#r.zInMultiEdgeView.__init__rwr%c#K|jD]+\}}|jD]\}}|D] }|||f -ywr+rrs r#r6zInMultiEdgeView.__iter__sV'') (GAt"jjl ( U (C3-'( ( (rct|}|dk(r|\}}}n|dk(r|\}}d}n td ||j||vS#t$rYywxYwrrrs r#rGzInMultiEdgeView.__contains__rrc t|trRtjt |j d|j d|jd|jd|\}}}|j|||Srrrs r#rEzInMultiEdgeView.__getitem__s a "":&&'((()y!&&166(!E 1a}}Q"1%%r%N) r?rfrgrhrir)rrr.r6rGrErkr%r#rr{s*=I/ #H/ ( &r%r) rhabcrcollections.abcrrnetworkxr<__all__rrrrrrrrrrrr r r rrrr rr rrrrkr%r#rsaSj( 6f;wf;R\<3\<@\:\:~AAHL.<.%l%P* *Z >D # H:kH:V3.?3.l._..QN?QNh!N,!NHN.N:P:#w P:f]{]@$#$#N1M{1Mh$*,&&,&r%