June 2019
Beginner to intermediate
770 pages
19h 24m
English
We can refactor the previous load example to iterate through the Blog objects rather than constructing a list of the Blog objects. This allows us to skim through a large CSV file and locate just the relevant Blog and Post rows. This function is a generator that yields each individual Blog instance separately:
def blog_iter(source: TextIO) -> Iterator[Blog]: rdr = csv.reader(source) header = next(rdr) assert header == ["__class__", "title", "date", "title", "rst_text", "tags"] blog = None for r in rdr: if r[0] == "Blog": if blog: yield blog blog = blog_builder(r) elif r[0] == "Post": post = post_builder(r) blog.append(post) if blog: yield blog
This blog_iter() function creates the Blog object and appends ...
Read now
Unlock full access