【发布时间】:2016-05-07 14:34:45
【问题描述】:
我目前正在学习 Scala,并正在寻找一个优雅的解决方案来解决一个可以通过使用协程轻松解决的问题。
由于在 Scala 中默认情况下不启用协程,我认为它们至少不是被广泛接受的最佳实践,因此我想在不使用它们的情况下编写我的代码。
协同例程/延续是最佳实践的令人信服的论点将是另一种可接受的答案。
generate_all_matching_paths 函数
我想编写一个函数来搜索基目录中的文件。 匹配和下降标准应由具有“PathMatcher”特征的类的实例提供。 (至少我认为这是 Scala 的方式)
PathMatcher 可用于确定 fs_item_path 是否匹配,并确定搜索是否应进入目录(如果 fs_item_path 是目录的路径)。
我现在采用的 Python 实现方法只是为了表明我想到的功能。
我想用“Scala 方式”编写这段代码。
我的目标是找到具有以下特点的解决方案:
- 可读
- 在合适的地方使用 Scala 习语
- 在搜索期间(而不是之后)返回匹配的路径
- 适用于非常大的文件集合(但不是非常深的嵌套) - 即目录中的数千个文件;总共数百万个文件
我假设解决方案将涉及延迟评估流,但我无法以有效的方式组装流。
我还读到如果使用不当,惰性流可以保留“旧值”的副本。我所追求的解决方案不会这样做。
参数
base_abs_path
开始搜索的目录的绝对路径
rel_ancestor_dir_list
目录名称列表,指示我们已下降到 base_abs_path 子目录的程度
rel_path_matcher
具有 PathMatcher 特征的类的实例。
在下面的示例中,我使用了正则表达式实现,但我不想将使用限制为正则表达式。
Python 示例
这是一个完整的工作 Python 程序(使用 Python 3.4 测试),其中包括 Python 版本的“generate_all_matching_paths”。
程序将在“d:\Projects”中搜索以“json”结尾的文件系统路径,分析文件使用的缩进,然后打印出结果。
如果路径包含子字符串“python_portable”,则搜索不会进入该目录。
import os
import re
import codecs
#
# this is the bespoke function I want to port to Scala
#
def generate_all_matching_paths(
base_dir_abs_path,
rel_ancestor_dir_list,
rel_path_matcher
):
rooted_ancestor_dir_list = [base_dir_abs_path] + rel_ancestor_dir_list
current_dir_abs_path = os.path.join(*rooted_ancestor_dir_list)
dir_listing = os.listdir(current_dir_abs_path)
for fs_item_name in dir_listing:
fs_item_abs_path = os.path.join(
current_dir_abs_path,
fs_item_name
)
fs_item_rel_ancestor_list = rel_ancestor_dir_list + [fs_item_name]
fs_item_rel_path = os.path.join(
*fs_item_rel_ancestor_list
)
result = rel_path_matcher.match(fs_item_rel_path)
if result.is_match:
yield fs_item_abs_path
if result.do_descend and os.path.isdir(fs_item_abs_path):
child_ancestor_dir_list = rel_ancestor_dir_list + [fs_item_name]
for r in generate_all_matching_paths(
base_dir_abs_path,
child_ancestor_dir_list,
rel_path_matcher
):
yield r
#
# all following code is only a context giving example of how generate_all_matching_paths might be used
#
class MyMatchResult:
def __init__(
self,
is_match,
do_descend
):
self.is_match = is_match
self.do_descend = do_descend
# in Scala this should implement the PathMatcher trait
class MyMatcher:
def __init__(
self,
rel_path_regex,
abort_dir_descend_regex_list
):
self.rel_path_regex = rel_path_regex
self.abort_dir_descend_regex_list = abort_dir_descend_regex_list
def match(self, path):
rel_path_match = self.rel_path_regex.match(path)
is_match = rel_path_match is not None
do_descend = True
for abort_dir_descend_regex in self.abort_dir_descend_regex_list:
abort_match = abort_dir_descend_regex.match(path)
if abort_match:
do_descend = False
break
r = MyMatchResult(is_match, do_descend)
return r
def leading_whitespace(file_path):
b_leading_spaces = False
b_leading_tabs = False
with codecs.open(file_path, "r", "utf-8") as f:
for line in f:
for c in line:
if c == '\t':
b_leading_tabs = True
elif c == ' ':
b_leading_spaces = True
else:
break
if b_leading_tabs and b_leading_spaces:
break
return b_leading_spaces, b_leading_tabs
def print_paths(path_list):
for path in path_list:
print(path)
def main():
leading_spaces_file_path_list = []
leading_tabs_file_path_list = []
leading_mixed_file_path_list = []
leading_none_file_path_list = []
base_dir_abs_path = r'd:\Projects'
rel_path_regex = re.compile('.*json$')
abort_dir_descend_regex_list = [
re.compile('^.*python_portable.*$')
]
rel_patch_matcher = MyMatcher(rel_path_regex, abort_dir_descend_regex_list)
ancestor_dir_list = []
for fs_item_path in generate_all_matching_paths(
base_dir_abs_path,
ancestor_dir_list,
rel_patch_matcher
):
if os.path.isfile(fs_item_path):
b_leading_spaces, b_leading_tabs = leading_whitespace(fs_item_path)
if b_leading_spaces and b_leading_tabs:
leading_mixed_file_path_list.append(fs_item_path)
elif b_leading_spaces:
leading_spaces_file_path_list.append(fs_item_path)
elif b_leading_tabs:
leading_tabs_file_path_list.append(fs_item_path)
else:
leading_none_file_path_list.append(fs_item_path)
print('space indentation:')
print_paths(leading_spaces_file_path_list)
print('tab indentation:')
print_paths(leading_tabs_file_path_list)
print('mixed indentation:')
print_paths(leading_mixed_file_path_list)
print('no indentation:')
print_paths(leading_none_file_path_list)
print('space: {}'.format(len(leading_spaces_file_path_list)))
print('tab: {}'.format(len(leading_tabs_file_path_list)))
print('mixed: {}'.format(len(leading_mixed_file_path_list)))
print('none: {}'.format(len(leading_none_file_path_list)))
if __name__ == '__main__':
main()
【问题讨论】:
-
有很多代码需要我们阅读和理解。能否归结为要点,更清楚地说明您想问什么?
-
我试图在文本中概述要点。代码中唯一重要的函数是“generate_all_matching_paths”。其余代码如果用于上下文。我将添加一些 cmets 并更改顺序:)
-
有点相关 - 有一个Scala Coroutines library available。
标签: python scala yield coroutine lazy-sequences