Patterns (re) and maths

A 6-digit PIN is a pattern. A mean of lab readings is maths. re and math are stdlib. Validate at the edge; keep the core boring.

{ }[ ]

Telemetry packet

Nested keys like an API payload.

You will be able to

  • Validate a PIN-shaped pattern at the door, before storage
  • Summarise lab readings with min, max and an honest mean
  • Keep re and math at the edge; keep the core boring

Six digits is a policy, not a vibe

Pune PIN 411001 is six digits. A GSTIN has a shape. re is the stdlib module for those patterns — compile once if you loop. math is for roots and constants you should not hard-code as 3.14 in seven files. Neither runs here.

You still reject a short PIN with len, and you still summarise a DHT list with min, max and sum. The boolean is the same as match() on a laptop. Copy the sample into a venv when you need a real pattern.

Validate at the edge; compute in the core

Do not sprinkle regex inside a GST total. Clean the PIN, then store a string. Mean temperature is sum / len — say the unit. A ‘clever’ one-liner that both matches and invoices is how audits fail.

Words that matter

re
Stdlib patterns — digits, GSTIN-shaped text, tokens.
math
Stdlib numbers — roots, constants, not money totals.
Edge validation
Reject garbage before it reaches storage or GST.

Common mistakes

Avoid: Running a regex inside every paise line of a 40,000-row UPI file.

Do this: Validate once at the door; keep the total boring.

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 — pattern as length (toy PIN)

Full Python uses re. The OK/Reject boolean is the same.

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 — summarise DHT readings

min, max, sum are honest. Mean is sum / len.

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 — re, math

PIN at the attendance door, RMS of lab bench readings. Stdlib — no extra appetite.

No pip — stdlib.

Compile the PIN pattern once. Maths stays a function.

Real library code (not run in this browser sandbox)

import math
import re

PIN = re.compile(r"^\d{6}$")

def ok_pin(text):
    return bool(PIN.match(text))

def rms(xs):
    return math.sqrt(sum(x * x for x in xs) / len(xs))

print(ok_pin("411001"), ok_pin("12"), rms([3, 4]))

Example program — PIN then a mean

Two jobs, two functions. No regex inside the mean.

Python sandboxlesson://workspace
console

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

build

Press Build to compile.

Your turn — Reject a short PIN

ok_pin(pin) returns OK if len is 6, else Reject. Print ok_pin("4110") so Reject appears.

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. Regex belongs at the…
2. A mean of lab readings should use…

Progress is stored in a browser cookie on this device.