Match string keys in a dictionary, after e.g. 'foo['
(self, text)
| 1564 | return argMatches |
| 1565 | |
| 1566 | def dict_key_matches(self, text): |
| 1567 | "Match string keys in a dictionary, after e.g. 'foo[' " |
| 1568 | def get_keys(obj): |
| 1569 | # Objects can define their own completions by defining an |
| 1570 | # _ipy_key_completions_() method. |
| 1571 | method = get_real_method(obj, '_ipython_key_completions_') |
| 1572 | if method is not None: |
| 1573 | return method() |
| 1574 | |
| 1575 | # Special case some common in-memory dict-like types |
| 1576 | if isinstance(obj, dict) or\ |
| 1577 | _safe_isinstance(obj, 'pandas', 'DataFrame'): |
| 1578 | try: |
| 1579 | return list(obj.keys()) |
| 1580 | except Exception: |
| 1581 | return [] |
| 1582 | elif _safe_isinstance(obj, 'numpy', 'ndarray') or\ |
| 1583 | _safe_isinstance(obj, 'numpy', 'void'): |
| 1584 | return obj.dtype.names or [] |
| 1585 | return [] |
| 1586 | |
| 1587 | try: |
| 1588 | regexps = self.__dict_key_regexps |
| 1589 | except AttributeError: |
| 1590 | dict_key_re_fmt = r'''(?x) |
| 1591 | ( # match dict-referring expression wrt greedy setting |
| 1592 | %s |
| 1593 | ) |
| 1594 | \[ # open bracket |
| 1595 | \s* # and optional whitespace |
| 1596 | ([uUbB]? # string prefix (r not handled) |
| 1597 | (?: # unclosed string |
| 1598 | '(?:[^']|(?<!\\)\\')* |
| 1599 | | |
| 1600 | "(?:[^"]|(?<!\\)\\")* |
| 1601 | ) |
| 1602 | )? |
| 1603 | $ |
| 1604 | ''' |
| 1605 | regexps = self.__dict_key_regexps = { |
| 1606 | False: re.compile(dict_key_re_fmt % r''' |
| 1607 | # identifiers separated by . |
| 1608 | (?!\d)\w+ |
| 1609 | (?:\.(?!\d)\w+)* |
| 1610 | '''), |
| 1611 | True: re.compile(dict_key_re_fmt % ''' |
| 1612 | .+ |
| 1613 | ''') |
| 1614 | } |
| 1615 | |
| 1616 | match = regexps[self.greedy].search(self.text_until_cursor) |
| 1617 | if match is None: |
| 1618 | return [] |
| 1619 | |
| 1620 | expr, prefix = match.groups() |
| 1621 | try: |
| 1622 | obj = eval(expr, self.namespace) |
| 1623 | except Exception: |
nothing calls this directly
no test coverage detected