|
7 | 7 | from numpy.lib.recfunctions import append_fields |
8 | 8 | from pandas import DataFrame, RangeIndex |
9 | 9 | from root_numpy import root2array, list_trees |
10 | | -from fnmatch import fnmatch |
| 10 | +import fnmatch |
11 | 11 | from root_numpy import list_branches |
12 | 12 | from root_numpy.extern.six import string_types |
13 | 13 | import itertools |
@@ -59,17 +59,24 @@ def get_nonscalar_columns(array): |
59 | 59 |
|
60 | 60 |
|
61 | 61 | def get_matching_variables(branches, patterns, fail=True): |
62 | | - selected = [] |
63 | | - |
64 | | - for p in patterns: |
| 62 | + # Convert branches to a set to make x "in branches" O(1) on average |
| 63 | + branches = set(branches) |
| 64 | + patterns = set(patterns) |
| 65 | + # Find any trivial matches |
| 66 | + selected = list(branches.intersection(patterns)) |
| 67 | + # Any matches that weren't trivial need to be looped over... |
| 68 | + for pattern in patterns.difference(selected): |
65 | 69 | found = False |
66 | | - for b in branches: |
67 | | - if fnmatch(b, p): |
| 70 | + # Avoid using fnmatch if the pattern if possible |
| 71 | + if re.findall(r'(\*)|(\?)|(\[.*\])|(\[\!.*\])', pattern): |
| 72 | + for match in fnmatch.filter(branches, pattern): |
68 | 73 | found = True |
69 | | - if fnmatch(b, p) and b not in selected: |
70 | | - selected.append(b) |
| 74 | + if match not in selected: |
| 75 | + selected.append(match) |
| 76 | + elif pattern in branches: |
| 77 | + raise NotImplementedError('I think this is impossible?') |
71 | 78 | if not found and fail: |
72 | | - raise ValueError("Pattern '{}' didn't match any branch".format(p)) |
| 79 | + raise ValueError("Pattern '{}' didn't match any branch".format(pattern)) |
73 | 80 | return selected |
74 | 81 |
|
75 | 82 |
|
|
0 commit comments