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.

42 lines
1.3 KiB

9 months ago
from collections import Counter
9 months ago
from cppy.cp_util import *
import re
9 months ago
class Pipe:
9 months ago
def __init__(self, func, *args, **kwargs):
# print( self, func, *args, **kwargs )
9 months ago
self.func = func
9 months ago
self.args = args
self.kwargs = kwargs
9 months ago
def __or__(self, other):
9 months ago
return Pipe(lambda x: self.func(x) or other.func(x))
# print(self.func.__name__, other.func.__name__ )
def composed_func():
print( other.func(self.func(self.args, self.kwargs), other.args, other.kwargs) )
return other.func(self.func(self.args, self.kwargs), other.args, other.kwargs)
return Pipe(composed_func)
# def __call__(self, *args, **kwargs):
# print( *args, **kwargs )
# return self.func(*args, **kwargs)
def __call__(self, data):
return self.func(data)
9 months ago
def read_file(filename):
9 months ago
with open(filename, 'r',encoding='utf-8') as f:
9 months ago
return f.read()
def split_words(text):
return re.findall(r'\b\w+\b', text.lower())
def count_words(words):
return Counter(words)
def top_n_words(word_counts, n):
return word_counts.most_common(n)
# 使用管道
9 months ago
pipe = Pipe(read_file) | Pipe(split_words) | Pipe(count_words) | Pipe(top_n_words, 10)
9 months ago
result = pipe(testfilepath)
9 months ago
print(result)