This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
from cppy.cp_util import *
# 如果有连续的对数据加工操作,而且总是把共同加工数据对象当第一个参数,可以用一个管道框架来封装
# 注意:最后还要调用一次对象call方法,才能执行最后一个函数
class Pipe:
def __init__(self, func, *args, **kwargs):
self.func = func
self.args = args
self.kwargs = kwargs
def __or__(self, other):
data = self.func(*self.args, **self.kwargs)
return Pipe( other.func,data,*other.args,**other.kwargs)
def __call__(self):
return self.func(*self.args, **self.kwargs)
# 模仿管道
pipe = Pipe(extract_file_words,testfilepath) | Pipe(get_frequencies) | Pipe(sort_dict) | Pipe(print_word_freqs, 10)
pipe()