MCPcopy
hub / github.com/ormar-orm/ormar / normalize_slice

Function normalize_slice

ormar/queryset/utils.py:103–129  ·  view source on GitHub ↗

Top-level dispatcher: turns a Python integer index or slice into a :class:`SliceBounds` suitable for ``QuerySet``. Delegates to a dedicated helper for each shape. Any shape that would require a ``COUNT(*)`` round-trip (``step != 1``, a bare ``[:-N]``, or mixed-sign bounds) rais

(key: Union[int, slice])

Source from the content-addressed store, hash-verified

101
102
103def normalize_slice(key: Union[int, slice]) -> SliceBounds:
104 """
105 Top-level dispatcher: turns a Python integer index or slice into a
106 :class:`SliceBounds` suitable for ``QuerySet``.
107
108 Delegates to a dedicated helper for each shape. Any shape that would
109 require a ``COUNT(*)`` round-trip (``step != 1``, a bare ``[:-N]``, or
110 mixed-sign bounds) raises ``QueryDefinitionError``.
111
112 Examples::
113
114 5 → SliceBounds(limit=1, offset=5, reverse=False)
115 slice(2, 8) → SliceBounds(limit=6, offset=2, reverse=False)
116 slice(-3, None) → SliceBounds(limit=3, offset=0, reverse=True)
117
118 :param key: integer or slice passed to ``QuerySet.__getitem__``
119 :type key: int | slice
120 :return: normalized slice parameters
121 :rtype: SliceBounds
122 """
123 if isinstance(key, int):
124 return _int_to_limit_offset(key)
125 if isinstance(key, slice):
126 return _slice_to_limit_offset(key)
127 raise QueryDefinitionError(
128 f"QuerySet indices must be integers or slices, not {type(key).__name__}."
129 )
130
131
132def _int_to_limit_offset(key: int) -> SliceBounds:

Calls 3

_int_to_limit_offsetFunction · 0.85
_slice_to_limit_offsetFunction · 0.85