From 9d74a5c184e72c338cac5603daad668b7855c1f2 Mon Sep 17 00:00:00 2001 From: zj3D Date: Wed, 20 Mar 2024 12:02:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=A7=E4=BF=AE=208?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 11 概念认知/尾调用/1 方法bind.py | 29 +++++++++ 11 概念认知/尾调用/2 重载.py | 28 +++++++++ .../尾调用/{tf-26B.py => 3 类方法.py} | 11 +++- 11 概念认知/尾调用/tf-10.py | 27 --------- 11 概念认知/尾调用/tf-25.py | 59 ------------------- 11 概念认知/尾调用/模拟管道.py | 21 ------- 6 files changed, 65 insertions(+), 110 deletions(-) create mode 100644 11 概念认知/尾调用/1 方法bind.py create mode 100644 11 概念认知/尾调用/2 重载.py rename 11 概念认知/尾调用/{tf-26B.py => 3 类方法.py} (75%) delete mode 100644 11 概念认知/尾调用/tf-10.py delete mode 100644 11 概念认知/尾调用/tf-25.py delete mode 100644 11 概念认知/尾调用/模拟管道.py diff --git a/11 概念认知/尾调用/1 方法bind.py b/11 概念认知/尾调用/1 方法bind.py new file mode 100644 index 0000000..95300c9 --- /dev/null +++ b/11 概念认知/尾调用/1 方法bind.py @@ -0,0 +1,29 @@ +from cppy.cp_util import * + +# 如果有连续的对数据加工操作,而且总是把共同加工数据对象当第一个参数,可以用本文件夹方法提升阅读体验 + +# 框架类 +class FunFlow: + def bind(self, func,*args, **kwargs): + try: + self.data = func(self.data,*args, **kwargs) + except: + self.data = func(*args, **kwargs) + return self + + +if __name__ == "__main__": + data = FunFlow()\ + .bind(extract_file_words,testfilepath)\ + .bind(get_frequencies)\ + .bind(sort_dict)\ + .bind(print_word_freqs,10)\ + .data + print(data) + +''' +函数是自由函数,还是正常的函数写法 +使用, + - 列举函数名,首部参数外的其它参数 + - 调用 data 得到最后数据 +''' \ No newline at end of file diff --git a/11 概念认知/尾调用/2 重载.py b/11 概念认知/尾调用/2 重载.py new file mode 100644 index 0000000..6e3ec8a --- /dev/null +++ b/11 概念认知/尾调用/2 重载.py @@ -0,0 +1,28 @@ +from cppy.cp_util import * + +''' +函数是自由函数,还是正常的函数写法 +使用, + - 列举函数名,首部参数外的其它参数 + - 调用 data 得到最后数据 +''' + +class FunPipe: + 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 FunPipe( other.func,_data,*other.args,**other.kwargs) + + @property + def data(self): + return self.func(*self.args, **self.kwargs) + + +# 模仿管道 +pipe = FunPipe(extract_file_words,testfilepath) | FunPipe(get_frequencies) | FunPipe(sort_dict) | FunPipe(print_word_freqs, 10) +pipe.data + diff --git a/11 概念认知/尾调用/tf-26B.py b/11 概念认知/尾调用/3 类方法.py similarity index 75% rename from 11 概念认知/尾调用/tf-26B.py rename to 11 概念认知/尾调用/3 类方法.py index 0ce8433..3c581f1 100644 --- a/11 概念认知/尾调用/tf-26B.py +++ b/11 概念认知/尾调用/3 类方法.py @@ -1,8 +1,5 @@ from cppy.cp_util import * -# 这种连续方法调用,看起来也比较舒服 -# 每一个类方法返回 self ,另外最后一个方法注意是否有返回值 - class Flow: def extract_file_words(self, filepath): self.data = extract_file_words(filepath) @@ -23,3 +20,11 @@ class Flow: # 顺序调用 Flow().extract_file_words(testfilepath).get_frequencies().sort_dict().print_word_freqs(10) + +''' +连续方法调用,看起来比较舒服 +但是需要假设: + - 每一个类方法返回 self + - 类方法默认不写第一个参数 +约束使得不符合使用习惯 +''' \ No newline at end of file diff --git a/11 概念认知/尾调用/tf-10.py b/11 概念认知/尾调用/tf-10.py deleted file mode 100644 index 66fc8b5..0000000 --- a/11 概念认知/尾调用/tf-10.py +++ /dev/null @@ -1,27 +0,0 @@ -from cppy.cp_util import * - - -# 框架类 -class TFFlowcls: - def __init__(self, v): - self._value = v - - def bind(self, func): - self._value = func(self._value) - return self - - def over(self): - print(self._value) - - -def top_freqs(word_freqs): - top10 = "\n".join(f"{word} - {count}" for word, count in word_freqs[:10]) - return top10 - -if __name__ == "__main__": - TFFlowcls( testfilepath )\ - .bind(extract_file_words)\ - .bind(get_frequencies)\ - .bind(sort_dict)\ - .bind(top_freqs)\ - .over() \ No newline at end of file diff --git a/11 概念认知/尾调用/tf-25.py b/11 概念认知/尾调用/tf-25.py deleted file mode 100644 index 11fac4a..0000000 --- a/11 概念认知/尾调用/tf-25.py +++ /dev/null @@ -1,59 +0,0 @@ -from cppy.cp_util import * - -# -# 框架类 -# -class TFFlowcls: - def __init__(self, func): - self._funcs = [func] - - def bind(self, func): - self._funcs.append(func) - return self - - def execute(self): - def call_if_possible(obj): - """Call the object if it's callable, otherwise return it as is.""" - return obj() if hasattr(obj, '__call__') else obj - - # Initialize the value to a no-op lambda function - value = lambda: None - for func in self._funcs: - value = call_if_possible(func(value)) - -# -# 工具函数 -# -def get_input(arg): - def _f(): - return testfilepath - return _f - -def extractwords(path_to_file): - def _f(): - return extract_file_words(path_to_file) - return _f - -def frequencies(word_list): - def _f(): - return get_frequencies(word_list) - return _f - -def sort(word_freq): - def _f(): - return sort_dict(word_freq) - return _f - -def top10_freqs(word_freqs): - def _f(): - return print_word_freqs( word_freqs ) - return _f - - -if __name__ == "__main__": - TFFlowcls(get_input)\ - .bind(extractwords)\ - .bind(frequencies)\ - .bind(sort)\ - .bind(top10_freqs)\ - .execute() \ No newline at end of file diff --git a/11 概念认知/尾调用/模拟管道.py b/11 概念认知/尾调用/模拟管道.py deleted file mode 100644 index acb46d0..0000000 --- a/11 概念认知/尾调用/模拟管道.py +++ /dev/null @@ -1,21 +0,0 @@ -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() \ No newline at end of file