From 1ab32a1c416d900f4ee1611b3fa6aa0bf4f78eb0 Mon Sep 17 00:00:00 2001 From: "wang.xiaolong" Date: Wed, 24 Mar 2021 13:04:31 +0800 Subject: [PATCH 1/3] yield --- basic/do_yield/fab.py | 22 +++++++++++++++++++++ basic/do_yield/fab2.py | 37 ++++++++++++++++++++++++++++++++++++ basic/do_yield/fab3.py | 28 +++++++++++++++++++++++++++ basic/do_yield/fab4.py | 31 ++++++++++++++++++++++++++++++ basic/do_yield/fab5_yield.py | 24 +++++++++++++++++++++++ basic/do_yield/iter_1.py | 26 +++++++++++++++++++++++++ samples/advance/do_yield.py | 5 ++++- 7 files changed, 172 insertions(+), 1 deletion(-) create mode 100644 basic/do_yield/fab.py create mode 100644 basic/do_yield/fab2.py create mode 100644 basic/do_yield/fab3.py create mode 100644 basic/do_yield/fab4.py create mode 100644 basic/do_yield/fab5_yield.py create mode 100644 basic/do_yield/iter_1.py diff --git a/basic/do_yield/fab.py b/basic/do_yield/fab.py new file mode 100644 index 00000000..0a975529 --- /dev/null +++ b/basic/do_yield/fab.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +# 斐波那契数列 +# F(0)=0,F(1)=1, F(n)=F(n - 1)+F(n - 2) + + +def fab_data(n): + if n <= 0: + return 0 + elif n == 1: + return 1 + return fab_data(n-1) + fab_data(n-2) + + +def test(): + + for i in range(10): + print(fab_data(i)), + + +test() diff --git a/basic/do_yield/fab2.py b/basic/do_yield/fab2.py new file mode 100644 index 00000000..7cc87ef2 --- /dev/null +++ b/basic/do_yield/fab2.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +# 斐波那契数列 +# F(0)=0,F(1)=1, F(n)=F(n - 1)+F(n - 2) + + +class FabIterator: + def __init__(self, fb): + self.a = fb.a + self.b = fb.b + + def __next__(self): + r = self.a + self.b + self.a = self.b + self.b = r + return r + + +class Fab: + def __init__(self): + self.a = 0 + self.b = 1 + + def __iter__(self): + return FabIterator(self) + + +fab = Fab() +for i in fab: + if i > 100: + break + print(i) + + + + diff --git a/basic/do_yield/fab3.py b/basic/do_yield/fab3.py new file mode 100644 index 00000000..13e862a2 --- /dev/null +++ b/basic/do_yield/fab3.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +# 斐波那契数列 +# F(0)=0,F(1)=1, F(n)=F(n - 1)+F(n - 2) + +class Fab(object): + def __init__(self): + self.a = 0 + self.b = 1 + + def __iter__(self): + return self + + def __next__(self): + r = self.a + self.b + self.a = self.b + self.b = r + return r + + +fab = Fab() +for i in fab: + print(i) + + + + diff --git a/basic/do_yield/fab4.py b/basic/do_yield/fab4.py new file mode 100644 index 00000000..c34b12d6 --- /dev/null +++ b/basic/do_yield/fab4.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +# 斐波那契数列 +# F(0)=0,F(1)=1, F(n)=F(n - 1)+F(n - 2) + +class Fab(object): + def __init__(self, mx): + self.mx = mx + self.a = 0 + self.b = 1 + + def __iter__(self): + return self + + def __next__(self): + r = self.a + self.b + if r < self.mx: + self.a = self.b + self.b = r + return r + raise StopIteration + + +fab = Fab(100) +for i in fab: + print(i) + + + + diff --git a/basic/do_yield/fab5_yield.py b/basic/do_yield/fab5_yield.py new file mode 100644 index 00000000..09abf4a9 --- /dev/null +++ b/basic/do_yield/fab5_yield.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +# 斐波那契数列 +# F(0)=0,F(1)=1, F(n)=F(n - 1)+F(n - 2) + +def fab_with_yield(mx): + a, b, n = 0, 1, 1 + while n < mx: + yield b + a, b = b, a + b + n += 1 + + +def test(): + for i in fab_with_yield(10): + print(i) + + +test() + + + + diff --git a/basic/do_yield/iter_1.py b/basic/do_yield/iter_1.py new file mode 100644 index 00000000..702503a8 --- /dev/null +++ b/basic/do_yield/iter_1.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + + +def test1(): + for x in [1, 2, 3, 4, 5]: + print(x) + + +def test2(): + # 首先获得Iterator对象: + it = iter([1, 2, 3, 4, 5]) + # 循环: + while True: + try: + # 获得下一个值: + x = next(it) + print(x) + except StopIteration: + # 遇到StopIteration就退出循环 + break + + +test1() + +test2() diff --git a/samples/advance/do_yield.py b/samples/advance/do_yield.py index 8615284a..cd542306 100755 --- a/samples/advance/do_yield.py +++ b/samples/advance/do_yield.py @@ -6,10 +6,12 @@ def each_ascii(s): yield ord(ch) return '%s chars' % len(s) + def yield_from(s): r = yield from each_ascii(s) print(r) + def main(): for x in each_ascii('abc'): print(x) # => 'a', 'b', 'c' @@ -26,4 +28,5 @@ def main(): for ch in yield_from('hello'): pass -main() + +# main() From a8c711cdf80077d03a929bebc73c9cb25ce0dda4 Mon Sep 17 00:00:00 2001 From: "wang.xiaolong" Date: Wed, 24 Mar 2021 13:07:47 +0800 Subject: [PATCH 2/3] yield: rename --- basic/do_yield/fab5_yield.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/basic/do_yield/fab5_yield.py b/basic/do_yield/fab5_yield.py index 09abf4a9..96b32fdf 100644 --- a/basic/do_yield/fab5_yield.py +++ b/basic/do_yield/fab5_yield.py @@ -5,15 +5,15 @@ # F(0)=0,F(1)=1, F(n)=F(n - 1)+F(n - 2) def fab_with_yield(mx): - a, b, n = 0, 1, 1 + x, y, n = 0, 1, 1 while n < mx: - yield b - a, b = b, a + b + yield y + x, y = y, x + y n += 1 def test(): - for i in fab_with_yield(10): + for i in fab_with_yield(5): print(i) From 20aab2a951bd2e206d1f9695b46e6b3ae79d454a Mon Sep 17 00:00:00 2001 From: "wang.xiaolong" Date: Wed, 24 Mar 2021 13:18:34 +0800 Subject: [PATCH 3/3] yield: read file --- basic/do_yield/fab5_yield.py | 5 +++++ basic/do_yield/yield_demo1.py | 14 ++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 basic/do_yield/yield_demo1.py diff --git a/basic/do_yield/fab5_yield.py b/basic/do_yield/fab5_yield.py index 96b32fdf..af84d286 100644 --- a/basic/do_yield/fab5_yield.py +++ b/basic/do_yield/fab5_yield.py @@ -20,5 +20,10 @@ def test(): test() +from inspect import isgeneratorfunction +print(isgeneratorfunction(fab_with_yield)) +from collections import Iterable +print(isinstance(fab_with_yield, Iterable)) +print(isinstance(fab_with_yield(5), Iterable)) \ No newline at end of file diff --git a/basic/do_yield/yield_demo1.py b/basic/do_yield/yield_demo1.py new file mode 100644 index 00000000..91ee3794 --- /dev/null +++ b/basic/do_yield/yield_demo1.py @@ -0,0 +1,14 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +# 文件太大,会导致不可预测的内存 + +def read_file(fpath): + BLOCK_SIZE = 1024 + with open(fpath, 'rb') as f: + while True: + block = f.read(BLOCK_SIZE) + if block: + yield block + else: + return