Pydantic Private Fields (or Attributes)

Let’s say we have a simple Pydantic model that looks like this: When you instantiate a Pydantic model – post = Post(**data) – all the required attributes defined must be passed, i.e., id, title, body. What if you had one or more of the following requirements: This would mostly require us to have an attribute […]

How to Capture and Log Tracebacks (Stack trace) in Python

Once you’ve caught an exception, you can capture and log a message with the exception info (including full stack traceback), using logging.exception() or logging.error(). With logging.exception(): With logging.error() we must set the exc_info keyword argument to True (default is False): In both cases, the Error occurred message is logged with level ERROR on the root […]

Pytest Print or Dump Variables to Console for Debugging

Trying to print(var) in your pytest tests and wondering why you cannot see the output in your terminal’s standard output? This is because by default pytest captures all output sent to stdout and stderr, i.e., it intercepts the data going into the low-level file descriptors 1 and 2. This is a good idea because pytest […]