Fixed UTC offsets and datetime formatting with `strftime` and `isoformat`
Attach a fixed UTC offset without changing the wall clock reading, then render the same datetime as both a custom string and an ISO 8601 string.
CKTIS / Python
Programming syntax, operators and concepts. Find an explanation, explore an example and return to learning.
Attach a fixed UTC offset without changing the wall clock reading, then render the same datetime as both a custom string and an ISO 8601 string.
Use `addHandler()` to attach a handler to a specific logger, and set `propagate = False` to keep its records from reaching handlers on ancestor loggers, including the root logger.
Choose a method on a named logger based on the severity of an event, and see how logger and handler settings determine which messages appear.
Look up a logger relative to its parent’s name and send its records to the parent’s handler without copying that handler onto the child.
In Python 3.12, `logger.handlers` shows only the handlers attached directly to a logger. Read the list to inspect it; use `addHandler()` and `removeHandler()` to change it.
Set a named logger to `logging.INFO` to drop `DEBUG` while allowing `INFO` and higher levels past the logger’s threshold. The handler still controls what reaches its output.
Call `cache_info()` on an `lru_cache`-decorated function to read its hit and miss counts, entry limit, and current number of cached entries.
`__next__` returns one item and advances the iterator. Once the iterator is exhausted, the method raises `StopIteration` on every further call.
`propagate` controls whether a log record also reaches ancestor loggers' handlers. Setting it to `False` stops forwarding upward without disabling local handlers.
Use `property.setter` to make ordinary assignment to an instance attribute call a function that can validate the new value before storing it.
Use the `r` prefix to keep backslashes in a string value. This is useful for regular expressions: Python preserves the backslashes, and `re` interprets them.
The `r` prefix preserves backslashes in a `str` literal. This makes regular-expression patterns easier to read without changing how `re` interprets them.
Use this compiled regular expression method to check whether the pattern matches the entire string or the entire range selected by `pos` and `endpos`.
Use a compiled regular expression to find the first match in a string or within a range set by start and end indexes.
Call `sub()` on a compiled regular expression to replace matches with a template or a function’s result. Use `count` to limit the number of replacements.
The `-` operator builds a new set from elements found in the left set but not the right one. With integers, the same operator performs arithmetic subtraction.
Use `^` to keep elements that belong to exactly one of two sets. The same operator performs bitwise XOR when both operands are integers.
Use `|` to combine sets without changing either operand. With two integers, the same operator performs bitwise OR instead.
Use `&` to find the elements two sets share. The same operator performs bitwise AND when both operands are integers.
Attach a known fixed UTC offset without changing the clock time, then display the same datetime using a custom format and an ISO 8601 representation.
Import `sys` to pass the current standard output stream explicitly to `print()`, or choose another text stream for just one call.
Build a suite from one `unittest.TestCase` subclass and distinguish the number of loaded test methods from the number of subtests those methods execute.
Run a suite with `unittest.TextTestRunner.run()` and read `testsRun`, `failures`, and `errors` from the returned result to build a summary.
Run a test suite and use the returned `TestResult` to report how many tests started, how many failures and errors were recorded, and whether the run was successful.