Used by dict_key_matches, matching the prefix to a list of keys Parameters ---------- keys list of keys in dictionary currently being completed. prefix Part of the text already typed by the user. E.g. `mydict[b'fo` delims String of delimiters to consider
(
keys: list[Union[str, bytes, tuple[Union[str, bytes], ...]]],
prefix: str,
delims: str,
extra_prefix: Optional[tuple[Union[str, bytes], ...]] = None,
)
| 1485 | |
| 1486 | |
| 1487 | def match_dict_keys( |
| 1488 | keys: list[Union[str, bytes, tuple[Union[str, bytes], ...]]], |
| 1489 | prefix: str, |
| 1490 | delims: str, |
| 1491 | extra_prefix: Optional[tuple[Union[str, bytes], ...]] = None, |
| 1492 | ) -> tuple[str, int, dict[str, _DictKeyState]]: |
| 1493 | """Used by dict_key_matches, matching the prefix to a list of keys |
| 1494 | |
| 1495 | Parameters |
| 1496 | ---------- |
| 1497 | keys |
| 1498 | list of keys in dictionary currently being completed. |
| 1499 | prefix |
| 1500 | Part of the text already typed by the user. E.g. `mydict[b'fo` |
| 1501 | delims |
| 1502 | String of delimiters to consider when finding the current key. |
| 1503 | extra_prefix : optional |
| 1504 | Part of the text already typed in multi-key index cases. E.g. for |
| 1505 | `mydict['foo', "bar", 'b`, this would be `('foo', 'bar')`. |
| 1506 | |
| 1507 | Returns |
| 1508 | ------- |
| 1509 | A tuple of three elements: ``quote``, ``token_start``, ``matched``, with |
| 1510 | ``quote`` being the quote that need to be used to close current string. |
| 1511 | ``token_start`` the position where the replacement should start occurring, |
| 1512 | ``matches`` a dictionary of replacement/completion keys on keys and values |
| 1513 | indicating whether the state. |
| 1514 | """ |
| 1515 | prefix_tuple = extra_prefix if extra_prefix else () |
| 1516 | |
| 1517 | prefix_tuple_size = sum( |
| 1518 | [ |
| 1519 | # for pandas, do not count slices as taking space |
| 1520 | not isinstance(k, slice) |
| 1521 | for k in prefix_tuple |
| 1522 | ] |
| 1523 | ) |
| 1524 | text_serializable_types = (str, bytes, int, float, slice) |
| 1525 | |
| 1526 | def filter_prefix_tuple(key): |
| 1527 | # Reject too short keys |
| 1528 | if len(key) <= prefix_tuple_size: |
| 1529 | return False |
| 1530 | # Reject keys which cannot be serialised to text |
| 1531 | for k in key: |
| 1532 | if not isinstance(k, text_serializable_types): |
| 1533 | return False |
| 1534 | # Reject keys that do not match the prefix |
| 1535 | for k, pt in zip(key, prefix_tuple): |
| 1536 | if k != pt and not isinstance(pt, slice): |
| 1537 | return False |
| 1538 | # All checks passed! |
| 1539 | return True |
| 1540 | |
| 1541 | filtered_key_is_final: dict[ |
| 1542 | Union[str, bytes, int, float], _DictKeyState |
| 1543 | ] = defaultdict(lambda: _DictKeyState.BASELINE) |
| 1544 |
searching dependent graphs…