You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

21 lines
784 B

8 months ago
from cppy.cp_util import *
8 months ago
8 months ago
# 如果有连续的对数据加工操作,而且总是把共同加工数据对象当第一个参数,可以用一个管道框架来封装
# 注意最后还要调用一次对象call方法才能执行最后一个函数
8 months ago
class Pipe:
8 months ago
def __init__(self, func, *args, **kwargs):
8 months ago
self.func = func
8 months ago
self.args = args
self.kwargs = kwargs
8 months ago
def __or__(self, other):
8 months ago
data = self.func(*self.args, **self.kwargs)
return Pipe( other.func,data,*other.args,**other.kwargs)
8 months ago
8 months ago
def __call__(self):
return self.func(*self.args, **self.kwargs)
8 months ago
8 months ago
# 模仿管道
pipe = Pipe(extract_file_words,testfilepath) | Pipe(get_frequencies) | Pipe(sort_dict) | Pipe(print_word_freqs, 10)
pipe()