Sponge

Sept. 16, 2015, 10:50 p.m.

sponge is amazing. moreutils in general is a brilliant idea, even if most of its tools can be simulated by scripting (or is it the other way around?).

Python unicode

June 23, 2015, 12:56 a.m.

Ned Batchelder's Pragmatic Unicode talk has everything a Python programmer needs to know about dealing with unicode.

His unicode sandwich is worth repeating here. Just remember:

str -> decode -> unicode -> encode -> str #python 2
bytes -> decode -> str -> encode -> bytes #python 3

Unpacking nothing in Python

June 10, 2015, 6:56 p.m.

In his presentation on concurrency during PyCon, David Beazley did this:

can_recv, can_send, [] = select(recv_wait, send_wait, [])

He meant to put _ where the first [] is. Essentially, he invoked this:

[] = []

This works! Thinking about this in terms of unpacking, it sort of makes sense. Since the left-hand side has no identifiers to unpack to, the right-hand side should have no values to unpack.

[] = [1] # ValueError: too many values to unpack (expected 0)
[x] = [] # ValueError: need more than 0 values to unpack

Since unpacking works with iterables, naturally these are legal as well:

[] = ()
[] = {}
[] = range(0)
...

But not this:

() = () # SyntaxError

In tuple unpacking, the outer parentheses in the left-hand side are redundant. Maybe this is treated as if there is nothing on the left-hand side?

Python fdopen bug

June 8, 2015, 11:09 p.m.

A cute bug in the Python REPL that somebody posted in #python the other day:

import os
os.fdopen(0); 0

Typing this inside the REPL exits it. A file wrapper object is constructed for fd 0 (stdin), but the reference is not saved. The interpreter then frees the object, which probably accidentally closes the actual stdin as well.

Python global interpreter lock

May 4, 2015, 12:08 a.m.

CPython employs a global interpreter lock. In short, multithreading in a single Python process will not speed up computations, even on a multicore machine (and in fact, it will even slow them down!).

Non-CPU-bound workload still benefits (e.g. managing multiple sockets). But for CPU-bound workload, go for multiprocessing.



newer << Page 3/6 >> older