MCPcopy Index your code
hub / github.com/ipython/ipython / extract_hist_ranges

Function extract_hist_ranges

IPython/core/history.py:1213–1267  ·  view source on GitHub ↗

Turn a string of history ranges into 3-tuples of (session, start, stop). Empty string results in a `[(0, 1, None)]`, i.e. "everything from current session". Examples -------- >>> list(extract_hist_ranges("~8/5-~7/4 2")) [(-8, 5, None), (-7, 1, 5), (0, 2, 3)] >>> list(ex

(ranges_str: str)

Source from the content-addressed store, hash-verified

1211
1212
1213def extract_hist_ranges(ranges_str: str) -> Iterable[tuple[int, int, Optional[int]]]:
1214 """Turn a string of history ranges into 3-tuples of (session, start, stop).
1215
1216 Empty string results in a `[(0, 1, None)]`, i.e. "everything from current
1217 session".
1218
1219 Examples
1220 --------
1221 >>> list(extract_hist_ranges("~8/5-~7/4 2"))
1222 [(-8, 5, None), (-7, 1, 5), (0, 2, 3)]
1223 >>> list(extract_hist_ranges("~4/"))
1224 [(-4, 1, None)]
1225 >>> list(extract_hist_ranges("4-"))
1226 [(0, 4, None)]
1227 >>> list(extract_hist_ranges("~4/4-"))
1228 [(-4, 4, None)]
1229 """
1230 if ranges_str == "":
1231 yield (0, 1, None) # Everything from current session
1232 return
1233
1234 for range_str in ranges_str.split():
1235 rmatch = range_re.match(range_str)
1236 if not rmatch:
1237 continue
1238 start = rmatch.group("start")
1239 sep = rmatch.group("sep")
1240 if start:
1241 start = int(start)
1242 end = rmatch.group("end")
1243 if sep == "-":
1244 end = (int(end) + 1) if end else None
1245 else:
1246 end = int(end) if end else start + 1
1247 else:
1248 if not rmatch.group("startsess"):
1249 continue
1250 start = 1
1251 end = None
1252 startsess = rmatch.group("startsess") or "0"
1253 endsess = rmatch.group("endsess") or startsess
1254 startsess = startsess.rstrip("/")
1255 endsess = endsess.rstrip("/")
1256 startsess = int(startsess.replace("~", "-"))
1257 endsess = int(endsess.replace("~", "-"))
1258 assert endsess >= startsess, "start session must be earlier than end session"
1259
1260 if endsess == startsess:
1261 yield (startsess, start, end)
1262 continue
1263 # Multiple sessions in one range:
1264 yield (startsess, start, None)
1265 for sess in range(startsess + 1, endsess):
1266 yield (sess, 1, None)
1267 yield (endsess, 1, end)
1268
1269
1270def _format_lineno(session: int, line: int) -> str:

Callers 4

test_extract_hist_rangesFunction · 0.90
get_range_by_strMethod · 0.85

Calls 3

matchMethod · 0.80
groupMethod · 0.80
replaceMethod · 0.80

Tested by 3

test_extract_hist_rangesFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…