The snippet library.
Small, practical examples. Search by task or narrow things down below.
Parse an ISO calendar date
Python: Use date.fromisoformat and inspect the calendar components.
Python✓ Tested
Parse and format a date
Convert a date string into a datetime object, then format it in a different, human-readable order.
date✓ Tested
Parse quoted CSV from an in-memory string
Python: Use StringIO with csv.reader so quoted commas are handled correctly.
Python✓ Tested
Preconfigure some function arguments
Python: Use partial to produce a callable with selected arguments already bound.
Python✓ Tested
Print a number with two decimal places
Python: Format a small decimal value consistently for display.
Python✓ Tested
Produce compact deterministic JSON
Python: Sort dictionary keys and remove separator whitespace for a reproducible representation.
Python✓ Tested
Raise and catch a domain-specific exception
Python: Give a validation failure its own exception type so callers can handle it narrowly.
Python✓ Tested
Read a file size in bytes
Python: Use stat().st_size to read the logical byte length without loading the file.
Python✓ Tested
Read a JSON file
Load a JSON document from disk into Python objects using an explicitly selected text encoding.
json✓ Tested
Read a small INI configuration
Python: Use ConfigParser typed accessors for configuration values stored as text.
Python✓ Tested
Read a tab-separated table
Python: Select a delimiter explicitly rather than splitting lines manually.
Python✓ Tested
Read a text file
Open a UTF-8 text file and read its contents. A context manager closes the file automatically, even if reading fails.
file✓ Tested