MCPcopy Index your code
hub / github.com/mongodb/mongo-python-driver / _index_document

Function _index_document

pymongo/helpers_shared.py:175–201  ·  view source on GitHub ↗

Helper to generate an index specifying document. Takes a list of (key, direction) pairs.

(index_list: _IndexList)

Source from the content-addressed store, hash-verified

173
174
175def _index_document(index_list: _IndexList) -> dict[str, Any]:
176 """Helper to generate an index specifying document.
177
178 Takes a list of (key, direction) pairs.
179 """
180 if not isinstance(index_list, (list, tuple, abc.Mapping)):
181 raise TypeError(
182 "must use a dictionary or a list of (key, direction) pairs, not: " + repr(index_list)
183 )
184 if not len(index_list):
185 raise ValueError("key_or_list must not be empty")
186
187 index: dict[str, Any] = {}
188
189 if isinstance(index_list, abc.Mapping):
190 for key in index_list:
191 value = index_list[key]
192 _validate_index_key_pair(key, value)
193 index[key] = value
194 else:
195 for item in index_list:
196 if isinstance(item, str):
197 item = (item, ASCENDING) # noqa: PLW2901
198 key, value = item
199 _validate_index_key_pair(key, value)
200 index[key] = value
201 return index
202
203
204def _validate_index_key_pair(key: Any, value: Any) -> None:

Callers 1

__init__Method · 0.90

Calls 1

_validate_index_key_pairFunction · 0.85

Tested by

no test coverage detected