Use deque for tracking last n whatever

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the algorithms category.

Last Updated: 2024-03-05

I wanted to track the number of missing data points in the dimensions zipCode, basket or totalAmount over the last 100 requests for a machine learning algorithm.

My original implementation was this inelegant bulk:

metrics_lookback = 100
failures = {
    "requests_seen": 0,
    "zipCode": np.zeros((metrics_lookback)),
    "basket": np.zeros((metrics_lookback)),
    "totalAmount": np.zeros((metrics_lookback))

data = {**failures}
data["zipCode"] = int(failures["zipCode"].sum())
data["basket"] = int(failures["basket"].sum())
data["totalAmount"] = int(failures["totalAmount"].sum())

I could have made it much simpler with a deque structure:

>>> d=deque(maxlen=3)
>>> d.append(2)
deque([2], maxlen=3)
>>> d.append(3)
deque([2, 3], maxlen=3)
>>> d.append(4)
deque([2, 3, 4], maxlen=3)
>>> d.append(5)
deque([3, 4, 5], maxlen=3)