|
|
@ -1,29 +1,50 @@
|
|
|
|
from cppy.cp_util import *
|
|
|
|
from cppy.cp_util import *
|
|
|
|
|
|
|
|
|
|
|
|
class Flow:
|
|
|
|
# 装饰器改写类
|
|
|
|
def extract_file_words(self, filepath):
|
|
|
|
# - 找到以f_开头的方法
|
|
|
|
self.data = extract_file_words(filepath)
|
|
|
|
# - 将方法函数的返回值赋值给对象的data属性
|
|
|
|
return self
|
|
|
|
# - 返回对象自身
|
|
|
|
|
|
|
|
def return_self_decorator(cls):
|
|
|
|
|
|
|
|
def return_self(func):
|
|
|
|
|
|
|
|
# 定义一个闭包函数,用于接收参数
|
|
|
|
|
|
|
|
def wrapper(self, *args, **kwargs):
|
|
|
|
|
|
|
|
self.data = func(self, *args, **kwargs)
|
|
|
|
|
|
|
|
return self # 返回类自身
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
|
|
|
def get_frequencies(self):
|
|
|
|
for name, method in cls.__dict__.items():
|
|
|
|
self.data = get_frequencies(self.data)
|
|
|
|
# 判断属性是否可调用,且属性名以f_开头
|
|
|
|
return self
|
|
|
|
if callable(method) and name.startswith('f_'):
|
|
|
|
|
|
|
|
# 为类改写属性,将封装后的函数赋值
|
|
|
|
|
|
|
|
setattr(cls, name, return_self(method))
|
|
|
|
|
|
|
|
return cls
|
|
|
|
|
|
|
|
|
|
|
|
def sort_dict(self):
|
|
|
|
@return_self_decorator
|
|
|
|
self.data = sort_dict(self.data)
|
|
|
|
class Flow():
|
|
|
|
return self
|
|
|
|
def test(self):
|
|
|
|
|
|
|
|
return 'test'
|
|
|
|
|
|
|
|
|
|
|
|
def print_word_freqs(self, n):
|
|
|
|
def f_extract_file_words(self, filepath):
|
|
|
|
|
|
|
|
return extract_file_words(filepath)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def f_get_frequencies(self):
|
|
|
|
|
|
|
|
return get_frequencies(self.data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def f_sort_dict(self):
|
|
|
|
|
|
|
|
return sort_dict(self.data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def f_print_word_freqs(self, n):
|
|
|
|
print_word_freqs(self.data, n)
|
|
|
|
print_word_freqs(self.data, n)
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 顺序调用
|
|
|
|
# 顺序调用
|
|
|
|
Flow().extract_file_words(testfilepath).get_frequencies().sort_dict().print_word_freqs(10)
|
|
|
|
Flow().f_extract_file_words(testfilepath).f_get_frequencies().f_sort_dict().f_print_word_freqs(10)
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
连续方法调用,看起来比较舒服
|
|
|
|
改写后,参与 function flow 功能的方法
|
|
|
|
但是需要假设:
|
|
|
|
- 需要以 'f_' 开头
|
|
|
|
- 每一个类方法返回 self :否则,没法连续
|
|
|
|
|
|
|
|
- 类方法默认不写第一个参数,数据都在 .data 里面
|
|
|
|
- 类方法默认不写第一个参数,数据都在 .data 里面
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
仍旧需要特殊的方法写法
|
|
|
|
|
|
|
|
所以,还是 1,2种方法比较自然
|
|
|
|
'''
|
|
|
|
'''
|