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

Method __getitem__

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

Return value of corresponding key. If key does not exist, create it with default value. >>> g = Graph() >>> g[1].add([1,2]) >>> g[3][1] False >>> print g #NOTE: Vertex(3) created! {1: {1, 2}, 2: {}, 3: {}}

(self, vid)

Source from the content-addressed store, hash-verified

501 return True
502
503 def __getitem__(self, vid): #could just set equal to GraphBaseType.setdefault, but doctest module complains
504 """Return value of corresponding key. If key does not exist, create it
505 with default value.
506
507 >>> g = Graph()
508 >>> g[1].add([1,2])
509 >>> g[3][1]
510 False
511 >>> print g #NOTE: Vertex(3) created!
512 {1: {1, 2}, 2: {}, 3: {}}
513 """
514 return self.setdefault(vid, {}) #will convert plain {} to VertexType if necessary
515
516 def __setitem__(self, vid, value):
517 """Set graph[vid] to VertexType(value).

Callers 2

__init__Method · 0.45
__delitem__Method · 0.45

Calls 1

setdefaultMethod · 0.45

Tested by

no test coverage detected