MCPcopy Index your code
hub / github.com/python-cmd2/cmd2 / span

Method span

cmd2/history.py:226–273  ·  view source on GitHub ↗

Return a slice of the History list. :param span: string containing an index or a slice :param include_persisted: if True, then retrieve full results including from persisted history :return: a dictionary of history items keyed by their 1-based index in ascending order,

(self, span: str, include_persisted: bool = False)

Source from the content-addressed store, hash-verified

224 spanpattern = re.compile(r"^\s*(?P<start>-?[1-9]\d*)?(?P<separator>:|(\.{2,}))(?P<end>-?[1-9]\d*)?\s*$")
225
226 def span(self, span: str, include_persisted: bool = False) -> dict[int, "HistoryItem"]:
227 """Return a slice of the History list.
228
229 :param span: string containing an index or a slice
230 :param include_persisted: if True, then retrieve full results including from persisted history
231 :return: a dictionary of history items keyed by their 1-based index in ascending order,
232 or an empty dictionary if no results were found
233
234 This method can accommodate input in any of these forms:
235
236 a..b or a:b
237 a.. or a:
238 ..a or :a
239 -a.. or -a:
240 ..-a or :-a
241
242 Different from native python indexing and slicing of arrays, this method
243 uses 1-based array numbering. Users who are not programmers can&#x27;t grok
244 zero based numbering. Programmers can sometimes grok zero based numbering.
245 Which reminds me, there are only two hard problems in programming:
246
247 - naming
248 - cache invalidation
249 - off by one errors
250
251 """
252 results = self.spanpattern.search(span)
253 if not results:
254 # our regex doesn't match the input, bail out
255 raise ValueError("History indices must be positive or negative integers, and may not be zero.")
256
257 start_token = results.group("start")
258 if start_token:
259 start = min(self._zero_based_index(start_token), len(self) - 1)
260 if start < 0:
261 start = max(0, len(self) + start)
262 else:
263 start = 0 if include_persisted else self.session_start_index
264
265 end_token = results.group("end")
266 if end_token:
267 end = min(int(end_token), len(self))
268 if end < 0:
269 end = max(0, len(self) + end + 1)
270 else:
271 end = len(self)
272
273 return self._build_result_dictionary(start, end)
274
275 def str_search(self, search: str, include_persisted: bool = False) -> dict[int, "HistoryItem"]:
276 """Find history items which contain a given string.

Callers 3

_get_historyMethod · 0.80
test_history_class_spanFunction · 0.80

Calls 2

_zero_based_indexMethod · 0.95

Tested by 2

test_history_class_spanFunction · 0.64