Human sorting, hardened

Ned Batchelder · 2026年11月25日

AI 摘要 AI 生成,可能有误

本文描述了修复作者'人类排序'(human sorting)函数中一个长尾 bug 的过程:当文件名中包含超过 Python 4300 位整数转换上限(该限制于 2022 年加入,旨在防止拒绝服务攻击)的连续数字时,会引发 TypeError,因为 ValueError 被误判为'不是数字'。修复方案是仅对 re.split 识别出的数字部分进行转换,对于超长数字串返回 float('inf');文中还指出,基于 isdigit() 的方法会因 Unicode 与 \d 正则表达式之间的不一致而失效。后续更新介绍了 Jeff Epler 提出的更巧妙的替代方案,包括将连续数字按十六进制解析(十六进制没有长度限制且排序结果相同),或使用(长度, 值)元组,不过作者尚未对它们进行基准测试。

“Human sorting” is sorting strings (usually file names) so that any numbers in the names come out in the right order. For example, “file10.txt” should come after “file2.txt”, but regular sorting will put them in the wrong order. I’ve written twice before about the function to do this (Human sorting and Human sorting improved), but there’s more to say about it.

One effect of AI these days is bug reports for obscure edge cases. As a maintainer, it’s been a challenge first to read through them all, and then to decide which ones deserve a fix. It’s tempting to fix every little thing no matter how unlikely it is to happen in the real world. But every fix costs time, energy and focus. Every fix comes with risks in the form of new bugs or changed behavior. So some long-tail issues should be marked as “won’t fix.”

But this one intrigued me: if a file name includes a numeric component with 4301 digits, then the human sorting function fails. Buh?

继续阅读全文

为了尊重作者,这里只展示开头几段。你可以去原文阅读,或者在 HorizonReader 里读:划线做笔记、让 AI 帮忙。

更多来自 Ned Batchelder

Cupcake party

AI 摘要To celebrate Nat's 37th birthday, the family hosted a cupcake-baking party at his group home, bringing box mixes, sugar-free options for his diabetic housemate, and decorating supplies so all the housemates could join in. Everyone wore aprons and chef's hats, helped mix and bake, and decorated cupcakes—surprisingly all choosing vanilla icing—though the family forgot candles and singing 'Happy Birthday.' Reflecting on the day, the author expresses gratitude that Nat has found a warm living situation where the housemates feel like family and the staff like friends.

Evergreen posts

AI 摘要The author describes blog features they built: since old technical posts may contain outdated details, posts older than five years automatically display human-style age descriptions like 'nearly 12 years' followed by a 'Be careful' warning. Posts that never go stale can be marked 'evergreen,' replacing the warning with 'but it's still good,' which the author recently made customizable per post. They conclude that owning their own site lets them create small, seemingly pointless features that bring them joy.

Making git hooks visible

AI 摘要Inspired by an attack where a Dropbox-shared git checkout carried active malicious hooks, the author built zsh chpwd functions to make git hooks visible when entering a repo. The first function announces any executable, non-sample hooks in a repo's hooks directory (once per repo to avoid noise), and a second warns when a .pre-commit-config.yaml exists but the pre-commit hook isn't installed. Together with a startup check for the initial directory, these functions keep the author aware of active hooks and caught at least one project where pre-commit had been forgotten.

Expert asterisks

AI 摘要The article describes the 'Expert Asterisk,' a common pattern in online help discussions where experts assisting beginners add obscure, rarely-used details and tangents that distract learners from their original questions. Using a git help exchange as an example, the author shows how such additions—while well-intentioned and potentially useful to advanced users—can derail a novice who lacks the context to sift important information from esoteric footnotes. The author advises experts to resist the urge to add tangents, instead focusing on understanding the beginner's actual situation and mindset so their help can truly be heard.

Silence is golden lightning talk

AI 摘要The author describes their 'Silence is Golden' lightning talk at PyCon US 2026, whose message is to leave quiet time in discussions so reluctant speakers have a chance to participate. As someone who speaks easily, the author admits they must consciously remind themselves to hold back and create space for others. They were pleased to extend the well-known Pac-Man Rule from space to time, giving the talk an engaging hook rather than just a lecture.