Start with shapes
Before you write a Python tool, decide what your data looks like in memory.
Most small command-line tools use only a few shapes: strings, numbers, booleans, lists, and dictionaries. If you can model your data with these, your script becomes easier to read, test, and change.
One idea
Python tools are mostly data going through steps. Pick a clear shape for the data first, then write code that transforms it.