try...except blocks only
after they occur.
if statements) to ensure an operation will succeed before attempting
to execute it.
try..except..as..else..finally
raise SomeException when the code is technically valid but violates business logic.
raise...from to throw a more relevant exception while preserving the original error's traceback
for debugging.
Exception class to provide more precise
error reporting.
with vs. try...finally
with statement for resource management; it is safer, more concise, and eliminates
the risk of forgetting to release resources; i.e., use with to handle the finally
part and wrap it in a try...except block to handle logic errors.
loguru library has them all.
| Dimension | Key Concepts | Problem Solved |
|---|---|---|
| Content | Timestamp, File, Func, Line, Col, Msg | Tracing: Pinpoint exactly when and where. |
| Metadata | Levels, Stacktrace | Filtering: Gauge severity and capture evidence. |
| Storage | Rotation & Retention | Survival: Prevent disk overflow and manage history. |
| Distribution | Multi-Handler, Console/File/Cloud | Delivery: Debug live vs. persistent storage. |
| Architecture | Global Singleton, Entry Config | Consistency: Unified logging across all modules. |
| Compatibility | Structured Logging (e.g. JSON) | Searchability: Integration with Cloud tools. |
| Performance | Asynchronous Writing | Efficiency: Zero impact on application speed. |
| Security | Data Masking/Anonymization | Compliance: Protect sensitive user info. |
| Tracing | Context Binding, Request ID | Correlation: Link logs for a single request. |
loguru.
| Concept | Description |
|---|---|
| Case | The smallest, atomic unit of testing that verifies a specific behavior by comparing an actual outcome against an expected result. |
| Fixture | A baseline set of preconditions (files, database connections, or state) and post-test cleanup to ensure test isolation. |
| Suite | A logical grouping of test cases used to manage execution flow, typically categorized by module, tag, or priority. |
| Runner | The execution engine that automatically discovers test files, manages the lifecycle of fixtures, and provides structured reporting. |
pytest (most popular and preferred), unittest (built-in but not as powerful), and
others, see below for a simplistic usage example with pytest.
uv (preferred) or poetry.