Code Explainers

Code explainers tagged #generators

python
from collections.abc import Mapping
from typing import Any, Iterator
 
 

Flattening nested config into dotted keys

recursion generators tree-traversal
Intermediate 7 steps
python
import csv
import io
from datetime import datetime
 

Streaming a CSV export in Flask

streaming generators csv
Intermediate 9 steps
php
<?php
 
declare(strict_types=1);
 

Streaming file lines with a PHP generator

generators lazy-evaluation file-io
Intermediate 7 steps
python
from collections import OrderedDict
from typing import Callable, Hashable, Iterable, Iterator, TypeVar
 
T = TypeVar("T")

Two ways to dedupe while keeping order

deduplication generators ordered-data
Intermediate 7 steps
python
import re
from datetime import datetime
from dataclasses import dataclass
 

Parsing access logs with named regex groups

regex named-groups dataclasses
Intermediate 6 steps
python
import gzip
from pathlib import Path
from typing import Iterator
 

Streaming TSV records with Python generators

generators lazy-evaluation file-io
Intermediate 5 steps
javascript
class Stack {
  #items = [];
 
  push(value) {

Building a Stack with private fields

data-structures encapsulation iterators
Intermediate 7 steps
javascript
async function* fetchPages(baseUrl, maxPages) {
  let page = 1;
  while (page <= maxPages) {
    const data = await mockFetch(`${baseUrl}?page=${page}`);

Paginated APIs with async generators

async-iterators generators pagination
Advanced 7 steps