Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider replacing regex for matching * with fnmatch #542

Closed
phackstock opened this issue Jun 1, 2021 · 4 comments
Closed

Consider replacing regex for matching * with fnmatch #542

phackstock opened this issue Jun 1, 2021 · 4 comments

Comments

@phackstock
Copy link
Contributor

Fnmatch is a python standard library tool which provides unix style wildcard pattern matching. Not as powerful as regex of course but for just matching with * it should be sufficient.

Example for finding all .csv files in a folder:

import fnmatch
import os

for file in os.listdir('.'):
    if fnmatch.fnmatch(file, '*.csv'):
        print(file)

This could replace using regex per default for the pyam filter function.

@danielhuppmann
Copy link
Member

Thanks for the suggestion @phackstock - this could be a nice way to get rid of the utils function _escape_regexp() within pattern_match() (for the non-regexp use case) , which seems to be doing pretty much the same thing...

def _escape_regexp(s):

@danielhuppmann
Copy link
Member

This is used as a trial for filtering the index-query to the IIASA Database API, see #697

@danielhuppmann
Copy link
Member

I reverted the previous PR in #699, because fnmatch does not natively support filtering lists, using the pyam function pattern_match() directly was an easier fix. fnmatch might still be a useful option within pattern_match().

@danielhuppmann
Copy link
Member

Looked at this again as a possible improvement to #849, but this package also has issues with square brackets and would require some customization... See https://docs.python.org/3/library/fnmatch.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants