Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disparity between PurePath.match(pattern) and Path.glob(pattern)

If PurePath.match(pattern) does

Match this path against the provided glob-style pattern. Return True if matching is successful, False otherwise

and Path.glob(pattern) will

Glob the given pattern in the directory represented by this path, yielding all matching files (of any kind):

Question-1

Then why the below assertion fails?

query_files = Path('../').glob("sql-queries/**/*.sql")
filtered_files = [fname for fname in query_files if fname.match("sql-queries/**/*.sql")]
assert query_files == filtered_files # This assertion fails?

Question-2

How can we modify the PurePath.match pattern, to ensure the assertion does not fail.

NB The recursive pattern fails to Path.match the files under base path which are matched and returned by Path.glob. This seems to be the observable discrepancies between the two APIs.

QnA

Q: Can you give an example of a specific path that appears in query_files but not filtered_files?

A:

query_files = <class 'list'>: [
                    PosixPath('../sql-queries/combined_gv.sql'), 
                    PosixPath('../sql-queries/gv_with_merchant.sql'), 
                    PosixPath('../sql-queries/merchant_dimension.sql'), 
                    PosixPath('../sql-queries/gen_test_data/combined_gv.sql'),
                    PosixPath('../sql-queries/gen_test_data/merchant_dimension.sql')]
filtered_files = <class 'list'>: [
                    PosixPath('../sql-queries/gen_test_data/combined_gv.sql'),
                    PosixPath('../sql-queries/gen_test_data/merchant_dimension.sql')]
like image 694
Abhijit Avatar asked Aug 17 '26 07:08

Abhijit


1 Answers

Path.glob and Path.match are backed by different glob implementations with different behavior. Particularly, Path.match doesn't support **.

It's weird and inconsistent and probably not originally intended, but I don't know if they'll change it.

like image 150
user2357112 supports Monica Avatar answered Aug 21 '26 19:08

user2357112 supports Monica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!