MCPcopy Create free account
hub / github.com/ActiveState/code / __delitem__

Method __delitem__

recipes/Python/578615_graph/recipe-578615.py:534–551  ·  view source on GitHub ↗

Delete a single vertex and associated edges. >>> g = Graph() >>> g.add([1, 2], [1, 2, 3]) >>> del g[2] #will remove vertex 2 and edges [(1, 2), (2, 1), (2, 2), (2, 3)] >>> print g {1: {1, 3}, 3: {}} Raises LookupError if given non-existant vertex.

(self, head)

Source from the content-addressed store, hash-verified

532 dict.__setitem__(self, vid, self.VertexType(self, vid, value)) #XXX shallow copy
533
534 def __delitem__(self, head):
535 """Delete a single vertex and associated edges.
536 >>> g = Graph()
537 >>> g.add([1, 2], [1, 2, 3])
538 >>> del g[2] #will remove vertex 2 and edges [(1, 2), (2, 1), (2, 2), (2, 3)]
539 >>> print g
540 {1: {1, 3}, 3: {}}
541
542 Raises LookupError if given non-existant vertex.
543 >>> del g[2]
544 Traceback (most recent call last):
545 ...
546 KeyError: 2
547 """
548 dict.__getitem__(self, head).clear() #removes out vertices (bypass key creation with dict.__getitem__)
549 for v in list(self[head].in_vertices()): #create copy (via list()) since in_vertices contents may change during iteration
550 del self[v][head]
551 super(Graph, self).__delitem__(head)
552
553 def __str__(self):
554 """Return graph in adjacency format.

Callers 2

__delitem__Method · 0.45
discardMethod · 0.45

Calls 4

listClass · 0.85
clearMethod · 0.45
__getitem__Method · 0.45
in_verticesMethod · 0.45

Tested by

no test coverage detected