2.20.2008

python: reference anonymous list created by list comprehension

When using a list comprehension, the list created under the hood can be referred to with locals()['_[1]']. In the case of nested elements, the elements are named _[2], _[3], etc, as you delve deeper into the nested items. The 'anonymous' list exists only while the list comprehension executes,ie. only during the work between '[' and ']'. It's sometimes handy to be able to refer to this list for in-line processing during the list comprehension. Here's an easy example. Let's say I wanted to join three lists together, removing any duplicate elements:

>>> L1 = [ 1,2,3 ]
>>> L2 = [ 2,3,4 ]
>>> L3 = [ 2,4,5 ]
>>> [ x for x in L1 + L2 + L3 if x not in locals()['_[1]'] ]
[1, 2, 3, 4, 5]

Now, the 'gotcha' is that locals()['_[n]'] is not just incremented for each nested list comprehension, it's incremented whenever a list comprehension is used in the local scope. So, if you've executed four list comprehensions previously in the run of the local scope, your next one is referenced as locals()['_[5]']. This is most evident within a loop:
>>> for x in [1,2,3]:
... [ x for x in l1 + l2 + l3 if x not in locals()['_[1]'] ] #this will work
... [ x for x in l1 + l2 + l3 if x not in locals()['_[1]'] ] #this key no longer exists
...
[1, 2, 3, 4, 5]
Traceback (most recent call last):
File "", line 3, in
KeyError: '_[1]'

>>> for x in [1,2]:
... [ x for x in l1 + l2 + l3 if x not in locals()['_[1]'] ]
... [ x for x in l1 + l2 + l3 if x not in locals()['_[2]'] ] #here's the increment
...
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]




2.06.2008

Prettying up my ion3 desktop

Every once in a while, I get a wild hair to change up my desktop. Every time I've played with an xterm replacement that supports transparency, I generally get annoyed within hours, being too lazy to actually spend any time tweaking it. For whatever reason, today I bit the bullet, and took a couple of minutes set up aterm to be useable. There was actually no excuse for my earlier laziness - all my 'problems' were settled with just a few entries to ~/.Xdefaults:

! For aterms:
Aterm*loginShell: true
Aterm*saveLines: 2000
Aterm*foreground: DeepSkyBlue
Aterm*transparent: true
Aterm*font: fixed
Aterm*scrollBar: false
Aterm*backspacekey: ^H
Aterm*borderless: true
Aterm*internalBorder: 0
Aterm*termName: xterm
Aterm*fading: 80
Aterm*shading: 50
These take care of the annoyances (such as the character mapping for my backspace key changing within a vi session).
Then, enabling transparency in empty frames for ion3 brings back the old school behavior (well ion2 old school =]):

In your styles look_.lua:

de.defstyle("frame", {
background_colour = "#000000",
--transparent_background = false,
transparent_background = true,
})

Then, what the heck, I dl'd some darker gtk-2 themes, so that pidgin, gqview, firefox, etc would be happy.

That brought me to this place, and nothing annoys me yet. No guarantees about next week. =]