MCPcopy Index your code
hub / github.com/MongoEngine/mongoengine / __getitem__

Method __getitem__

mongoengine/base/datastructures.py:124–148  ·  view source on GitHub ↗
(self, key)

Source from the content-addressed store, hash-verified

122 super().__init__(list_items)
123
124 def __getitem__(self, key):
125 # change index to positive value because MongoDB does not support negative one
126 if isinstance(key, int) and key < 0:
127 key = len(self) + key
128 value = super().__getitem__(key)
129
130 if isinstance(key, slice):
131 # When receiving a slice operator, we don't convert the structure and bind
132 # to parent's instance. This is buggy for now but would require more work to be handled properly
133 return value
134
135 EmbeddedDocument = _import_class("EmbeddedDocument")
136 if isinstance(value, EmbeddedDocument) and value._instance is None:
137 value._instance = self._instance
138 elif isinstance(value, dict) and not isinstance(value, BaseDict):
139 # Replace dict by BaseDict
140 value = BaseDict(value, None, f"{self._name}.{key}")
141 super().__setitem__(key, value)
142 value._instance = self._instance
143 elif isinstance(value, list) and not isinstance(value, BaseList):
144 # Replace list by BaseList
145 value = BaseList(value, None, f"{self._name}.{key}")
146 super().__setitem__(key, value)
147 value._instance = self._instance
148 return value
149
150 def __iter__(self):
151 yield from super().__iter__()

Callers

nothing calls this directly

Calls 5

_import_classFunction · 0.90
BaseDictClass · 0.85
BaseListClass · 0.85
__getitem__Method · 0.45
__setitem__Method · 0.45

Tested by

no test coverage detected