r/Python • u/Pharaoh_Bellhop8 • 6h ago
Discussion I've started treating the output of my little Python scripts as a user interface
I inherited a bunch of internal Python scripts at work last year. Most of them were perfectly readable, did their job and had been running for years without any major issues.
The annoying part was figuring out what they'd actually done.
One would print "Done" whether it processed 400 files or found nothing to process. Another would silently skip invalid records. To understand the result of a successful run I sometimes had to read the source code.
I've since developed a habit of ending even fairly trivial scripts with a small summary of what was found, what changed, what was skipped and why.
It's probably the smallest change I've made to my coding habits with the biggest practical payoff. Especially when revisiting something I wrote six months ago.
I think we spend a disproportionate amount of time making Python code pleasant for other developers to read compared to making Python programs pleasant for other people to actually run.
68
u/frankieinaprius 6h ago
This is what logging is for. And by using log levels appropriately, you control how verbose you want the output to be.
34
u/Spawn_quiche 6h ago
"Successfully processed 0 records" and "Failed to find any records to process" are two very different outcomes that an alarming number of scripts consider identical.
10
u/monkeybreath Ignoring PEP 8 5h ago
Normalize responding appropriately to --help for each app. I rarely run my apps from the command line, but I really should document what they do better.
3
u/mdrjevois 2h ago
I call this DBAJTY, in the spirit of DRY and YAGNI. "Don't Be A Jerk To Yourself." So often you are your own end user. Be kind! Don't leave em guessing.
2
u/RandomGgames 2h ago
Over the years I’ve made myself a base script I build all new scripts from. It’s not crazy complicated, but sets up logging and as configurable, so I can customize it per script as I want. Might be something to look into?
3
u/def__eq__ 6h ago
Or use click or textualize..
2
u/patryk_builds 3h ago
Agree, and done after processing zero items is the worst case of it. a run that did nothing and a run that did everything shouldn't look the same. test runners have the same problem: a pass with zero tests collected looks identical to a real pass unless the count is printed.
A few things in the same spirit that cost almost nothing:
- Treat zero as suspicious. if the script expected input and found none, say so loudly, and consider a non-zero exit code. whatever calls the script (cron, ci) only sees the exit code
- Count the skips by reason. skipped 12: 9 missing date, 3 duplicate id" is far more useful than a total, and it's just a Counter
- Summary to stdout, diagnostics to stderr, so the summary can be piped or saved without noise
- A --dry-run flag that prints the same summary without changing anything. the summary code is already there, so it's nearly free
- Print what the script was pointed at: the input path, the date range, the environment. half of "why did it do nothing is it looked in the wrong folder The six months later point is the real one. the person running it is usually future you, with no memory of what done was supposed to mean
2
u/rando1-6180 5h ago
This is an excellent practice that depends on your need, environment and budget. The scripts you inherited might have been written in a pinch.
Making scripts "fit for human consumption" is very valuable because it reduces cognitive burden for you and anyone else succeeding you. Institutional knowledge transfer is a thorn in the side of many teams. This is one way to reduce that burden.
If you pursue this, I suggest starting the script with a context report. It provides things like run timestamp, the exact command run, any arguments and any options, and any defaults. If you are processing sequence, provide a row count.
Then enumerate the steps, as a section in your output, as you are executing with whatever details belong to that step.
Alternatively, you can use the logger, which structures messages into info, warning and error. It provides some scaffolding that helps you cleanly format and organize your messages. Be ready to manage logging from modules you import though.
Consider writing script level docstrings with example usage.
Be mindful of your exit code. 0 means all good and non-zero is the error code for your error classifications (e.g. 1 is operational like file doesn't exist, 2 is validation/business logic error).
Lastly, you can elevate by providing a curated experience by providing suggested next commands. Make them copy/paste ready. This will help you when you need to revisit a bunch of related scripts.
I think we can give ourselves and ops folks a better experience.
1
u/Smort01 5h ago
I started reading from an online API, and putting every shit In a try/except clause with readable error handling saved me so many headdaches.
-3
u/nicksuperb 5h ago
Try marimo and make yourself a nice playbook.
https://marimo.io/