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.
grocery_utils.py is a utility module containing three pure functions for sorting and searching a frequency dictionary. It defines no classes, holds no state, and produces no side effects beyond printing results directly to stdout. These functions accept any Python dictionary whose keys are item-name strings and whose values are integer counts — they are compatible with both GroceryFrequency.frequency (a defaultdict) and the plain dict objects returned by db_handler query functions.
Import
Functions
sort_alpha
freq_data in alphabetical (A–Z) order using Python’s built-in sorted() and prints each item and its count in the format item: count. Keys that differ only in case will already be normalized to lowercase if data has been loaded through GroceryFrequency or CornerGrocerApp.
A dictionary mapping item-name strings to integer frequency counts. For example,
{'apples': 4, 'bananas': 3}.None. Output is printed to stdout.
sort_by_freq
freq_data by value (frequency count) in descending order using sorted(..., key=lambda x: x[1], reverse=True) and prints each item and its count in the format item: count. Items with equal counts are printed in the order Python’s sort returns them (stable sort preserving original insertion order for ties).
A dictionary mapping item-name strings to integer frequency counts.
None. Output is printed to stdout.
search_item
item_name to lowercase and checks whether it exists as a key in freq_data. If found, prints item: count. If not found, prints Item not found. Both messages are written directly to stdout.
A dictionary mapping lowercase item-name strings to integer frequency counts.
The item to search for. Case-insensitive — the function applies
.lower() before the lookup.None. Output is printed to stdout.
Combined Example
All three functions print directly to
stdout and return None. They are designed for terminal use and are not suitable for contexts where programmatic access to sorted or filtered results is required. If you need to consume the output in code, use Python’s built-in sorted() directly on your frequency dictionary.