Logging, deque, weakref and templates

print is not a log. deque is a fast queue. weakref is ‘do not keep this object alive just because I cached it’. string.Template is a safer fill-in than format with untrusted text.

Pocket-money pouch

Combine skills into a small tool.

You will be able to

  • Log with a level prefix, not only print
  • See a short queue as the analog of deque
  • Prefer templates / f-strings over format with untrusted text

print is not a log

A log is a time-ordered trail someone else will grep at 2 a.m. logging in the stdlib adds levels (INFO, ERROR), timestamps and a place to send lines (file, stderr). print disappears into a terminal history. In the sandbox we practise a prefix: INFO then the message — the habit of a level, not a pile of unmarked numbers.

deque is a fast queue: append on one end, drop from the other, optional maxlen so the last five DHT beeps fit. weakref is ‘do not keep this sensor object alive just because I cached a pointer’. You store last Celsius as a number, not a live USB handle.

Fill-in that will not explode

string.Template with safe_substitute is a safer fill-in than str.format when a clerk-supplied name might contain braces. f-strings are fine when the names are yours. Untrusted WhatsApp text does not belong in a format string.

Cache numbers, not lives

A weak cache on a laptop lets the GC drop a closed device. Here you just refuse to keep a fake ‘handle’ in CONFIG — keep the last reading integer instead.

Words that matter

Log level
INFO / WARNING / ERROR — how loud this line is.
deque
A fast queue, optionally bounded.
Template
Named fill-in that is safer with untrusted braces.

Common mistakes

Avoid: Keeping a live USB sensor in a dict ‘cache’ forever.

Do this: Log the last Celsius; let the device object die (weakref on a laptop).

Run it step by step

Each box is a real program. Press Run, change a number, Run again — the output must follow your code.

1. Step 1 — a level on the line

INFO is searchable. A bare 236 is not.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

2. Step 2 — a bounded alert queue

Only three slots. Extra beeps do not grow RAM.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

On a full Python install — logging, deque, weakref, string.Template

Ops-grepable levels; a bounded alert queue; do not pin live devices; safer notice text.

No pip — stdlib.

One logger. deque of recent C. Template for a notice.

Real library code (not run in this browser sandbox)

import logging
import weakref
from collections import deque
from string import Template

logging.basicConfig(level=logging.INFO, format="%(levelname)s %(message)s")
log = logging.getLogger("gate")

recent = deque(maxlen=5)
recent.append(31)
log.info("dht %s", list(recent))

notice = Template("Lab $lab is $pct percent full")
print(notice.safe_substitute(lab="A", pct=90))

# weakref.ref(sensor) — cache must not keep a closed USB device alive
print("weakref: store last C, not the live handle")

Example program — Template analog plus a cache number

Named holes. Last C is an int — not a fake device handle.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

Your turn — Log a bill

Print a line that includes both INFO and 236.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

Self-assessment

Check your understanding before you mark the lesson complete.

1. logging exists so that…
2. A cache of DHT objects should…

Progress is stored in a browser cookie on this device.