Python

Output & Logging

33 flashcards · answers and spaced-repetition review in the KnowCard app

In pprint, what does passing depth=2 do to a deeply nested structure?
What does this print (note the absence of any explicit newline)? ``` print("loading", end="") print("...done") ```
You want an error message to go to stderr, not stdout, so it doesn't mix with normal program output. How do you make print write to stderr?
A long-running loop prints progress with end="", but nothing appears on screen until the program finishes. Why, and what fixes it?
Why use pprint.pprint(obj) instead of print(obj) for a big nested list or dict?
You want pprint's nicely formatted output as a string (e.g. to log it or write to a file) rather than printed. Which function?
What's the main reason to use the logging module instead of scattering print() calls for diagnostics?
List the five standard logging levels from least to most severe.
What appears on screen (default config, nothing else set up)? ``` import logging logging.info("starting up") ```
What appears on screen (default config)? ``` import logging logging.warning("disk almost full") ```
You add logging.basicConfig(level=logging.DEBUG) AFTER several logging calls already ran. Does it reconfigure the existing setup?
Why prefer logging.getLogger(__name__) in a module over just calling logging.info(...) on the root logger?
Inside an except block you want the message AND the full traceback in the log. Which call gives both with one line?
Why is logger.info("user %s logged in", name) preferred over logger.info(f"user {name} logged in")?
In logger.info("x=%s", x), what is the second argument x for, and why not pre-format it with % yourself?
How do you change basicConfig so log entries include a timestamp and the level name?
You set logging.basicConfig(level=logging.WARNING) but a named logger's logger.setLevel(logging.DEBUG) — and DEBUG messages still don't show. What's blocking them?
What's the difference between a logging Handler and a Formatter?
Name the four built-in handler destinations mentioned for logging (beyond writing to a plain file).
You attach a logging.FileHandler but don't want the log file created unless something is actually logged. Which parameter helps?
At the prompt, "Hello" echoes as 'Hello' (with quotes) but print("Hello") shows Hello. What's the underlying difference?
Besides depth, which two pprint parameters control layout, and what are their defaults?
By default pprint puts each element of a long list on its own line. Which keyword makes it pack several elements per line instead?
The convenience calls logging.info(msg) and logging.error(msg) are shorthands for what single generic function?
A script logs to a file and exits, but the last few entries are sometimes missing from the file. Which logging call flushes pending writes?
The urgency levels run DEBUG < INFO < WARNING < ERROR < CRITICAL. What is NOTSET and where does it sit?
You run a program using logging.basicConfig(filename="app.log") several times. Do later runs overwrite the log or append to it?
In logging.basicConfig you pass both stream=sys.stdout and filename="app.log". Where do the messages go?
By default a basicConfig format string uses %(levelname)s-style placeholders. How do you switch to {levelname} brace style?
In interactive mode, typing 1.2 echoes 1.2. Why does the same bare line inside a .py script produce no output at all?
Your log lines show the message but not where in the code they came from. Which format identifiers add the function, line number, and module?
asctime renders a timestamp like 2020-02-05 14:28:55,811. How do you change its format (e.g. drop the milliseconds)?
What does this print? ``` print("a", "b", "c", sep="") ```

Start learning today

Free to start — download the app or use it in your browser.

Get it on App StoreGet it on Google Play
Output & Logging (Python) · KnowCard