Libreria di snippet

Snippet pronti e testati per Python, Flask, JavaScript, SQL, HTML, CSS e Bash. Aprili nel playground.

9 snippet

python

Read a JSON file with clear errors

Load a JSON file and get a readable message when it is missing or malformed.

beginner #json#files#errors
python

Retry a flaky call with exponential backoff

A decorator that retries a function on chosen exceptions, waiting longer after each failure.

intermediate #retry#decorators#network
python

Cache expensive results

Memoize a pure function with functools.lru_cache.

beginner #performance#functools#recursion
python

Generate a secure random token

Use the secrets module (never random) for passwords, tokens and reset links.

beginner #security#secrets#auth
python

Group items by a key

Turn a flat list into a dict of lists with collections.defaultdict.

beginner #collections#dict#data
python

Read and write CSV files

Read rows as dicts and write a filtered copy with csv.DictWriter.

beginner #csv#files#data
python

Slugify a title

Turn any title into a lowercase, URL-safe slug.

beginner #strings#regex#urls
python

Time a block of code

A tiny context manager that prints how long a block took.

intermediate #performance#context-manager
python

Validate data with dataclasses

Use __post_init__ to reject bad values as soon as an object is created.

intermediate #dataclasses#validation#types