Turn a string of range for %%load into 2-tuples of (start, stop) ready to use as a slice of the content split by lines. Examples -------- list(extract_input_ranges("5-10 2")) [(4, 10), (1, 2)]
(ranges_str)
| 55 | |
| 56 | |
| 57 | def extract_code_ranges(ranges_str): |
| 58 | """Turn a string of range for %%load into 2-tuples of (start, stop) |
| 59 | ready to use as a slice of the content split by lines. |
| 60 | |
| 61 | Examples |
| 62 | -------- |
| 63 | list(extract_input_ranges("5-10 2")) |
| 64 | [(4, 10), (1, 2)] |
| 65 | """ |
| 66 | for range_str in ranges_str.split(): |
| 67 | rmatch = range_re.match(range_str) |
| 68 | if not rmatch: |
| 69 | continue |
| 70 | sep = rmatch.group("sep") |
| 71 | start = rmatch.group("start") |
| 72 | end = rmatch.group("end") |
| 73 | |
| 74 | if sep == '-': |
| 75 | start = int(start) - 1 if start else None |
| 76 | end = int(end) if end else None |
| 77 | elif sep == ':': |
| 78 | start = int(start) - 1 if start else None |
| 79 | end = int(end) - 1 if end else None |
| 80 | else: |
| 81 | end = int(start) |
| 82 | start = int(start) - 1 |
| 83 | yield (start, end) |
| 84 | |
| 85 | |
| 86 | def extract_symbols(code, symbols): |