The Quiet Revolution in Automation
Attention mechanisms have reshaped every corner of Automation. I had traced their origins, their math, and what comes next when the context window has no ceiling.
Where code meets curiosity
A Python tutorial blog exploring comprehensive PYTHON programming methods, algorithms, and the culture of contemporary coding to keep pace with the fast-moving AI technology. Deep dives for developers — from beginner Python projects to advanced programming concepts.OOP programming is the DNA of AI.
introduction
"We live between the valleys of human thought and the bytes of machine logic — this is where I write."
Valleys & Bytes is an independent technology publication dedicated to exploring the intersections of software engineering, computational thinking, and the culture surrounding the machines we build. Founded in 2023, this space was born out of a conviction that technical writing doesn't have to be dry — it can be poetic, provocative, and precise all at once.
Every article here is a deep-dive: no shallow takes, no engagement-bait. Whether dissecting a sorting algorithm or tracing the philosophy behind open-source software, we commit to the full picture. Pull up a chair. The terminal is always on.
Featured articles
Attention mechanisms have reshaped every corner of Automation. I had traced their origins, their math, and what comes next when the context window has no ceiling.
The article that started it all
★ Signature Feature · Python · Step-by-Step
This humble Python script is the heart and soul of Valleys & Bytes. A complete command-line vegetable ordering system built from scratch — and pulled apart line by line so every piece of logic is crystal clear. Whether you're a beginner meeting Python for the first time or an experienced dev who wants a crisp refresher, this is the article for you.
Full source · vegetable_shop.py
def main(): prices = { "Eggplant (Talong)": 80, "Tomatoes": 120, "Potatoes": 140, "Onions": 160, "Garlic": 180, "Pechay": 80, "Broccoli": 170, "Carrots": 160, "Radish (Labanos)": 80, "Alugbati": 60, "Kangkong": 50, "Cabbage": 150 } order = {} print("════════════════════════════════════════════") print(" WELCOME TO THE VEGETABLE SHOP ") print("════════════════════════════════════════════") print(f"{'Item Name':<25} {'Price per kg':>15}") print("-" * 42) for veg, price in prices.items(): print(f"{veg:<25} ₱{price:>14.2f}") print("-" * 42) print("\nINSTRUCTIONS:") print("• Enter quantity in kg (e.g., 0.5, 2, 1.25)") print("• Press Enter to skip an item") print("• Type 'DONE' at any time to finish and pay\n") for vegetable, price in prices.items(): while True: try: user_input = input(f"How many kg of {vegetable}? ").strip().lower() if user_input == "done": break if user_input == "": qty = 0.0 break qty = float(user_input) if qty < 0: print("Quantity cannot be negative.") continue break except ValueError: print("Invalid input. Please enter a number or 'done'.") if 'user_input' in locals() and user_input == "done": break if qty > 0: order[vegetable] = qty if not order: print("\nNo items selected. Have a nice day!") return print("\n" + "="*60) print("FINAL RECEIPT".center(60)) print("="*60) print(f"{'Vegetable':<20} {'Qty (kg)':>10} {'Unit Price':>12} {'Subtotal':>12}") print("-" * 60) total = 0.0 for veg, qty in order.items(): subtotal = qty * prices[veg] total += subtotal print(f"{veg:<20} {qty:>10.2f} {prices[veg]:>12.2f} {subtotal:>12.2f}") print("-" * 60) print(f"{'TOTAL AMOUNT DUE':<44} ₱{total:>11.2f}") print("="*60) print("\nThank you for shopping with us!") if __name__ == "__main__": main()
Interactive demo
This is a fully functional JavaScript simulation of the Python program. Enter quantities, type done to finish early, press Enter to skip — it behaves exactly like the real script.
Step-By-Step explanation
A function definition wraps all program logic under a single named block. Encapsulating everything inside main() keeps variables out of global scope, prevents accidental side effects on import, and makes the code easy to test in isolation.
Think of it as the engine — nothing runs until main() is explicitly called at the very bottom of the file.
prices DictionaryA Python dictionary stores key-value pairs. Each vegetable name (the key) maps to its price per kg (the value). Dictionaries allow instant O(1) lookup — prices["Garlic"] returns 180 without scanning the entire collection.
This is the program's single source of truth for all pricing. Updating a price means changing exactly one number in one place.
order DictionaryAn empty dictionary initialised to collect the customer's selections as the program runs. As quantities are entered it grows dynamically: order["Tomatoes"] = 1.5.
Only items with qty > 0 are ever stored, so the final receipt stays clean — no zero-quantity rows cluttering the output.
F-strings with alignment specifiers produce a neat, readable price table before any ordering begins — so customers know what's available and what it costs.
| Specifier | Meaning |
|---|---|
| :<25 | Left-align, pad to 25 chars |
| :>14.2f | Right-align float, 2 decimals |
| "–" * 42 | Repeats a separator line |
for + while LoopsThe for loop visits each vegetable exactly once, in order. The inner while True loop keeps re-asking the same question until the user provides valid input — it can only escape via break.
.strip().lower() normalises the raw input so "DONE", "Done", and "done" are all treated identically.
Calling float("abc") raises a ValueError that would crash an unprotected program. The try/except block intercepts it and prints a helpful message instead of terminating.
| Keyword | What it does |
|---|---|
| continue | Skips back to top of while loop |
| break | Exits the current loop level |
A single break only exits one loop level. Because the loops are nested, two strategically placed break statements are needed — the first escapes the while loop, the second escapes the for loop.
'user_input' in locals() is a defensive guard ensuring the variable exists before it's read, preventing a NameError on first iteration.
Skipped items (Enter pressed) default to qty = 0.0. The if qty > 0 guard ensures only genuinely ordered vegetables are added to the order dictionary.
This keeps the receipt accurate and uncluttered — a small condition with a big impact on the final output's quality.
not order evaluates to True when the dictionary is empty. The return statement exits the function immediately, preventing the receipt section from running with no data to display.
This is called a guard clause — a clean pattern that handles edge cases at the top rather than nesting them inside the main logic.
total starts at 0.0. Each iteration computes subtotal = qty × price and adds it to the running total — the classic accumulator pattern.
prices[veg] cross-references the original prices dictionary by name, since order only stores quantities. Two dictionaries working as one system.
__main__ GuardWhen this file is run directly, Python sets __name__ to "__main__" and main() is called. When it's imported by another script, __name__ equals the filename — so main() is silently skipped.
This is a universal Python best practice that makes every script both safely importable as a module and directly runnable as a program.
// program flow
"The best code is the code you don't have to write. The second best is code so clear it reads like a well-argued essay — confident, purposeful, and impossible to misread."— Valleys & Bytes Editorial Manifesto, Issue #001
Code lab
from functools import cache from typing import Generator # ── Memoised Fibonacci with generator ────────────────────── @cache def fib(n: int) -> int: """O(n) time, O(n) space — but only computed once per n.""" return n if n < 2 else fib(n - 1) + fib(n - 2) def fib_stream(limit: int) -> Generator[int, None, None]: """Lazy infinite stream — pull only what you need.""" a, b = 0, 1 while a <= limit: yield a a, b = b, a + b # ── Context manager for timing code blocks ────────────────── from contextlib import contextmanager import time @contextmanager def timer(label: str = "block"): start = time.perf_counter() try: yield finally: elapsed = time.perf_counter() - start print(f"[{label}] {elapsed * 1000:.2f}ms") # Usage with timer("fib(35)"): result = fib(35) print(f"fib(35) = {result}") first_20 = list(fib_stream(6765)) print(f"First 20 Fibonacci: {first_20}")
// ── Safe concurrent counter using Arc + Mutex ─────────────── use std::sync::{Arc, Mutex}; use std::thread; fn main() { let counter = Arc::new(Mutex::new(0u64)); let mut handles = Vec::new(); for _ in 0..8 { let c = Arc::clone(&counter); handles.push(thread::spawn(move || { for _ in 0..1_000_000 { let mut val = c.lock().unwrap(); *val += 1; } })); } for h in handles { h.join().unwrap(); } println!("Final count: {}", *counter.lock().unwrap()); // Always prints 8_000_000 — no data race possible } // ── Zero-copy string slicing ────────────────────────────────── fn first_word(s: &str) -> &str { s.split_whitespace().next().unwrap_or("") } The returned &str borrows from s — no allocation, no copy. // The borrow checker guarantees s outlives the slice. fn demonstrate() { let sentence = String::from("valleys and bytes"); let word = first_word(&sentence); &str into String's buffer println!("First word: {word}"); }
── Observable pattern — reactive state without a framework ── function createStore(initialState) { let state = structuredClone(initialState); const listeners = new Set(); return { getState: () => structuredClone(state), setState(updater) { const next = typeof updater === 'function' ? updater(state) : updater; state = { ...state, ...next }; listeners.forEach(fn => fn(state)); }, subscribe(fn) { listeners.add(fn); return () => listeners.delete(fn); // unsubscribe } }; } // Usage const store = createStore({ count: 0, user: null }); const unsubscribe = store.subscribe(state => { console.log('State changed:', state); }); store.setState(s => ({ count: s.count + 1 })); store.setState({ user: { name: 'Ada' } }); unsubscribe(); // stop listening // ── Async retry with exponential backoff ────────────────────── async function withRetry(fn, { retries = 3, base = 300 } = {}) { for (let i = 0; i <= retries; i++) { try { return await fn(); } catch (err) { if (i === retries) throw err; await new Promise(r => setTimeout(r, base * 2 ** i)); } } }
-- ── Window functions: running totals and rankings ──────────── SELECT author, title, views, published_at, -- Running total of views per author, ordered by date SUM(views) OVER ( PARTITION BY author ORDER BY published_at ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) AS running_views, -- Dense rank by views (ties get same rank, no gaps) DENSE_RANK() OVER (ORDER BY views DESC) AS popularity_rank, -- 3-article rolling average views AVG(views) OVER ( PARTITION BY author ORDER BY published_at ROWS BETWEEN 2 PRECEDING AND CURRENT ROW ) AS rolling_avg_3 FROM articles WHERE published_at >= NOW() - INTERVAL '90 days' ORDER BY author, published_at; -- ── Recursive CTE: traverse a tag hierarchy ────────────────── WITH RECURSIVE tag_tree AS ( SELECT id, name, parent_id, 0 AS depth FROM tags WHERE parent_id IS NULL UNION ALL SELECT t.id, t.name, t.parent_id, tt.depth + 1 FROM tags t JOIN tag_tree tt ON t.parent_id = tt.id ) SELECT REPEAT(' ', depth) || name AS hierarchy FROM tag_tree ORDER BY depth, name;
#!/usr/bin/env bash # ── Robust script template with error handling ──────────────── set -euo pipefail # exit on error, unset var, pipe fail trap 'cleanup' EXIT INT TERM SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" LOG_FILE="${SCRIPT_DIR}/deploy.log" log() { printf '[%s] %s\n' "$(date +%T)" "$*" | tee -a "$LOG_FILE"; } die() { log "ERROR: $*"; exit 1; } cleanup() { log "Script finished."; } # ── Check dependencies ──────────────────────────────────────── require() { for cmd in "$@"; do command -v "$cmd" &>/dev/null || die "Required: $cmd not found" done } require docker kubectl git curl # ── Parallel execution with job control ─────────────────────── declare -a pids=() for service in api worker scheduler; do docker build -t "app/${service}:${GIT_SHA}" "services/${service}" & pids+=("$!") log "Building ${service} (PID $!)" done for pid in "${pids[@]}"; do wait "$pid" || die "Build failed (PID $pid)" done log "All services built successfully."
// tech radar · Q1 2026
Curated reading list
Stay connected
Join the community across every platform. Get updates, code snippets, and behind-the-scenes dispatches wherever you spend your screen time.
Discussion
Subscription
Choose a plan that fits how deep you want to go. From free weekly digests to full archive access and community perks.
no spam · unsubscribe anytime · your data stays yours
Or just subscribe free — no plan required:
Leave a Comment
The section on attention mechanisms finally made it click for me. I've read three textbooks and this article explained it better in 1,200 words. The analogy with spotlight allocation was particularly elegant.
As someone coming from a Python background, the Rust article felt like being told you've been riding with training wheels your whole life. Simultaneously humbling and invigorating. The borrow checker section was a revelation.
The HTMX piece aged incredibly well. My team just ripped out a 40k-line React codebase and replaced it with server-side templates + HTMX. Build times went from 4 minutes to 8 seconds. Valleys & Bytes called it months ago.