MCPcopy Create free account
hub / github.com/ipython/ipython / extract_hist_ranges

Function extract_hist_ranges

IPython/core/history.py:860–899  ·  view source on GitHub ↗

Turn a string of history ranges into 3-tuples of (session, start, stop). Examples -------- >>> list(extract_hist_ranges("~8/5-~7/4 2")) [(-8, 5, None), (-7, 1, 5), (0, 2, 3)]

(ranges_str)

Source from the content-addressed store, hash-verified

858
859
860def extract_hist_ranges(ranges_str):
861 """Turn a string of history ranges into 3-tuples of (session, start, stop).
862
863 Examples
864 --------
865 >>> list(extract_hist_ranges("~8/5-~7/4 2"))
866 [(-8, 5, None), (-7, 1, 5), (0, 2, 3)]
867 """
868 for range_str in ranges_str.split():
869 rmatch = range_re.match(range_str)
870 if not rmatch:
871 continue
872 start = rmatch.group("start")
873 if start:
874 start = int(start)
875 end = rmatch.group("end")
876 # If no end specified, get (a, a + 1)
877 end = int(end) if end else start + 1
878 else: # start not specified
879 if not rmatch.group('startsess'): # no startsess
880 continue
881 start = 1
882 end = None # provide the entire session hist
883
884 if rmatch.group("sep") == "-": # 1-3 == 1:4 --> [1, 2, 3]
885 end += 1
886 startsess = rmatch.group("startsess") or "0"
887 endsess = rmatch.group("endsess") or startsess
888 startsess = int(startsess.replace("~","-"))
889 endsess = int(endsess.replace("~","-"))
890 assert endsess >= startsess, "start session must be earlier than end session"
891
892 if endsess == startsess:
893 yield (startsess, start, end)
894 continue
895 # Multiple sessions in one range:
896 yield (startsess, start, None)
897 for sess in range(startsess+1, endsess):
898 yield (sess, 1, None)
899 yield (endsess, 1, end)
900
901
902def _format_lineno(session, line):

Callers 2

test_extract_hist_rangesFunction · 0.90
get_range_by_strMethod · 0.85

Calls 1

groupMethod · 0.80

Tested by 1

test_extract_hist_rangesFunction · 0.72