From 3ff9dee64affb9e3dea1af56203e1d80c82e51ca Mon Sep 17 00:00:00 2001 From: Aleksey Zubakov Date: Tue, 13 Dec 2022 02:12:20 +0300 Subject: [PATCH] Add code from lecture about iterators --- 09_python/dict_gen.py | 2 ++ 09_python/enum.py | 9 +++++++++ 09_python/for_loop.py | 36 ++++++++++++++++++++++++++++++++++++ 09_python/func_stream.py | 9 +++++++++ 09_python/gen.py | 17 +++++++++++++++++ 09_python/lzy_load.py | 20 ++++++++++++++++++++ 6 files changed, 93 insertions(+) create mode 100644 09_python/dict_gen.py create mode 100644 09_python/enum.py create mode 100644 09_python/for_loop.py create mode 100644 09_python/func_stream.py create mode 100644 09_python/gen.py create mode 100644 09_python/lzy_load.py diff --git a/09_python/dict_gen.py b/09_python/dict_gen.py new file mode 100644 index 0000000..8f215fe --- /dev/null +++ b/09_python/dict_gen.py @@ -0,0 +1,2 @@ +a = {k: k * 379 for k in range(10)} +print(a) diff --git a/09_python/enum.py b/09_python/enum.py new file mode 100644 index 0000000..0f96087 --- /dev/null +++ b/09_python/enum.py @@ -0,0 +1,9 @@ +els = ["a", "b", "c"] + + +def _enum(els): + return zip(range(len(els)), els) + + +for idx, el in _enum(els): + print(idx, el) diff --git a/09_python/for_loop.py b/09_python/for_loop.py new file mode 100644 index 0000000..c7acdec --- /dev/null +++ b/09_python/for_loop.py @@ -0,0 +1,36 @@ +class IterableStack: + def __init__(self): + self._lst = [1, 2, 3, 4, 5] + + def __iter__(self): + return ReverseIterator(self._lst) + + +class ReverseIterator: + def __init__(self, _lst): + self._lst = _lst + self.position = len(_lst) + + def __next__(self): + self.position -= 1 + if self.position < 0: + raise StopIteration() + + return self._lst[self.position] + + +# container = IterableStack() +# for v in container: +# print(v) + +container = IterableStack() +it_container = iter(container) # container.__iter__() +while True: + try: + v = next(it_container) # it_container.__next__() + + # тело фора + print(v) + # конец тела фора + except StopIteration: + break diff --git a/09_python/func_stream.py b/09_python/func_stream.py new file mode 100644 index 0000000..737de47 --- /dev/null +++ b/09_python/func_stream.py @@ -0,0 +1,9 @@ +def local_gen(): + n = 0 + + def next(): + nonlocal n + n += 1 + return n + + return next diff --git a/09_python/gen.py b/09_python/gen.py new file mode 100644 index 0000000..e9f08f2 --- /dev/null +++ b/09_python/gen.py @@ -0,0 +1,17 @@ +def _range(start, stop): + assert start < stop + + def inside_gen(): + nonlocal start + while start < stop: + print("Going to return: ", start) + yield start + start += 1 + + print("here") + + return inside_gen() + + +# for el in _range(0, 10): +# print(el) diff --git a/09_python/lzy_load.py b/09_python/lzy_load.py new file mode 100644 index 0000000..334918e --- /dev/null +++ b/09_python/lzy_load.py @@ -0,0 +1,20 @@ +from typing import List + + +def load_files(paths: List[str]): + for path in paths: + with open(path) as f: + yield f.readlines() + + +paths = [ + "dict_gen.py", + "enum.py", + "for_loop.py", + "gen.py", + "lzy_load.py", +] + +for cont in load_files(paths): + # code working with content + print(cont)