Code Explainers

Code explainers tagged #generators

python
import random
from typing import Iterator, List
 
 

How reservoir sampling picks k items

reservoir-sampling streaming randomness
Intermediate 5 steps
python
from typing import Iterable
 
 
def jaccard_similarity(tags_a: Iterable[str], tags_b: Iterable[str]) -> float:

Ranking tag sets by Jaccard similarity

set-operations similarity-metric generators
Intermediate 5 steps
python
import os
import re
from flask import Blueprint, request, Response, abort
 

HTTP range requests for video streaming in Flask

http-range-requests streaming generators
Intermediate 8 steps
python
import heapq
 
 
class MovingMedian:

Running median with two heaps

heaps streaming invariants
Advanced 8 steps
typescript
interface RunningStats {
  count: number;
  total: number;
  average: number;

Streaming running averages in TypeScript

generators streaming incremental-aggregation
Intermediate 6 steps
python
from pathlib import Path
from typing import Iterator
 
 

Filtering files with pathlib.glob

generators filesystem filtering
Intermediate 6 steps
python
import heapq
from collections import Counter
from typing import Iterable, Hashable
 

Finding the top-N items in a stream

heaps counting generators
Intermediate 5 steps
python
import heapq
from datetime import datetime
from pathlib import Path
from typing import Iterator, NamedTuple

Merging sorted log files with heapq

generators heapq lazy-evaluation
Intermediate 6 steps
python
import datetime
from dataclasses import dataclass
 
 

Parsing fixed-width records in Python

parsing generators dataclasses
Intermediate 8 steps
python
from itertools import product
from dataclasses import dataclass
from decimal import Decimal
 

Generating product variants with itertools.product

cartesian-product dataclass decimal
Intermediate 7 steps
php
<?php
 
declare(strict_types=1);
 

Streaming a fixed-width file into typed objects

generators value-objects file-parsing
Intermediate 8 steps
python
from contextlib import contextmanager
from typing import Iterator
 
import psycopg2

Streaming Postgres rows with a server-side cursor

generators context-managers database-streaming
Intermediate 7 steps
python
import json
import time
import queue
 

Server-Sent Events streaming in Flask

server-sent-events streaming pub-sub
Advanced 9 steps
python
import re
from collections import Counter
from pathlib import Path
 

Ranking the top words in a PDF

text-processing regex counting
Intermediate 6 steps
typescript
function* map<T, U>(source: Iterable<T>, fn: (value: T, index: number) => U): Generator<U> {
  let index = 0;
  for (const value of source) {
    yield fn(value, index++);

Building a lazy iterator pipeline in TypeScript

generators lazy-evaluation iterators
Intermediate 7 steps
python
import csv
 
from django.http import StreamingHttpResponse
from django.utils.timezone import now

Streaming a CSV export in Django

streaming csv-export generators
Intermediate 7 steps
python
import ijson
from decimal import Decimal
 
 

Streaming JSON aggregation with ijson

streaming json-parsing generators
Intermediate 7 steps
python
import time
from contextlib import contextmanager
 
 

A reusable timing context manager in Python

context-manager timing generators
Intermediate 5 steps