This piece describes Penny, a simple command-line expense tracker built with Python and the Click library. Penny allows users to add, list, summarize, and clear expenses directly from their terminal without needing a web interface or database. The author chose Click over Python's built-in argparse for its more intuitive, decorator-based approach to defining commands and options.The project is structured into three main files: commands.py for individual command logic, penny.py to group these commands into a single CLI tool, and pyproject.toml for packaging and defining the entry point. Penny stores expense data in a plain JSON file named expenses.json, reading and writing to it for each operation. The add command creates the JSON file if it doesn't exist, appends new expenses, and saves the updated list, providing colored confirmation.The list command displays saved expenses in a tabular format, offering a warning if no expenses are found. The summary command calculates and prints spending totals by category and a grand total, outputting the results as formatted JSON. The clear command includes a confirmation prompt before overwriting the expenses.json file with an empty list.The penny.py file uses Click's group functionality to consolidate all commands under a single penny executable. The pyproject.toml file configures this grouping, making Penny a globally installable command-line tool via pip install .. The author highlights the benefits of Click's declarative style, the suitability of JSON for simple data storage, the importance of small user experience details, and how command grouping elevates a script into a polished CLI application.
commands.pyfor individual command logic,penny.pyto group these commands into a single CLI tool, andpyproject.tomlfor packaging and defining the entry point. Penny stores expense data in a plain JSON file namedexpenses.json, reading and writing to it for each operation. Theaddcommand creates the JSON file if it doesn't exist, appends new expenses, and saves the updated list, providing colored confirmation.Thelistcommand displays saved expenses in a tabular format, offering a warning if no expenses are found. Thesummarycommand calculates and prints spending totals by category and a grand total, outputting the results as formatted JSON. Theclearcommand includes a confirmation prompt before overwriting theexpenses.jsonfile with an empty list.Thepenny.pyfile uses Click's group functionality to consolidate all commands under a singlepennyexecutable. Thepyproject.tomlfile configures this grouping, making Penny a globally installable command-line tool viapip install .. The author highlights the benefits of Click's declarative style, the suitability of JSON for simple data storage, the importance of small user experience details, and how command grouping elevates a script into a polished CLI application.