Skip Navigation

User banner
Python

Members
0
Posts
47
Created
3 yr. ago

usb-rea: a small Python CLI that turns a USB capture into a protocol report and a pyusb driver

I wrote this for hardware that only ships a Windows app (case LCDs, RGB controllers, AIO coolers, mice). You record USB traffic while clicking through the vendor app (usbmon on Linux, USBPcap on Windows), note what each click did, and usb-rea infers the command layout: constant bytes, counters, value fields with their scaling, lookup tables, checksums and CRCs, handshakes and image streams. Output is a Markdown report where every claim points at Wireshark frame numbers, plus a standalone pyusb driver. A few Python-side details that might interest people here: • The pcap and pcapng parsing is plain standard library, so the only runtime dependency besides the optional MCP server is pyusb. • Linear field fits have to survive leave-one-out cross-checks before they are reported, so it says "unknown" rather than guessing. • usb-rea demo --device fan generates a synthetic capture and runs the whole pipeline, so you can try it with no hardware. It is MIT licensed and on PyPI (pip install usb-rea). On known protocols, for example the NZXT Kraken X fan command from liquidctl, it recovers the packet format byte for byte. Feedback on the code or the inference approach is welcome.

github.com

Four rules that keep a Python tool that runs as root honest (from building Noust)

Noust is a free (AGPL) Python 3.10+ CLI and FastAPI service that deploys web apps onto Linux servers. It runs as root, so a few classes of bugs are expensive. The project ended up with four rules, each enforced by a test, and each one exists because its absence shipped a real bug: 1. Nothing runs a process except one module. Only core/runner.py may import subprocess. Argv only, never a shell, timeouts mandatory, secrets through env= or input= (never argv, which every local user can read in ps). The test suite's conftest makes any real process call fail, so code that bypasses the runner can't even be tested. --dry-run lives there too, which is why it can't be honoured in one path and ignored in another. 2. except Exception only at an error boundary, and it must log. There used to be 302 of them, 149 silent; that's how calls to methods that didn't exist shipped for whole releases. mypy blocks CI. 3. One implementation of each thing. The web API is a client of the same managers the CLI uses, never a second implementation. 4. Guards at the chokepoint. Unit ownership is checked in the service manager, read-only SQL is enforced by the database server, escaping by the template engine. The contributing guide explains each one with the bug behind it: https://github.com/Perkybeet/noust/blob/main/CONTRIBUTING.md Interested in how others structure similar tools.

github.com

Google's Custom Search JSON API shuts down on Jan 1, 2027 (and a free alternative for your own rankings)

If you've got anything running on Google's Custom Search JSON API, it's on a clock. Google closed it to new customers and their overview page says it's gone on January 1, 2027.

Easy to miss in the docs: one call returns 10 results and a query tops out at 100. So pulling the top 100 for a single keyword is 10 billable calls.

For checking your own site's rankings there's an easier path anyway. The Search Console API hands you query, page, country and device rows, up to 25,000 per request, and it's all within Google's rules.

Anyone already moved off Custom Search? What did you switch to?

Python keeps the proxy status that Chrome throws away, so don't retry every ProxyError

Got ERR_TUNNEL_CONNECTION_FAILED in Chrome and no idea why? Try the same proxy from Python, because it'll tell you what the browser won't. Chromium drops every CONNECT reply except 200 and 407, so you can't tell a policy block from a dead exit in there.

Python leaves the status in the exception text. You'll see OSError: Tunnel connection failed: 502 Bad Gateway from http.client, and requests wraps that in a ProxyError with the same words inside.

I'd read that string before retrying anything, since a blanket except ProxyError plus a retry loop just burns time on errors that can't heal. A 403 is usually policy, sometimes just a port they don't allow. 407 means check your login. If you get 405 or 501 you aren't talking to a forward proxy at all. Only 502, 503 and 504 are worth backing off on, those happened behind the proxy.

Disclosure: we run a proxy service, none of this depends on whose.

A plea not to use uv-build for Python

@mgorny@treehouse.systems:

After seeing yet another dozen packages lose keywords on Gentoo because of a single key dependency switching to uv-build, I would like to plea on behalf of Gentoo users: please don’t use uv-build as a build backend.

Just to be clear, this is not about uv itself or any other Astral tools; these are good tools with a nice and responsive upstream. The uv-build backend is one thing we have to disagree on. I’m not saying it’s bad; I’m saying that its benefits don’t outweigh the drawbacks.

blogs.gentoo.org

Hard Python Dictionary Exercise

A hard exercise to help build the right mental model for Python data. • Solution: https://memory-graph.com/#codeurl=https%3A%2F%2Fraw.githubusercontent.com%2Fbterwijn%2Fmemory_graph_videos%2Frefs%2Fheads%2Fmain%2Fexercises%2Fexercise26.py&play= • Explanation: https://github.com/bterwijn/memory_graph?tab=readme-ov-file#python-data-model The “Solution” link visualizes execution and reveals what’s actually happening using 𝗺𝗲𝗺𝗼𝗿𝘆_𝗴𝗿𝗮𝗽𝗵: https://github.com/bterwijn/memory_graph

5 tools that helped me modernize a legacy app

So I recently inherited a legacy application that was a nightmare to maintain (tooling was clearly lacking and the codebase was pretty outdated). I quickly realized that if I ever wanted to actually enjoy working on it, I would have to give it a proper overhaul. These are the 5 changes that had some of the biggest impact IMO: 1. uv: Being new to the Python ecosystem, I didn't want to figure out pip vs poetry vs pyenv vs virtualenv, so I just used uv and let it handle all of that. One Rust-based tool that installs and pins Python versions, manages the venv automatically and locks deps in a uv.lock for reproducible builds. Installs are also a lot faster, which makes CI a lot less painful. 2. Ruff: Coming from JS, I really missed eslint --fix for automatically fixing linting issues and format code on save. Ruff brought that experience back. I know that there are plenty of linters and formatters in the Python ecosystem, but this one really stands out for me. Since it's built in Rust, it's super fast. 3. Dependabot: Instead of remembering to update dependencies every few months, I enabled Dependabot. It automatically opens PRs when updates are available and then CI tells me whether they're safe to merge. It takes only a couple of minutes to set up but saves a lot of maintenance. 4. Pylance: Without it, VS Code gives generic completions and never warns you about passing the wrong type until runtime. Pylance provides proper type-aware autocompletion, jump-to-definition (even in third-party libraries), inline documentation, and real-time type checking. I personally keep it on "basic" mode for legacy codebases, since "strict" surfaced hundreds of errors (thank you, but no thank you lol). 5. Pydantic: Pydantic is basically Python's Zod: you declare a model, pass your data in and get either a validated typed object or a ValidationError naming the exact field at fault. It really helped me keep the codebase clean, with one place defining the shape of the data instead of raw dicts floating around. I use it wherever data comes from outside, like API payloads, forms, and env vars with pydantic-settings, which fails at startup instead of mid-request. None of these tools changed the application itself. But together they made working on it a lot less frustrating.

marmelab.com

How does Lazy Evaluation work in Python?

In last week’s post we showed that a Python for-loop works through the Iterator Protocol. This same protocol also enables values to be produced lazily. In this new Lazy Evalution Example we show one sink reading values from five different sources: • source1: eagerly returns all values by list • source2: lazily by generator function using yield • source3: eagerly by list comprehension • source4: lazily by generator expression • source5: lazily by iterator protocol The same for-loop consumes all five sources. The eager sources produce all values before the sink starts consuming them. In the lazy sources producer and consumer take turns: • produce → consume → produce → consume → … Each value is produced only when the for-loop requests it. Generator functions and generator expressions are concise, readable ways to create lazy iterables. The final source makes their underlying mechanism explicit: • iter() obtains an iterator. • next() requests the next value. • StopIteration signals that no values remain. This clearly shows that generators achieve lazy iteration by implementing Python’s Iterator Protocol.