Python: Measure Time and Memory

When debugging, I often want to measure a piece of code's performance/memory characteristics. I do this with the stdlib time class and psutil module. These are some snippets I always have in my utils before starting a project. Note: I use perf_counter (i.e performance counter clock) to measure time. For more details, see this. Measuring time and memory I use python's contextmanager to manage state. Realpython has a nice post on this....

March 11, 2023

Logging in Python

Python does not come with reasonable logging defaults. The following code usually prints nothing. import logging logging.info("Hello world") While it does provide `logging.basicConfig()` which kinda works (sets up stderr by default), I prefer setting this up myself. This snippet sets up logging, adds a formatter and outputs error/info logs to the respective std streams. Without setting up the filter for errors, log aggregators confuse a python error log with an info log....

March 6, 2023