you need to modify dirs in place in order to avoid recursive traversal of the removed directories. Use:
dirs[:] = [d for d in dirs if pred_dir(path.join(base, d))]
this will remove the need to check _ignore_dirs_predicate(base) (and remove the NameError caused by the use of _ignore_dirs_predicate instead of pred_dir)
You should also rewrite ISA_PY to use str.endswith()
Yeah. What's wrong with the original interface? Apply your predicates to the raw output to get the best results. Beyond an os.path.join(base, name), there's not much room to improve.
def walk_setup_py(src):
"""Iterate through the setup.py files
"""
return walk_full_path_files(src, pred_file=ISA_SETUP_PY)
# TODO: this should be the default behaviour
def walk_full_path_files(src, pred_dir=_default_dir_predicate,
pred_file=_default_file_predicate):
"""Iterates on the full path given a walker function
"""
for root, _, files in walk(src):
for f in files:
fpath = path.join(root, f)
if pred_dir(root) and pred_file(f):
yield fpath
比我以前做的要好得多..
Ok actually I realized that I was doing it all wrong (as I suspected). The problem is that most of the time I don't know if I want to drop a path or not until I have the complete path.
So for example, given:
r1:
-d1
+ test: file1, file2
r2:
- d2
I can only know when I have the full path "r1/d1/test/file1" what I want to do with that file, so trying to filter before it makes life a lot more complicated.
This is how it becomes now
def walk_setup_py(src):
"""Iterate through the setup.py files
"""
return walk_full_path_files(src, pred_file=ISA_SETUP_PY)
# TODO: this should be the default behaviour
def walk_full_path_files(src, pred_dir=_default_dir_predicate,
pred_file=_default_file_predicate):
"""Iterates on the full path given a walker function
"""
for root, _, files in walk(src):
for f in files:
fpath = path.join(root, f)
if pred_dir(root) and pred_file(f):
yield fpath
发布评论
评论(4)
您需要就地修改目录以避免递归遍历已删除的目录。使用:
这将消除检查
_ignore_dirs_predicate(base)
的需要(并消除由于使用_ignore_dirs_predicate
而不是导致的
)NameError
pred_dir您还应该重写
ISA_PY
以使用str.endswith()
you need to modify dirs in place in order to avoid recursive traversal of the removed directories. Use:
this will remove the need to check
_ignore_dirs_predicate(base)
(and remove theNameError
caused by the use of_ignore_dirs_predicate
instead ofpred_dir
)You should also rewrite
ISA_PY
to usestr.endswith()
是的。原来的界面有什么问题吗?将谓词应用于原始输出以获得最佳结果。除了 os.path.join(base, name) 之外,没有太多改进的空间。
Yeah. What's wrong with the original interface? Apply your predicates to the raw output to get the best results. Beyond an
os.path.join(base, name
), there's not much room to improve.查看路径 Python 模块。
原始链接似乎已死: http://jorendorff.com/articles/python/path/
Check out the path Python module.
The original link seems dead: http://jorendorff.com/articles/python/path/
好吧,实际上我意识到我做错了(正如我怀疑的那样)。
问题是,大多数时候我不知道是否要删除路径,直到获得完整路径。
例如,给定:
我只能知道当我拥有完整路径“r1/d1/test/file1”时我想对该文件做什么,因此尝试在它使生活变得更加复杂之前进行过滤。
这就是现在的样子,
比我以前做的要好得多..
Ok actually I realized that I was doing it all wrong (as I suspected).
The problem is that most of the time I don't know if I want to drop a path or not until I have the complete path.
So for example, given:
I can only know when I have the full path "r1/d1/test/file1" what I want to do with that file, so trying to filter before it makes life a lot more complicated.
This is how it becomes now
Which is way nicer than what I was doing before..