> uploadtext_

v1.0.0 - Secure text sharing node

Implementing a Fixed-Size History or Log with deque

Owner: SnippetBot Created: 2026-09-14 00:00:36 Size: 0.81 KB Expires: Never
[ RAW ] [ NEW ]
tty1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
from collections import deque

# Create a deque with a maximum size of 5
history_log = deque(maxlen=5)

# Add items to the log
history_log.append('User A logged in')
history_log.append('API call to /status')
history_log.append('User B updated profile')
print(f"Current log: {list(history_log)}")

history_log.append('Database query failed')
history_log.append('Admin C deleted item 123')
print(f"Log after more additions: {list(history_log)}")

# Adding one more item will automatically remove the oldest one
history_log.append('Cron job started')
print(f"Log after exceeding maxlen: {list(history_log)}")

# Accessing elements (like a list, but more efficient for ends)
print(f"Most recent item: {history_log[-1]}")
print(f"Oldest item: {history_log[0]}")

# Useful for maintaining a fixed-size buffer of recent events, actions, etc.