PurePath.match(pattern) 和 Path.glob(pattern) 之间的差异
Disparity between PurePath.match(pattern) and Path.glob(pattern)
Match this path against the provided glob-style pattern. Return True if matching is successful, False otherwise
Glob the given pattern in the directory represented by this path, yielding all matching files (of any kind):
问题一
那为什么下面的断言会失败呢?
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?
问题2
我们如何修改 PurePath.match
模式,以确保断言不会失败。
NB 递归模式无法Path.match
基本路径下由Path.glob
匹配并返回的文件。这似乎是两个 API 之间可观察到的差异。
QnA
问:你能举例说明在query_files中出现但在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')]
Path.glob
和 Path.match
由具有不同行为的不同 glob 实现支持。特别是 Path.match
不支持 **
.
这很奇怪而且不一致,可能不是最初打算的,但我不知道他们是否会改变它。
Match this path against the provided glob-style pattern. Return True if matching is successful, False otherwise
Glob the given pattern in the directory represented by this path, yielding all matching files (of any kind):
问题一
那为什么下面的断言会失败呢?
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?
问题2
我们如何修改 PurePath.match
模式,以确保断言不会失败。
NB 递归模式无法Path.match
基本路径下由Path.glob
匹配并返回的文件。这似乎是两个 API 之间可观察到的差异。
QnA
问:你能举例说明在query_files中出现但在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')]
Path.glob
和 Path.match
由具有不同行为的不同 glob 实现支持。特别是 Path.match
不支持 **
.
这很奇怪而且不一致,可能不是最初打算的,但我不知道他们是否会改变它。