Documentation Index
Fetch the complete documentation index at: https://mintlify.com/apursley2012/corner-grocer/llms.txt
Use this file to discover all available pages before exploring further.
GroceryFrequency is the core class in corner_grocer.py, encapsulating in-memory frequency storage using collections.defaultdict. It reads a plain-text grocery purchase log, accumulates per-item counts in memory, and exposes methods for lookups, sorted output, and ASCII histograms. This class is used by the file-based (non-database) version of Corner Grocer and works alongside the helper functions in grocery_utils.py.
Instantiation
Methods
__init__
GroceryFrequency instance by creating an empty defaultdict(int) for frequency storage. No file I/O is performed at construction time — the dictionary is populated lazily by calling process_input_file.
Parameters: None
Returns: A new GroceryFrequency instance with self.frequency set to an empty defaultdict(int).
process_input_file
self.frequency. Empty lines (after stripping) are ignored. If the file does not exist, the method prints an error message and terminates the process with sys.exit(1).
Path to the input text file. Each non-empty line should contain exactly one item name. The file is read in the default system encoding.
None. Populates self.frequency in place.
Error handling: Catches FileNotFoundError. Prints Error: File '{filename}' not found. and exits with status code 1.
lookup_item_frequency
item string is normalized to lowercase before the lookup so that 'Apples', 'APPLES', and 'apples' all resolve to the same key. Returns 0 if the item has never been seen.
The item name to look up. Case-insensitive.
int — the number of times the item appeared in the input file, or 0 if not found.
print_all_frequencies
self.frequency sorted alphabetically (A–Z) and prints each item along with its count in the format item: count. Output is written directly to stdout.
Parameters: None
Returns: None
print_histogram
self.frequency sorted alphabetically (A–Z) and prints each item followed by a string of asterisks whose length equals the item’s count. This provides a visual bar chart in the terminal. Output is written directly to stdout.
Parameters: None
Returns: None
Standalone Helper Functions
The following module-level functions are defined incorner_grocer.py outside the GroceryFrequency class. They handle menu display and user input validation for the interactive terminal application.
get_validated_choice
"Enter your choice: " and attempts to parse the response as an integer. Returns the integer if it falls in the range 1–7 (inclusive). Prints an error message and returns 0 for any out-of-range value or non-integer input.
Parameters: None
Returns: int — a valid choice in the range 1–7, or 0 on invalid input.
is_valid_item_name
item_name is either an alphabetic letter or a space. Rejects strings containing digits, punctuation, or other special characters.
The item name string to validate.
bool — True if all characters pass the check, False otherwise.
print_menu
stdout and flushes the output buffer. The menu includes options for item lookup, frequency display, histogram rendering, alphabetical sort, frequency sort, item search, and program exit.
Parameters: None
Returns: None