How to handle exceptions
Earlier today, I saw two different StackOverflow questions that basically looked like this:
Why does this not work? try: [broken code] except: print('[-] exception occurred') Unless someone can read minds or gets lucky or puts a whole lot more work into your question than you have any right to exp
Three ways to read files
There are three commonly useful ways to read files: Read the whole thing into memory, iterate them element by element (usually meaning lines), or iterate them in chunks.
2Lazy Python lists
Code for this post can be found at https://github.com/abarnert/lazylist.
The same discussion that brought up lazy tuple unpacking also raised the idea of implementing a full lazy list class in Python.
This is easy to do, but isn't as useful as it sounds at first glance.
Lazy cons lists
This post is meant as background to the following post on lazy Python lists. If you already know all about cons lists, triggers, lazy evaluation, tail sharing, etc., you don't need to read it. Linked lists
What Python calls "list" is actually a dynamic array.
Lazy tuple unpacking
Lazy tuple unpacking
In a recent discussion on python-ideas, Paul Tagliamonte suggested that tuple unpacking could be lazy, using iterators.
Getting atomic writes right
Something that comes up all the time—and not just in Python—is how to write a file atomically. And the solutions given are usually wrong.
tl;dr
You just want some code that makes it easy to do atomic writes? Try fatomic.
Suites, scopes, and lifetimes
To my post Why Python doesn't need blocks, julien tayon replied with a comment about a completely different meaning of blocks, which I think leads to some interesting points, even if it's irrelevant to that post.
1Swift-style map and filter views
Along the way to an attempt to port itertools from Python to Apple's new language Swift (see my Stupid Swift Ideas blog), I discovered something interesting: Swift's map and filter functions are in some ways better than Python's.
1Inline (bytecode) assembly
In a recent discussion on adding an empty set literal to Python, the conversion went as far off-base as usual, and I offhandedly suggested:
Alternatively, it might be nice if there were a way to do "inline bytecode assembly" in CPython, similar to the way you do inline assembly in many C compilers,