Determine the start and end indices of an element in a line. Args: line: (str) the line in which the element is to be sought. indices_list: (list of list of int) list of indices of the element to search for. Assumes that the indices in the batch are unique and sorted in asce
(line, indices_list, ref_indices)
| 427 | |
| 428 | |
| 429 | def _locate_elements_in_line(line, indices_list, ref_indices): |
| 430 | """Determine the start and end indices of an element in a line. |
| 431 | |
| 432 | Args: |
| 433 | line: (str) the line in which the element is to be sought. |
| 434 | indices_list: (list of list of int) list of indices of the element to |
| 435 | search for. Assumes that the indices in the batch are unique and sorted |
| 436 | in ascending order. |
| 437 | ref_indices: (list of int) reference indices, i.e., the indices of the |
| 438 | first element represented in the line. |
| 439 | |
| 440 | Returns: |
| 441 | start_columns: (list of int) start column indices, if found. If not found, |
| 442 | None. |
| 443 | end_columns: (list of int) end column indices, if found. If not found, |
| 444 | None. |
| 445 | If found, the element is represented in the left-closed-right-open interval |
| 446 | [start_column, end_column]. |
| 447 | """ |
| 448 | |
| 449 | batch_size = len(indices_list) |
| 450 | offsets = [indices[-1] - ref_indices[-1] for indices in indices_list] |
| 451 | |
| 452 | start_columns = [None] * batch_size |
| 453 | end_columns = [None] * batch_size |
| 454 | |
| 455 | if _NUMPY_OMISSION in line: |
| 456 | ellipsis_index = line.find(_NUMPY_OMISSION) |
| 457 | else: |
| 458 | ellipsis_index = len(line) |
| 459 | |
| 460 | matches_iter = re.finditer(_NUMBER_REGEX, line) |
| 461 | |
| 462 | batch_pos = 0 |
| 463 | |
| 464 | offset_counter = 0 |
| 465 | for match in matches_iter: |
| 466 | if match.start() > ellipsis_index: |
| 467 | # Do not attempt to search beyond ellipsis. |
| 468 | break |
| 469 | |
| 470 | if offset_counter == offsets[batch_pos]: |
| 471 | start_columns[batch_pos] = match.start() |
| 472 | # Remove the final comma, right bracket, or whitespace. |
| 473 | end_columns[batch_pos] = match.end() - 1 |
| 474 | |
| 475 | batch_pos += 1 |
| 476 | if batch_pos >= batch_size: |
| 477 | break |
| 478 | |
| 479 | offset_counter += 1 |
| 480 | |
| 481 | return start_columns, end_columns |
| 482 | |
| 483 | |
| 484 | def _pad_string_to_length(string, length): |
no test coverage detected