|
1 | 1 | # -*- coding: utf8 -*-
|
2 | 2 | from __future__ import absolute_import, division, print_function
|
3 | 3 |
|
| 4 | +import fnmatch |
4 | 5 | import re
|
5 | 6 | import sys
|
6 | 7 | import warnings
|
@@ -560,43 +561,77 @@ def equals(self, other):
|
560 | 561 | return isinstance(other, Axis) and self.name == other.name and self.iswildcard == other.iswildcard and \
|
561 | 562 | (len(self) == len(other) if self.iswildcard else np.array_equal(self.labels, other.labels))
|
562 | 563 |
|
563 |
| - def matching(self, pattern): |
| 564 | + def matching(self, deprecated=None, pattern=None, regex=None): |
564 | 565 | """
|
565 |
| - Returns a group with all the labels matching the specified pattern (regular expression). |
| 566 | + Returns a group with all the labels matching the specified pattern or regular expression. |
566 | 567 |
|
567 | 568 | Parameters
|
568 | 569 | ----------
|
569 | 570 | pattern : str or Group
|
570 |
| - Regular expression (regex). |
| 571 | + Pattern to match. |
| 572 | + * `?` matches any single character |
| 573 | + * `*` matches any number of characters |
| 574 | + * [seq] matches any character in seq |
| 575 | + * [!seq] matches any character not in seq |
| 576 | +
|
| 577 | + To match any of the special characters above, wrap the character in brackets. For example, `[?]` matches |
| 578 | + the character `?`. |
| 579 | + regex : str or Group |
| 580 | + Regular expression pattern to match. Regular expressions are more powerful than what the simple patterns |
| 581 | + supported by the `pattern` argument but are also more complex to write. |
| 582 | + See `Regular Expression <https://docs.python.org/3/library/re.html>`_ for more details about how to build |
| 583 | + a regular expression pattern. |
571 | 584 |
|
572 | 585 | Returns
|
573 | 586 | -------
|
574 | 587 | LGroup
|
575 | 588 | Group containing all the labels matching the pattern.
|
576 | 589 |
|
577 |
| - Notes |
578 |
| - ----- |
579 |
| - See `Regular Expression <https://docs.python.org/3/library/re.html>`_ |
580 |
| - for more details about how to build a pattern. |
581 |
| -
|
582 | 590 | Examples
|
583 | 591 | --------
|
584 | 592 | >>> people = Axis(['Bruce Wayne', 'Bruce Willis', 'Waldo', 'Arthur Dent', 'Harvey Dent'], 'people')
|
585 | 593 |
|
586 |
| - All labels starting with "W" and ending with "o" are given by |
587 |
| -
|
588 |
| - >>> people.matching('W.*o') |
| 594 | + >>> # All labels starting with "A" and ending with "t" |
| 595 | + >>> people.matching(pattern='A*t') |
| 596 | + people['Arthur Dent'] |
| 597 | + >>> # All labels containing "W" and ending with "s" |
| 598 | + >>> people.matching(pattern='*W*s') |
| 599 | + people['Bruce Willis'] |
| 600 | + >>> # All labels with exactly 5 characters |
| 601 | + >>> people.matching(pattern='?????') |
589 | 602 | people['Waldo']
|
| 603 | + >>> # All labels starting with either "A" or "B" |
| 604 | + >>> people.matching(pattern='[AB]*') |
| 605 | + people['Bruce Wayne', 'Bruce Willis', 'Arthur Dent'] |
590 | 606 |
|
591 |
| - All labels not containing character "a" |
| 607 | + Regular expressions are more powerful but usually harder to write and less readable |
592 | 608 |
|
593 |
| - >>> people.matching('[^a]*$') |
| 609 | + >>> # All labels starting with "W" and ending with "o" |
| 610 | + >>> people.matching(regex='A.*t') |
| 611 | + people['Arthur Dent'] |
| 612 | + >>> # All labels not containing character "a" |
| 613 | + >>> people.matching(regex='^[^a]*$') |
594 | 614 | people['Bruce Willis', 'Arthur Dent']
|
595 | 615 | """
|
596 |
| - if isinstance(pattern, Group): |
597 |
| - pattern = pattern.eval() |
598 |
| - rx = re.compile(pattern) |
599 |
| - return LGroup([v for v in self.labels if rx.match(v)], axis=self) |
| 616 | + if deprecated is not None: |
| 617 | + assert pattern is None and regex is None |
| 618 | + regex = deprecated |
| 619 | + warnings.warn("Axis.matching() first argument will change to `pattern` in a later release. " |
| 620 | + "If your pattern is a regular expression, use Axis.matching(regex='yourpattern')." |
| 621 | + "If your pattern is a 'simple pattern', use Axis.matching(pattern='yourpattern').", |
| 622 | + FutureWarning, stacklevel=2) |
| 623 | + if pattern is not None and regex is not None: |
| 624 | + raise ValueError("Cannot use both `pattern` and `regex` arguments at the same time in Axis.matching()") |
| 625 | + if pattern is None and regex is None: |
| 626 | + raise ValueError("Must provide either `pattern` or `regex` argument in Axis.matching()") |
| 627 | + if isinstance(regex, Group): |
| 628 | + regex = regex.eval() |
| 629 | + if pattern is not None: |
| 630 | + if isinstance(pattern, Group): |
| 631 | + pattern = pattern.eval() |
| 632 | + regex = fnmatch.translate(pattern) |
| 633 | + match = re.compile(regex).match |
| 634 | + return LGroup([v for v in self.labels if match(v)], axis=self) |
600 | 635 |
|
601 | 636 | matches = renamed_to(matching, 'matches')
|
602 | 637 |
|
|
0 commit comments