/
graph.py
1594 lines (1296 loc) · 45.2 KB
/
graph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import print_function
from __future__ import absolute_import
from __future__ import division
from random import sample
from ast import literal_eval
from compas.datastructures.datastructure import Datastructure
from compas.datastructures.attributes import NodeAttributeView
from compas.datastructures.attributes import EdgeAttributeView
class Graph(Datastructure):
"""Base graph data structure for describing the topological relationships between nodes connected by edges.
Parameters
----------
default_node_attributes : dict[str, Any], optional
Default values for node attributes.
default_edge_attributes : dict[str, Any], optional
Default values for edge attributes.
**kwargs : dict, optional
Additional keyword arguments are passed to the base class, and will be stored in the :attr:`attributes` attribute.
Attributes
----------
default_node_attributes : dict[str, Any]
dictionary containing default values for the attributes of nodes.
It is recommended to add a default to this dictionary using :meth:`update_default_node_attributes`
for every node attribute used in the data structure.
default_edge_attributes : dict[str, Any]
dictionary containing default values for the attributes of edges.
It is recommended to add a default to this dictionary using :meth:`update_default_edge_attributes`
for every edge attribute used in the data structure.
See Also
--------
:class:`compas.datastructures.Network`
"""
DATASCHEMA = {
"type": "object",
"properties": {
"dna": {"type": "object"},
"dea": {"type": "object"},
"node": {
"type": "object",
"additionalProperties": {"type": "object"},
},
"edge": {
"type": "object",
"additionalProperties": {
"type": "object",
"additionalProperties": {"type": "object"},
},
},
"max_node": {"type": "integer", "minimum": -1},
},
"required": [
"dna",
"dea",
"node",
"edge",
"max_node",
],
}
def __init__(self, default_node_attributes=None, default_edge_attributes=None, **kwargs):
super(Graph, self).__init__(**kwargs)
self._max_node = -1
self.node = {}
self.edge = {}
self.adjacency = {}
self.default_node_attributes = {}
self.default_edge_attributes = {}
if default_node_attributes:
self.default_node_attributes.update(default_node_attributes)
if default_edge_attributes:
self.default_edge_attributes.update(default_edge_attributes)
def __str__(self):
tpl = "<Graph with {} nodes, {} edges>"
return tpl.format(self.number_of_nodes(), self.number_of_edges())
# --------------------------------------------------------------------------
# Data
# --------------------------------------------------------------------------
@property
def data(self):
data = {
"dna": self.default_node_attributes,
"dea": self.default_edge_attributes,
"node": {},
"edge": {},
"max_node": self._max_node,
}
for key in self.node:
data["node"][repr(key)] = self.node[key]
for u in self.edge:
ru = repr(u)
data["edge"][ru] = {}
for v in self.edge[u]:
rv = repr(v)
data["edge"][ru][rv] = self.edge[u][v]
return data
@classmethod
def from_data(cls, data):
dna = data.get("dna") or {}
dea = data.get("dea") or {}
node = data.get("node") or {}
edge = data.get("edge") or {}
graph = cls(default_node_attributes=dna, default_edge_attributes=dea)
for node, attr in iter(node.items()):
node = literal_eval(node)
graph.add_node(key=node, attr_dict=attr)
for u, nbrs in iter(edge.items()):
u = literal_eval(u)
for v, attr in iter(nbrs.items()):
v = literal_eval(v)
graph.add_edge(u, v, attr_dict=attr)
graph._max_node = data.get("max_node", graph._max_node)
return graph
# --------------------------------------------------------------------------
# Properties
# --------------------------------------------------------------------------
# --------------------------------------------------------------------------
# Constructors
# --------------------------------------------------------------------------
@classmethod
def from_edges(cls, edges):
"""Create a new graph instance from information about the edges.
Parameters
----------
edges : list[tuple[hashable, hashable]]
The edges of the graph as pairs of node identifiers.
Returns
-------
:class:`compas.datastructures.Graph`
See Also
--------
:meth:`from_networkx`
"""
graph = cls()
for u, v in edges:
if u not in graph.node:
graph.add_node(u)
if v not in graph.node:
graph.add_node(v)
graph.add_edge(u, v)
@classmethod
def from_networkx(cls, graph):
"""Create a new graph instance from a NetworkX DiGraph instance.
Parameters
----------
graph : networkx.DiGraph
NetworkX instance of a directed graph.
Returns
-------
:class:`compas.datastructures.Graph`
See Also
--------
:meth:`to_networkx`
:meth:`from_edges`
"""
g = cls()
g.attributes.update(graph.graph)
for node in graph.nodes():
g.add_node(node, **graph.nodes[node])
for edge in graph.edges():
g.add_edge(*edge, **graph.edges[edge])
return g
def to_networkx(self):
"""Create a new NetworkX graph instance from a graph.
Returns
-------
networkx.DiGraph
A newly created NetworkX DiGraph.
See Also
--------
:meth:`from_networkx`
"""
import networkx as nx
G = nx.DiGraph()
G.graph.update(self.attributes) # type: ignore
for node, attr in self.nodes(data=True):
G.add_node(node, **attr) # type: ignore
for edge, attr in self.edges(data=True):
G.add_edge(*edge, **attr)
return G
# --------------------------------------------------------------------------
# Helpers
# --------------------------------------------------------------------------
def clear(self):
"""Clear all the network data.
Returns
-------
None
"""
del self.node
del self.edge
del self.adjacency
self.node = {}
self.edge = {}
self.adjacency = {}
def node_sample(self, size=1):
"""Get a list of identifiers of a random set of n nodes.
Parameters
----------
size : int, optional
The size of the sample.
Returns
-------
list[hashable]
The identifiers of the nodes.
See Also
--------
:meth:`edge_sample`
"""
return sample(list(self.nodes()), size)
def edge_sample(self, size=1):
"""Get the identifiers of a set of random edges.
Parameters
----------
size : int, optional
The size of the sample.
Returns
-------
list[tuple[hashable, hashable]]
The identifiers of the random edges.
See Also
--------
:meth:`node_sample`
"""
return sample(list(self.edges()), size)
def node_index(self):
"""Returns a dictionary that maps node identifiers to their corresponding index in a node list or array.
Returns
-------
dict[hashable, int]
A dictionary of node-index pairs.
See Also
--------
:meth:`index_node`
:meth:`edge_index`
"""
return {key: index for index, key in enumerate(self.nodes())}
def index_node(self):
"""Returns a dictionary that maps the indices of a node list to keys in a node dictionary.
Returns
-------
dict[int, hashable]
A dictionary of index-node pairs.
See Also
--------
:meth:`node_index`
:meth:`index_edge`
"""
return dict(enumerate(self.nodes()))
def edge_index(self):
"""Returns a dictionary that maps edge identifiers (i.e. pairs of vertex identifiers)
to the corresponding edge index in a list or array of edges.
Returns
-------
dict[tuple[hashable, hashable], int]
A dictionary of uv-index pairs.
See Also
--------
:meth:`index_edge`
:meth:`node_index`
"""
return {(u, v): index for index, (u, v) in enumerate(self.edges())}
def index_edge(self):
"""Returns a dictionary that maps edges in a list to the corresponding
vertex identifier pairs.
Returns
-------
dict[int, tuple[hashable, hashable]]
A dictionary of index-uv pairs.
See Also
--------
:meth:`edge_index`
:meth:`index_node`
"""
return dict(enumerate(self.edges()))
# --------------------------------------------------------------------------
# Builders
# --------------------------------------------------------------------------
def add_node(self, key=None, attr_dict=None, **kwattr):
"""Add a node and specify its attributes (optional).
Parameters
----------
key : hashable, optional
An identifier for the node.
Defaults to None, in which case an identifier of type int is automatically generated.
attr_dict : dict[str, Any], optional
A dictionary of vertex attributes.
**kwattr : dict[str, Any], optional
A dictionary of additional attributes compiled of remaining named arguments.
Returns
-------
hashable
The identifier of the node.
See Also
--------
:meth:`add_edge`
:meth:`delete_node`
Notes
-----
If no key is provided for the node, one is generated
automatically. An automatically generated key increments the highest
integer key in use by 1.
Examples
--------
>>> graph = Graph()
>>> node = graph.add_node()
>>> node
0
"""
if key is None:
key = self._max_node = self._max_node + 1
try:
if key > self._max_node:
self._max_node = key
except (ValueError, TypeError):
pass
if key not in self.node:
self.node[key] = {}
self.edge[key] = {}
self.adjacency[key] = {}
attr = attr_dict or {}
attr.update(kwattr)
self.node[key].update(attr)
return key
def add_edge(self, u, v, attr_dict=None, **kwattr):
"""Add an edge and specify its attributes.
Parameters
----------
u : hashable
The identifier of the first node of the edge.
v : hashable
The identifier of the second node of the edge.
attr_dict : dict[str, Any], optional
A dictionary of edge attributes.
**kwattr : dict[str, Any], optional
A dictionary of additional attributes compiled of remaining named arguments.
Returns
-------
tuple[hashable, hashable]
The identifier of the edge.
See Also
--------
:meth:`add_node`
:meth:`delete_edge`
Examples
--------
>>>
"""
attr = attr_dict or {}
attr.update(kwattr)
if u not in self.node:
u = self.add_node(u)
if v not in self.node:
v = self.add_node(v)
data = self.edge[u].get(v, {})
data.update(attr)
self.edge[u][v] = data
if v not in self.adjacency[u]:
self.adjacency[u][v] = None
if u not in self.adjacency[v]:
self.adjacency[v][u] = None
return u, v
# --------------------------------------------------------------------------
# Modifiers
# --------------------------------------------------------------------------
def delete_node(self, key):
"""Delete a node from the graph.
Parameters
----------
key : hashable
The identifier of the node.
Returns
-------
None
See Also
--------
:meth:`delete_edge`
:meth:`add_node`
Examples
--------
>>>
"""
if key in self.edge:
del self.edge[key]
if key in self.adjacency:
del self.adjacency[key]
if key in self.node:
del self.node[key]
for u in list(self.edge):
for v in list(self.edge[u]):
if v == key:
del self.edge[u][v]
for u in self.adjacency:
for v in list(self.adjacency[u]):
if v == key:
del self.adjacency[u][v]
def delete_edge(self, edge):
"""Delete an edge from the network.
Parameters
----------
edge : tuple[hashable, hashable]
The identifier of the edge as a pair of node identifiers.
Returns
-------
None
See Also
--------
:meth:`delete_node`
:meth:`add_edge`
Examples
--------
>>>
"""
u, v = edge
if u in self.edge and v in self.edge[u]:
del self.edge[u][v]
if u == v: # invalid edge
del self.adjacency[u][v]
elif v not in self.edge or u not in self.edge[v]:
del self.adjacency[u][v]
del self.adjacency[v][u]
# else: an edge in an opposite direction exists, we don't want to delete the adjacency
# --------------------------------------------------------------------------
# Info
# --------------------------------------------------------------------------
def summary(self):
"""Return a summary of the graph.
Returns
-------
str
The formatted summary.
"""
tpl = "\n".join(
[
"{} summary",
"=" * (len(self.name) + len(" summary")),
"- nodes: {}",
"- edges: {}",
]
)
return tpl.format(self.name, self.number_of_nodes(), self.number_of_edges())
def number_of_nodes(self):
"""Compute the number of nodes of the graph.
Returns
-------
int
The number of nodes.
See Also
--------
:meth:`number_of_edges`
"""
return len(list(self.nodes()))
def number_of_edges(self):
"""Compute the number of edges of the graph.
Returns
-------
int
The number of edges.
See Also
--------
:meth:`number_of_nodes`
"""
return len(list(self.edges()))
# --------------------------------------------------------------------------
# Accessors
# --------------------------------------------------------------------------
def nodes(self, data=False):
"""Iterate over the nodes of the network.
Parameters
----------
data : bool, optional
If True, yield the node attributes in addition to the node identifiers.
Yields
------
hashable | tuple[hashable, dict[str, Any]]
If `data` is False, the next node identifier.
If `data` is True, the next node as a (key, attr) tuple.
See Also
--------
:meth:`nodes_where`, :meth:`nodes_where_predicate`
:meth:`edges`, :meth:`edges_where`, :meth:`edges_where_predicate`
"""
for key in self.node:
if not data:
yield key
else:
yield key, self.node_attributes(key)
def nodes_where(self, conditions=None, data=False, **kwargs):
"""Get nodes for which a certain condition or set of conditions is true.
Parameters
----------
conditions : dict, optional
A set of conditions in the form of key-value pairs.
The keys should be attribute names. The values can be attribute
values or ranges of attribute values in the form of min/max pairs.
data : bool, optional
If True, yield the node attributes in addition to the node identifiers.
Yields
------
hashable | tuple[hashable, dict[str, Any]]
If `data` is False, the next node that matches the condition.
If `data` is True, the next node and its attributes.
See Also
--------
:meth:`nodes`, :meth:`nodes_where_predicate`
:meth:`edges`, :meth:`edges_where`, :meth:`edges_where_predicate`
"""
conditions = conditions or {}
conditions.update(kwargs)
for key, attr in self.nodes(True):
is_match = True
attr = attr or {}
for name, value in conditions.items():
method = getattr(self, name, None)
if callable(method):
val = method(key)
if isinstance(val, list):
if value not in val:
is_match = False
break
break
if isinstance(value, (tuple, list)):
minval, maxval = value
if val < minval or val > maxval:
is_match = False
break
else:
if value != val:
is_match = False
break
else:
if name not in attr:
is_match = False
break
if isinstance(attr[name], list):
if value not in attr[name]:
is_match = False
break
break
if isinstance(value, (tuple, list)):
minval, maxval = value
if attr[name] < minval or attr[name] > maxval:
is_match = False
break
else:
if value != attr[name]:
is_match = False
break
if is_match:
if data:
yield key, attr
else:
yield key
def nodes_where_predicate(self, predicate, data=False):
"""Get nodes for which a certain condition or set of conditions is true using a lambda function.
Parameters
----------
predicate : callable
The condition you want to evaluate.
The callable takes 2 parameters: the node identifier and the node attributes, and should return True or False.
data : bool, optional
If True, yield the node attributes in addition to the node identifiers.
Yields
------
hashable | tuple[hashable, dict[str, Any]]
If `data` is False, the next node that matches the condition.
If `data` is True, the next node and its attributes.
See Also
--------
:meth:`nodes`, :meth:`nodes_where`
:meth:`edges`, :meth:`edges_where`, :meth:`edges_where_predicate`
Examples
--------
>>>
"""
for key, attr in self.nodes(True):
if predicate(key, attr):
if data:
yield key, attr
else:
yield key
def edges(self, data=False):
"""Iterate over the edges of the network.
Parameters
----------
data : bool, optional
If True, yield the edge attributes in addition to the edge identifiers.
Yields
------
tuple[hashable, hashable] | tuple[tuple[hashable, hashable], dict[str, Any]]
If `data` is False, the next edge identifier (u, v).
If `data` is True, the next edge identifier and its attributes as a ((u, v), attr) tuple.
See Also
--------
:meth:`edges_where`, :meth:`edges_where_predicate`
:meth:`nodes`, :meth:`nodes_where`, :meth:`nodes_where_predicate`
"""
for u, nbrs in iter(self.edge.items()):
for v, attr in iter(nbrs.items()):
if data:
yield (u, v), attr
else:
yield u, v
def edges_where(self, conditions=None, data=False, **kwargs):
"""Get edges for which a certain condition or set of conditions is true.
Parameters
----------
conditions : dict, optional
A set of conditions in the form of key-value pairs.
The keys should be attribute names. The values can be attribute
values or ranges of attribute values in the form of min/max pairs.
data : bool, optional
If True, yield the edge attributes in addition to the edge identifiers.
**kwargs : dict[str, Any], optional
Additional conditions provided as named function arguments.
Yields
------
tuple[hashable, hashable] | tuple[tuple[hashable, hashable], dict[str, Any]]
If `data` is False, the next edge identifier (u, v).
If `data` is True, the next edge identifier and its attributes as a ((u, v), attr) tuple.
See Also
--------
:meth:`edges`, :meth:`edges_where_predicate`
:meth:`nodes`, :meth:`nodes_where`, :meth:`nodes_where_predicate`
"""
conditions = conditions or {}
conditions.update(kwargs)
for key in self.edges():
is_match = True
attr = self.edge_attributes(key) or {}
for name, value in conditions.items():
method = getattr(self, name, None)
if method and callable(method):
val = method(key)
elif name in attr:
val = attr[name]
else:
is_match = False
break
if isinstance(val, list):
if value not in val:
is_match = False
break
elif isinstance(value, (tuple, list)):
minval, maxval = value
if val < minval or val > maxval:
is_match = False
break
else:
if value != val:
is_match = False
break
if is_match:
if data:
yield key, attr
else:
yield key
def edges_where_predicate(self, predicate, data=False):
"""Get edges for which a certain condition or set of conditions is true using a lambda function.
Parameters
----------
predicate : callable
The condition you want to evaluate.
The callable takes 2 parameters:
an edge identifier (tuple of node identifiers) and edge attributes,
and should return True or False.
data : bool, optional
If True, yield the edge attributes in addition to the edge attributes.
Yields
------
tuple[hashable, hashable] | tuple[tuple[hashable, hashable], dict[str, Any]]
If `data` is False, the next edge identifier (u, v).
If `data` is True, the next edge identifier and its attributes as a ((u, v), attr) tuple.
See Also
--------
:meth:`edges`, :meth:`edges_where`
:meth:`nodes`, :meth:`nodes_where`, :meth:`nodes_where_predicate`
Examples
--------
>>>
"""
for key, attr in self.edges(True):
if predicate(key, attr):
if data:
yield key, attr
else:
yield key
# --------------------------------------------------------------------------
# default attributes
# --------------------------------------------------------------------------
def update_default_node_attributes(self, attr_dict=None, **kwattr):
"""Update the default node attributes.
Parameters
----------
attr_dict : dict[str, Any], optional
A dictionary of attributes with their default values.
**kwattr : dict[str, Any], optional
A dictionary of additional attributes compiled of remaining named arguments.
Returns
-------
None
See Also
--------
:meth:`update_default_edge_attributes`
"""
if not attr_dict:
attr_dict = {}
attr_dict.update(kwattr)
self.default_node_attributes.update(attr_dict)
def update_default_edge_attributes(self, attr_dict=None, **kwattr):
"""Update the default edge attributes.
Parameters
----------
attr_dict : dict[str, Any], optional
A dictionary of attributes with their default values.
**kwattr : dict[str, Any], optional
A dictionary of additional attributes compiled of remaining named arguments.
Returns
-------
None
See Also
--------
:meth:`update_default_node_attributes`
"""
if not attr_dict:
attr_dict = {}
attr_dict.update(kwattr)
self.default_edge_attributes.update(attr_dict)
update_dna = update_default_node_attributes
update_dea = update_default_edge_attributes
# --------------------------------------------------------------------------
# Node attributes
# --------------------------------------------------------------------------
def node_attribute(self, key, name, value=None):
"""Get or set an attribute of a node.
Parameters
----------
key : hashable
The node identifier.
name : str
The name of the attribute
value : obj, optional
The value of the attribute.
Returns
-------
obj or None
The value of the attribute,
or None when the function is used as a "setter".
Raises
------
KeyError
If the node does not exist.
See Also
--------
:meth:`unset_node_attribute`
:meth:`node_attributes`, :meth:`nodes_attribute`, :meth:`nodes_attributes`
:meth:`edge_attribute`, :meth:`edge_attributes`, :meth:`edges_attribute`, :meth:`edges_attributes`
"""
if key not in self.node:
raise KeyError(key)
if value is not None:
self.node[key][name] = value
return
if name in self.node[key]:
return self.node[key][name]
else:
if name in self.default_node_attributes:
return self.default_node_attributes[name]
def unset_node_attribute(self, key, name):
"""Unset the attribute of a node.
Parameters
----------
key : int
The node identifier.
name : str
The name of the attribute.
Raises
------
KeyError
If the node does not exist.
See Also
--------
:meth:`node_attribute`
Notes
-----
Unsetting the value of a node attribute implicitly sets it back to the value
stored in the default node attribute dict.
"""
if name in self.node[key]:
del self.node[key][name]
def node_attributes(self, key, names=None, values=None):
"""Get or set multiple attributes of a node.
Parameters
----------
key : hashable
The identifier of the node.
names : list[str], optional
A list of attribute names.
values : list[Any], optional
A list of attribute values.
Returns
-------
dict[str, Any] | list[Any] | None
If the parameter `names` is empty,
the function returns a dictionary of all attribute name-value pairs of the node.
If the parameter `names` is not empty,
the function returns a list of the values corresponding to the requested attribute names.
The function returns None if it is used as a "setter".
Raises
------
KeyError
If the node does not exist.
See Also
--------