A Pox on the One-Liner

Dealing with bad code is something every developer has to do at some point. It's painful and tedious, but it can ultimately result in a cathartic sense of accomplishment. Liberating as it may be, the process itself is difficult, and can seem impossible at times. With this post, I hope to show that even the most apparently intractable code still can be changed into something more managable. Sure, the example is far worse than anything you'll be expected to fix (let's hope), but what the hell, let's try learning from it. Along the way you might learn a thing or two about Python and some of its lesser-known components; I know I will while I write it. Let's start with the lore behind this particular piece of horrifying code.

A long time ago in the #python IRC channel, there was a rash of people asking about writing some of the experienced Python developer's most hated things: one-liners. I wasn't there, mind you, but I get the feeling it became so irritating to the regulars that something had to be done to "dismay and shock" those visitors, as one of them put it. Habnabit wrote this thing to do just that, and it passes with flying colors; it has that effect on nearly everyone who sees it for the first time.

Side note: since the changes to this code necessarily affect large portions, I'm using diffs to highlight the changes. Still, some of the changesets are quite large, so I dumped the source files into a Github repo in case you'd like to review them in your own way.

Let's take a look at the original monstrosity first, it will help us appreciate how hopeless the refactoring process seems at first.

Ocular Bloodletting

main = lambda port:  (lambda dt: (lambda mm:  (lambda n: (map(lambda r:  (lambda
    rr: setattr(n, *rr) if (type(rr) is tuple and len(rr) == 2) else None)(r()),
    [lambda: map(setattr, *zip(*[(n, m, __import__(m)) for m in mm.m.decode('ba'
    'se64').split()])), lambda: map(n.s['signal.signal'], (n.s['signal.SIGINT'],
    n.s['signal.SIGTERM']),  [lambda s, f: (n.s['sys.exit']() if n.f else [n.sa(
    mm.l[0], n.o)]  and n.u('f', True)  or n.fc(n.l))] * 2), lambda: setattr(mm,
    'l', mm.l.decode('base64').split('~~~')), lambda: ('sw', n.s['types.Functio'
    'nType'] (compile("try:\n\tv = n.select.select(n.so, n.w(), [])\nexcept n.s"
    "elect.error, e:\n\tif e[0] != n.errno.EINTR: raise\nelse:\n\tn.u('sr', v)",
    '', 'exec'), dict(n=n, OSError=OSError))),lambda: ('l', n.s['socket.socket']
    (n.s['socket.AF_INET'], n.s['socket.SOCK_STREAM'])), lambda: n.s['l.bind']((
    '', port)), lambda: n.s['l.listen'](5), lambda: ('ro', lambda: filter(lambda
    s: s in n.nn,  n.so)),  lambda: n.update(si=(lambda o: o.__setitem__),  di=(
    lambda o: o.__delitem__),  cr=n.s['re.compile'](mm.l[11]), nn={},  dp=lambda
    s, d, c, l: n.dd.get(c, d)(s, l, c), dd=dict(me=lambda s, l, c: n.sa(mm.l[14
    ] % (n.nn[s], l)), quit=lambda s, l, c: n.c(s),  who=lambda s, l, c: n.ws(s,
    mm.l[1] + ', '.join(n.s['nn.values']())), help=lambda s, l, c: n.ws(s, mm.l[
    2]), nick=lambda s, l, c: ((([n.sa(mm.l[3] % (n.nn[s], l))] and n.si(n.nn)(
    s, l))  if n.nr.match(l)  else  n.ws(s, mm.l[4]))  if l not in n.nn.values()
    else n.ws(s, mm.l[7]))), so=(n.u('f', False) or [n.l]), ib=n.s['collections'
    '.defaultdict'](list),  ob=n.s['collections.defaultdict'](list),  o=(lambda:
    filter(lambda s:  s is not n.l, n.so)),  w=(lambda: filter(n.ob.__getitem__,
    n.o())), ws=(lambda s, l: n.ob[s].append(l + '\r\n')), nr=n.s['re.compile'](
    mm.l[12]),  sa=(lambda d, f=n.ro: map(lambda s: n.ws(s, d), f())), fs=set(),
    c=(lambda s: [n.sa(mm.l[13] % n.nn[s])  if s in n.nn and s not in n.fs  else
    None] and n.s['fs.add'](s)),  fc=(lambda s:  [s.close()] and (n.so.remove(s)
    if s in n.so else None) or (n.fs.remove(s) if s in n.fs else None) or (n.di(
    n.ib)(s) if s in n.ib else None)  or  (n.di(n.ob)(s) if s in n.ob else None)
    or (n.di(n.nn)(s)  if s in n.nn else  None))),  lambda:  map(lambda f:  map(
    apply, [lambda: n.u('sr', ([], [], [])),  lambda: n.sw(), lambda: map(apply,
    [lambda *ss: map(lambda s: map(apply,  [lambda: n.sa(mm.l[8]), lambda: n.so.
    append(s.accept()[0]),  lambda: n.ws(n.so[-1], mm.l[6])])  if s is n.l  else
    map(apply,  [lambda: n.ib[s].append(s.recv(4096)) or  (n.c(s) if n.ib[s][-1]
    == '' else None)]), ss), lambda *ss: map(lambda s: (lambda d: (n.si(n.ob)(s,
    [d[s.send(d):]])  or  (n.si(n.ob)(s, []) if n.ob[s] == [''] else None)))(''.
    join(n.ob[s])), ss),  lambda *ss: None], n.sr), lambda: [n.di(v)(slice(None,
    None)) for k, v in n.ob.items() if v and not filter(None, v)],  lambda: n.u(
    'sl', {}), lambda: map((lambda (k, v): n.si(n.sl)(k, ''.join(v).split('\r\n'
    )) or n.si(n.ib)(k, [n.sl[k].pop()])), n.ib.items()),  lambda: n.sl and map(
    lambda (s, l): ((n.sa(mm.l[15] %  (n.nn[s], l[1:] if l.startswith('//') else
    l))  if not l.startswith('/')  or l.startswith('//') else (n.dp(s, lambda s,
    l, c:  n.ws(s, mm.l[9] % c),  *n.s['cr.match'](l).groups())))  if  s in n.nn
    else (((n.si(n.nn)(s, l) or [n.ws(s, mm.l[5])] and n.sa(mm.l[10] % l)) if n.
    nr.match(l)  else n.ws(s, mm.l[4]))  if l not in n.nn.values()  else n.ws(s,
    mm.l[7]))),  [(s, l.rstrip())  for s, ll  in  n.sl.items()  for l  in  ll]),
    lambda: n.f and map(lambda (s, b): n.fc(s) if not filter(None, b) else None,
    n.ob.items()), lambda:  map(lambda s:  n.fc(s) if not filter(None,  n.ob[s])
    else None, list(n.fs))]), iter(lambda: bool(n.so), False)), lambda: n.s['l.'
    'close']()])))(dt()))(dt(m='c3lzIHNpZ25hbCBzb2NrZXQgc2VsZWN0IGNvbGxlY3Rpb25'
    'zIGVycm5vIHR5cGVzIGl0ZXJ0b29scyByZQ==', l='KioqIFNlcnZlciBnb2luZyBkb3duIX5'
    '+fioqKiBDdXJyZW50bHkgY29ubmVjdGVkOiB+fn4qKiogQXZhaWxhYmxlIGNvbW1hbmRzOg0KK'
    'ioqICAvaGVscCAtLSBnZXQgaGVscA0KKioqICAvcXVpdCAtLSBkaXNjb25uZWN0DQoqKiogIC9'
    'tZSAtLSBwZXJmb3JtIGFuIGFjdGlvbg0KKioqICAvd2hvIC0tIGxpc3QgY29ubmVjdGVkIHVzZ'
    'XJzDQoqKiogIC9uaWNrIC0tIGNoYW5nZSB5b3VyIG5pY2tuYW1lIHRvIHNvbWV0aGluZyBiZXR'
    '0ZXJ+fn4qKiogJXMgaXMgbm93IGtub3duIGFzICVzLn5+fioqKiBUaGF0IG5pY2tuYW1lIGlzI'
    'GludmFsaWQufn5+KioqIFR5cGUgIi9oZWxwIiBmb3IgaGVscC5+fn4qKiogSGVsbG8hIFdoYXQ'
    'gaXMgeW91ciBuaWNrbmFtZT9+fn4qKiogVGhhdCBuaWNrbmFtZSBpcyBhbHJlYWR5IGluIHVzZ'
    'S5+fn4qKiogSW5jb21pbmcgY29ubmVjdGlvbiF+fn4qKiogTm8gc3VjaCBjb21tYW5kOiAvJXN'
    '+fn4qKiogJXMgaGFzIGpvaW5lZC5+fn5eLyhbQS16XSopXHMqKC4qKSR+fn5eW0EtejAtOV9dK'
    'yR+fn4qKiogJXMgaGFzIGxlZnQufn5+KiAlcyAlc35+fjwlcz4gJXM=')))(type('', (dict,
    ), dict(__getattr__=lambda s, k:  s[k],  u=lambda s, *a:  s.__setitem__(*a),
    __setattr__=lambda s, k, v: s.__setitem__(k, v),  s=property(lambda s: type(
    '', (object,),  dict(__getitem__=lambda ss, k: reduce(getattr, k.split('.'),
    s)))()))))

Clearly this is madness. Without looking at the description in the gist, there's not even a hint at what it does, except that the main function seems to take an argument named port. Or does it? Regardless of what we think it does, there's not really a way of actually knowing at first glance, or for that matter even after an hour or two of careful study. Is it malware? Is it useful? Does it do anything at all? The answers are respectively no, maybe, and yes, and it turns out to be a simple chat server. Of course, I had no idea about the answers to these questions before I ran it for the first time, but since I trust Habnabit to not infect my computer, I wasn't too worried about the malware part.

To run it, drop the code into, say, chatserver.py and run it with python -c 'from chatserver import main; main(6667)'. Telnet to localhost on port 6667, and you'll be greeted with the following:

*** Hello! What is your nickname?
cdunklau
*** Type "/help" for help.
*** cdunklau has joined.
Hi there!
<cdunklau> Hi there!
/help
*** Available commands:
***  /help -- get help
***  /quit -- disconnect
***  /me -- perform an action
***  /who -- list connected users
***  /nick -- change your nickname to something better
/me waves
* cdunklau waves
/who
*** Currently connected: cdunklau
/nick colin
*** cdunklau is now known as colin.
/nick cdunklau
*** colin is now known as cdunklau.
/bogus
*** No such command: /bogus
/quit
*** cdunklau has left.
Connection closed by foreign host.

Pretty simple, but also pretty amazing, since the entire program is implemented with a single python expression. Try it out with multiple telnet connections, it does in fact work with more than one user (it wouldn't be much use otherwise, right?).

Indent for Readability! Well, maybe...

We certainly can't get anywhere with the code in its current state. I started with reindenting it manually because (1) I was bored, and (2) I was curious if I could do it without screwing it up. Apparently my inner Python grammar is decent, because much to my surprise I was able to do it with only a couple easily-fixable syntax errors (and a little help from my editor's parenthesis-matching engine). Here's the result:

main = lambda port: (
    lambda dt: (
        lambda mm: (
            lambda n: (
                map(
                    lambda r:  (
                        lambda rr: (
                            setattr(n, *rr)
                            if (type(rr) is tuple and len(rr) == 2)
                            else None
                        )
                    )(r()),
                    [
                        lambda: map(
                            setattr,
                            *zip(
                                *[
                                    (n, m, __import__(m))
                                    for m
                                    in mm.m.decode('ba' 'se64').split()
                                ]
                            )
                        ),
                        lambda: map(
                            n.s['signal.signal'],
                            (n.s['signal.SIGINT'], n.s['signal.SIGTERM']),
                            [
                                lambda s, f: (
                                    n.s['sys.exit']()
                                    if n.f
                                    else (
                                        [n.sa(mm.l[0], n.o)] and
                                        n.u('f', True) or
                                        n.fc(n.l)
                                    )
                                )
                            ] * 2
                        ),
                        lambda: setattr(
                            mm, 'l', mm.l.decode('base64').split('~~~')
                        ),
                        lambda: (
                            'sw',
                            n.s['types.Functio' 'nType'](
                                compile(
                                    "try:\n"
                                    "\tv = n.select.select(n.so, n.w(), [])\n"
                                    "except n.select.error, e:\n"
                                    "\tif e[0] != n.errno.EINTR: raise\n"
                                    "else:\n"
                                    "\tn.u('sr', v)",
                                    '',
                                    'exec'
                                ),
                                dict(n=n, OSError=OSError)
                            )
                        ),
                        lambda: (
                            'l',
                            n.s['socket.socket'](
                                n.s['socket.AF_INET'],
                                n.s['socket.SOCK_STREAM']
                            )
                        ),
                        lambda: n.s['l.bind'](('', port)),
                        lambda: n.s['l.listen'](5),
                        lambda: (
                            'ro', lambda: filter(lambda s: s in n.nn,  n.so)
                        ),
                        lambda: n.update(
                            si=(lambda o: o.__setitem__),
                            di=(lambda o: o.__delitem__),
                            cr=n.s['re.compile'](mm.l[11]),
                            nn={},
                            dp=lambda s, d, c, l: n.dd.get(c, d)(s, l, c),
                            dd=dict(
                                me=lambda s, l, c: (
                                    n.sa(mm.l[14] % (n.nn[s], l))
                                ),
                                quit=lambda s, l, c: n.c(s),
                                who=lambda s, l, c: n.ws(
                                    s, mm.l[1] + ', '.join(n.s['nn.values']())
                                ),
                                help=lambda s, l, c: n.ws(s, mm.l[2]),
                                nick=lambda s, l, c: (
                                    (
                                        (
                                            [n.sa(mm.l[3] % (n.nn[s], l))] and
                                            n.si(n.nn)(s, l)
                                        )
                                        if n.nr.match(l)
                                        else n.ws(s, mm.l[4])
                                    )
                                    if l not in n.nn.values()
                                    else n.ws(s, mm.l[7])
                                )
                            ),
                            so=(n.u('f', False) or [n.l]),
                            ib=n.s['collections' '.defaultdict'](list),
                            ob=n.s['collections.defaultdict'](list),
                            o=(lambda: filter(lambda s:  s is not n.l, n.so)),
                            w=(lambda: filter(n.ob.__getitem__, n.o())),
                            ws=(lambda s, l: n.ob[s].append(l + '\r\n')),
                            nr=n.s['re.compile'](mm.l[12]),
                            sa=(
                                lambda d, f=n.ro: map(
                                    lambda s: n.ws(s, d),
                                    f()
                                )
                            ),
                            fs=set(),
                            c=(
                                lambda s: [
                                    n.sa(mm.l[13] % n.nn[s])
                                    if s in n.nn and s not in n.fs
                                    else None
                                ] and n.s['fs.add'](s)
                            ),
                            fc=(
                                lambda s: (
                                    [s.close()] and
                                    (n.so.remove(s) if s in n.so else None) or
                                    (n.fs.remove(s) if s in n.fs else None) or
                                    (n.di(n.ib)(s) if s in n.ib else None) or
                                    (n.di(n.ob)(s) if s in n.ob else None) or
                                    (n.di(n.nn)(s) if s in n.nn else None)
                                )
                            )
                        ),
                        lambda:  map(
                            lambda f:  map(
                                apply,
                                [
                                    lambda: n.u('sr', ([], [], [])),
                                    lambda: n.sw(),
                                    lambda: map(
                                        apply,
                                        [
                                            lambda *ss: map(
                                                lambda s: map(
                                                    apply,
                                                    [
                                                        lambda: (
                                                            n.sa(mm.l[8])
                                                        ),
                                                        lambda: (
                                                            n.so.append(
                                                                s.accept()[0]
                                                            )
                                                        ),
                                                        lambda: n.ws(
                                                            n.so[-1],
                                                            mm.l[6]
                                                        )
                                                    ]
                                                ) if s is n.l else map(
                                                    apply,
                                                    [
                                                        lambda: (
                                                            n.ib[s].append(
                                                                s.recv(4096)
                                                            ) or
                                                            (
                                                                n.c(s)
                                                                if (
                                                                    n.ib[s][-1]
                                                                    == ''
                                                                )
                                                                else None
                                                            )
                                                        )
                                                    ]
                                                ),
                                                ss
                                            ),
                                            lambda *ss: map(
                                                lambda s: (
                                                    lambda d: (
                                                        n.si(n.ob)(
                                                            s,
                                                            [d[s.send(d):]]
                                                        ) or
                                                        (
                                                            n.si(n.ob)(s, [])
                                                            if n.ob[s] == ['']
                                                            else None
                                                        )
                                                    )
                                                )(''.  join(n.ob[s])), ss),
                                            lambda *ss: None
                                        ],
                                        n.sr
                                    ),
                                    lambda: [
                                        n.di(v)(slice(None, None))
                                        for k, v
                                        in n.ob.items()
                                        if v and not filter(None, v)
                                    ],
                                    lambda: n.u('sl', {}),
                                    lambda: map(
                                        (
                                            lambda (k, v): (
                                                n.si(n.sl)(
                                                    k,
                                                    ''.join(v).split('\r\n')
                                                ) or n.si(n.ib)(
                                                    k,
                                                    [n.sl[k].pop()]
                                                )
                                            )
                                        ),
                                        n.ib.items()
                                    ),
                                    lambda: n.sl and map(
                                        lambda (s, l): (
                                            (
                                                n.sa(
                                                    mm.l[15] % (
                                                        n.nn[s],
                                                        l[1:]
                                                        if l.startswith('//')
                                                        else l
                                                    )
                                                )
                                                if (
                                                    not l.startswith('/') or
                                                    l.startswith('//')
                                                )
                                                else (
                                                    n.dp(
                                                        s,
                                                        lambda s, l, c: (
                                                            n.ws(
                                                                s,
                                                                mm.l[9] % c
                                                            )
                                                        ),
                                                        *n.s['cr.match'](
                                                            l
                                                        ).groups()
                                                    )
                                                )
                                            )
                                            if s in n.nn
                                            else (
                                                (
                                                    (
                                                        n.si(n.nn)(s, l) or
                                                        [n.ws(s, mm.l[5])] and
                                                        n.sa(mm.l[10] % l)
                                                    )
                                                    if n.nr.match(l)
                                                    else n.ws(s, mm.l[4])
                                                )
                                                if l not in n.nn.values()
                                                else n.ws(s, mm.l[7])
                                            )
                                        ),
                                        [
                                            (s, l.rstrip())
                                            for s, ll
                                            in n.sl.items()
                                            for l
                                            in ll
                                        ]
                                    ),
                                    lambda: (
                                        n.f and
                                        map(
                                            lambda (s, b): (
                                                n.fc(s)
                                                if not filter(None, b)
                                                else None
                                            ),
                                            n.ob.items()
                                        )
                                    ),
                                    lambda: map(
                                        lambda s: (
                                            n.fc(s)
                                            if not filter(None,  n.ob[s])
                                            else None
                                        ),
                                        list(n.fs)
                                    )
                                ]
                            ),
                            iter(lambda: bool(n.so), False)
                        ),
                        lambda: n.s['l.' 'close']()
                    ]
                )
            )
        )(dt())
    )(
        dt(
            m='c3lzIHNpZ25hbCBzb2NrZXQgc2VsZWN0IGNvbGxlY3Rpb25zIGVycm5vIHR5cGV'
            'zIGl0ZXJ0b29scyByZQ==',
            l='KioqIFNlcnZlciBnb2luZyBkb3duIX5+fioqKiBDdXJyZW50bHkgY29ubmVjdGV'
            'kOiB+fn4qKiogQXZhaWxhYmxlIGNvbW1hbmRzOg0KKioqICAvaGVscCAtLSBnZXQg'
            'aGVscA0KKioqICAvcXVpdCAtLSBkaXNjb25uZWN0DQoqKiogIC9tZSAtLSBwZXJmb'
            '3JtIGFuIGFjdGlvbg0KKioqICAvd2hvIC0tIGxpc3QgY29ubmVjdGVkIHVzZXJzDQ'
            'oqKiogIC9uaWNrIC0tIGNoYW5nZSB5b3VyIG5pY2tuYW1lIHRvIHNvbWV0aGluZyB'
            'iZXR0ZXJ+fn4qKiogJXMgaXMgbm93IGtub3duIGFzICVzLn5+fioqKiBUaGF0IG5p'
            'Y2tuYW1lIGlzIGludmFsaWQufn5+KioqIFR5cGUgIi9oZWxwIiBmb3IgaGVscC5+f'
            'n4qKiogSGVsbG8hIFdoYXQgaXMgeW91ciBuaWNrbmFtZT9+fn4qKiogVGhhdCBuaW'
            'NrbmFtZSBpcyBhbHJlYWR5IGluIHVzZS5+fn4qKiogSW5jb21pbmcgY29ubmVjdGl'
            'vbiF+fn4qKiogTm8gc3VjaCBjb21tYW5kOiAvJXN+fn4qKiogJXMgaGFzIGpvaW5l'
            'ZC5+fn5eLyhbQS16XSopXHMqKC4qKSR+fn5eW0EtejAtOV9dKyR+fn4qKiogJXMga'
            'GFzIGxlZnQufn5+KiAlcyAlc35+fjwlcz4gJXM='
        )
    )
)(
    type(
        '',
        (dict,),
        dict(
            __getattr__=lambda s, k:  s[k],
            u=lambda s, *a:  s.__setitem__(*a),
            __setattr__=lambda s, k, v: s.__setitem__(k, v),
            s=property(
                lambda s: type(
                    '',
                    (object,),
                    dict(
                        __getitem__=lambda ss, k: reduce(
                            getattr,
                            k.split('.'),
                            s
                        )
                    )
                )()
            )
        )
    )
)

I cleaned up the PEP 8 whitespace errors with the help of the lovely little Flake8 utility (which you should be using if you aren't already). Amazingly, I was able to fit it all in the 79 character limit with only a little bit of awkwardness in the deeper levels.

Sure, it's "readable" now, but just indenting properly isn't going to help us understand what's actually happening. You might think it's time to start refactoring stuff into properly named functions and separating concerns in a sane manner. Well, that's wrong. As I was told a while ago, "You can't refactor without tests. There's a better term for refactoring without tests, and that's 'changing shit'".

Untested Code is Broken Code

This aphorism has served me very well, even in the short time that I've had a grasp on writing tests for my code. I should have written some tests before changing a single line, but I didn't get the idea for this post until the reindent process was mostly complete. Still, in order to change this into something more managable, I'll need at least a minimal functional test suite.

I started off with vanilla unittest and tried mucking around with subprocess and multiprocessing, but quickly realized the simpler way to do this: Twisted. By combining the subprocess support and elegant abstractions that Twisted provideds, I can write a test suite with a reasonable effort, and hopefully only a few WTFs.

Since I'm not exceptionally familiar with Twisted, this part was really tricky at first. After several misguided starts, I figured out how to write a state machine that would keep track of received messages, and send to-transmit messages when necessary. The comments go into some detail about how it all works.

"""
Functional tests for chatserver refactor.

Run it with trial, with the order=toptobottom option::

    trial test_chatserver

Assumes chatserver is running on localhost, port 6667. To specify the
name of the chatserver module at runtime (from inside the chatservers
directory)::

    python -c 'import sys; __import__(sys.argv[1][:-3]).main(6667)' custom.py

"""
import re
from collections import namedtuple

from twisted.internet import reactor, defer
from twisted.internet.protocol import Factory
from twisted.internet.endpoints import TCP4ClientEndpoint
from twisted.internet.task import deferLater
from twisted.protocols.basic import LineReceiver
from twisted.trial.unittest import TestCase


# Example session

# *** Hello! What is your nickname?
# cdunklau
# *** Type "/help" for help.
# *** cdunklau has joined.
# Hi there!
# <cdunklau> Hi there!
# /help
# *** Available commands:
# ***  /help -- get help
# ***  /quit -- disconnect
# ***  /me -- perform an action
# ***  /who -- list connected users
# ***  /nick -- change your nickname to something better
# /me waves
# * cdunklau waves
# /who
# *** Currently connected: cdunklau
# /nick colin
# *** cdunklau is now known as colin.
# /nick cdunklau
# *** colin is now known as cdunklau.
# /bogus
# *** No such command: /bogus
# /quit
# *** cdunklau has left.
# Connection closed by foreign host.


CHAT_LINE_REGEX = re.compile(r'<([a-z0-9_]+)> (.*)$', re.I)
MESSAGE_LINE_REGEX = re.compile(r'\*\*\* (.*)$')
ACTION_LINE_REGEX = re.compile(r'\* ([a-z0-9_]+) (.*)$', re.I)

NICK_CHANGED_REGEX = re.compile(
    r'([a-z0-9_]+) is now known as ([a-z0-9_]+).',
    re.I
)


class ChatserverTestCase(TestCase):
    def deferExpectedMessages(self, messages, timeout=0.5):
        """
        Connect a StateMachineClient to the chat server on localhost,
        port 6667, and allow it to run for up to ``timeout`` seconds.

        ``messages`` is a list of ``Tx`` or ``Rx`` instances
        representing the messages to send and the messages expected
        from the server, in order.

        """
        endpoint = TCP4ClientEndpoint(reactor, 'localhost', 6667)
        d = endpoint.connect(SimpleFactory(StateMachineClient, messages, self))

        # This part gets a little hairy. In order to shut down the protocol's
        # connection, reactor.callLater needs to access the protocol, so I
        # have to do it in a callback. Then I also need to access the
        # delayed call object, so I need to set up a callback inside another
        # applied to the same Deferred.
        # This Deferred will fire when the client successfully connects to
        # the server, so the timeout will begin from that point.
        @d.addCallback
        def cbSetTimeout(proto):
            delayed = reactor.callLater(
                timeout,
                proto.transport.loseConnection
            )

            # I need to make sure the delayed timeout call gets cancelled
            # so it doesn't call loseConnection twice (it might be called from
            # within the protocol).
            @d.addBoth
            def cbCancelTimeout(passthrough):
                if delayed.active():
                    delayed.cancel()
                return passthrough

            # Returning the state machine's Deferred from this callback
            # implicitly "chains" it onto the deferred from endpoint.connect...
            return proto.stateMachine.deferred

        # ...so I can return the Deferred from endpoint.connect directly, and
        # trial will wait until it fires to record success or failure.
        return d

    def test_login_and_quit(self):
        messages = sessionMessages('logintest', [])
        return self.deferExpectedMessages(messages)

    def test_chat(self):
        messages = sessionMessages(
            'chattest',
            [Tx('Hello there!'), Rx('<chattest> Hello there!')]
        )
        return self.deferExpectedMessages(messages)

    def test_help(self):
        messages = sessionMessages(
            'helptest',
            [
                Tx('/help'),
                Rx('*** Available commands:'),
                Rx('***  /help -- get help'),
                Rx('***  /quit -- disconnect'),
                Rx('***  /me -- perform an action'),
                Rx('***  /who -- list connected users'),
                Rx('***  /nick -- change your nickname to something better'),
            ]
        )
        return self.deferExpectedMessages(messages)

    def test_emote(self):
        messages = sessionMessages(
            'emotetest',
            [Tx('/me is a teapot'), Rx('* emotetest is a teapot')]
        )
        return self.deferExpectedMessages(messages)

    def test_simple_who(self):
        messages = sessionMessages(
            'whotest',
            [Tx('/who'), Rx('*** Currently connected: whotest')]
        )
        return self.deferExpectedMessages(messages)

    def test_nick(self):
        messages = sessionMessages(
            'nicktest',
            [
                Tx('/nick newnick'),
                Rx('*** nicktest is now known as newnick.'),
                Tx('do you like my new name?'),
                Rx('<newnick> do you like my new name?'),
            ],
            'newnick',
        )
        return self.deferExpectedMessages(messages)

    def test_bogus_command_error(self):
        messages = sessionMessages(
            'bogustest',
            [Tx('/bogus'), Rx('*** No such command: /bogus')]
        )
        return self.deferExpectedMessages(messages)

    def test_invalid_nick(self):
        messages = sessionMessages(
            'badnicktest',
            [Tx('/nick badnick#$%'), Rx('*** That nickname is invalid.')],
        )
        return self.deferExpectedMessages(messages)

    def test_invalid_nick_on_login(self):
        messages = [
            Rx('*** Hello! What is your nickname?'),
            Tx('badnick2#$'),
            Rx('*** That nickname is invalid.'),
            Tx('fixednick'),
            Rx('*** Type "/help" for help.'),
            Rx('*** fixednick has joined.'),
            Tx('/quit'),
            Rx('*** fixednick has left.')
        ]
        return self.deferExpectedMessages(messages)

    def test_chat_broadcast_to_all_users(self):
        aliceMessages = sessionMessages(
            'alice',
            [
                Rx('*** Incoming connection!'),
                Rx('*** bob has joined.'),
                Rx('*** Incoming connection!'),
                Rx('*** charlie has joined.'),
                Rx('<charlie> Hi everyone!'),
                Rx('<charlie> Can you hear me?'),
                Tx('I can hear you, charlie'),
                Rx('<alice> I can hear you, charlie'),
                Rx('<bob> I can hear you too!'),
                Rx('*** charlie has left.'),
                Rx('*** bob has left.'),
            ]
        )
        bobMessages = sessionMessages(
            'bob',
            [
                Rx('*** Incoming connection!'),
                Rx('*** charlie has joined.'),
                Rx('<charlie> Hi everyone!'),
                Rx('<charlie> Can you hear me?'),
                Rx('<alice> I can hear you, charlie'),
                Tx('I can hear you too!'),
                Rx('<bob> I can hear you too!'),
                Rx('*** charlie has left.'),
            ]
        )
        charlieMessages = sessionMessages(
            'charlie',
            [
                Tx('Hi everyone!'),
                Rx('<charlie> Hi everyone!'),
                Tx('Can you hear me?'),
                Rx('<charlie> Can you hear me?'),
                Rx('<alice> I can hear you, charlie'),
                Rx('<bob> I can hear you too!'),
            ]
        )

        # This one is a bit tricky. Three clients involved connect 0.2 seconds
        # apart (alice, bob, then charlie), then charlie says two things,
        # alice responds, then bob responds, then they exit in the opposite
        # order they originally connected.
        aliceDeferred = self.deferExpectedMessages(aliceMessages, 2.0)
        bobDeferredLater = deferLater(
            reactor, 0.2, self.deferExpectedMessages, bobMessages, 2.0)
        aliceDeferred.addCallback(lambda _: bobDeferredLater)
        charlieDeferredLater = deferLater(
            reactor, 0.4, self.deferExpectedMessages, charlieMessages, 2.0)
        aliceDeferred.addCallback(lambda _: charlieDeferredLater)
        return aliceDeferred

    def test_error_nonunique_nick_on_login(self):
        notdebbieMessages = [
            Rx('*** Hello! What is your nickname?'),
            Tx('debbie'),
            Rx('*** That nickname is already in use.'),
            Tx('notdebbie'),
            Rx('*** Type "/help" for help.'),
            Rx('*** notdebbie has joined.'),
            Tx('/quit'),
            Rx('*** notdebbie has left.')
        ]
        debbieMessages = sessionMessages(
            'debbie',
            [
                Rx('*** Incoming connection!'),
                Rx('*** notdebbie has joined.'),
                Rx('*** notdebbie has left.'),
            ]
        )
        debbieDeferred = self.deferExpectedMessages(debbieMessages, 2.0)
        notdebbieDeferredLater = deferLater(
            reactor, 0.2, self.deferExpectedMessages, notdebbieMessages)
        debbieDeferred.addCallback(lambda _: notdebbieDeferredLater)
        return debbieDeferred

    def test_error_nonunique_nick(self):
        wannabeevanMessages = sessionMessages(
            'wannabeevan',
            [Tx('/nick evan'), Rx('*** That nickname is already in use.')]
        )
        evanMessages = sessionMessages(
            'evan',
            [
                Rx('*** Incoming connection!'),
                Rx('*** wannabeevan has joined.'),
                Rx('*** wannabeevan has left.'),
            ]
        )
        evanDeferred = self.deferExpectedMessages(evanMessages, 1.0)
        wannabeevanDeferredLater = deferLater(
            reactor, 0.2, self.deferExpectedMessages, wannabeevanMessages)
        evanDeferred.addCallback(lambda _: wannabeevanDeferredLater)
        return evanDeferred


# Message constructs
class Message(namedtuple('BaseMessage', ['message'])):
    def __repr__(self):
        return '%s(%r)' % (self.__class__.__name__, self.message)


class Tx(Message):
    """Message to transmit"""


class Rx(Message):
    """Message to receive"""


def loginMessages(nick):
    """
    Return a list of Message instances for the initial login process
    for the nickname ``nick``.

    """
    return [
        Rx('*** Hello! What is your nickname?'),
        Tx(nick),
        Rx('*** Type "/help" for help.'),
        Rx('*** %s has joined.' % (nick,)),
    ]


def logoffMessages(nick):
    """
    Return a list of Message instances for the logoff process for
    the nickname ``nick``.

    """
    return [
        Tx('/quit'),
        Rx('*** %s has left.' % (nick,)),
    ]


def sessionMessages(nick, innerMessages, endNick=None):
    """
    Return a list of Message instances for the entire session, including
    login, between login/logoff, and logoff.

    If ``endNick`` is provided, it will be used instead of ``nick`` in
    the call to logoffMessages.

    """
    if endNick is None:
        endNick = nick
    messages = loginMessages(nick)
    messages.extend(innerMessages)
    messages.extend(logoffMessages(endNick))
    return messages


# State Machine and protocol
class SendReceiveStateAsserter(object):
    """
    State machine used by ``StateMachineClient`` to send messages and
    make assertions about received messages.

    The ``deferred`` attribute is used to record the assertions, and is
    chained to the protocol's deferred.

    """
    def __init__(self, messages):
        # stack of Tx/Rx messages
        self.messages = list(reversed(messages))
        self.deferred = defer.Deferred()

    def addAssertion(self, assertionMethod, *args):
        """
        Add a callback to self.deferred that invokes ``assertionMethod``
        with ``*args`` and ignores the input.

        """
        @self.deferred.addCallback
        def cbAssertion(ignored):
            assertionMethod(*args)

    def recordReceived(self, messageString, testInstance):
        """
        Verify that a message was expected to be received, and that it
        fits the expected message content.

        """
        if self.messages:
            if type(self.messages[-1]) is Rx:
                self.addAssertion(
                    testInstance.assertEqual,
                    self.messages.pop().message,
                    messageString
                )
            else:
                # This branch shouldn't be hit, since we flush all the Tx
                # messages off the stack inside transmitNext
                self.addAssertion(
                    testInstance.fail,
                    'Expected to transmit %r next but received %r' % (
                        self.messages[-1].message,
                        messageString
                    )
                )
        else:
            self.addAssertion(
                testInstance.fail,
                'Received %r but expected no more actions' % (messageString,)
            )

    def transmitNext(self, lineProtocol):
        """
        Flush all Tx messages off the end of the stack until an Rx
        message is seen.

        If the stack becomes empty, disconnect from the server.

        """
        while self.messages:
            if type(self.messages[-1]) is Rx:
                break
            lineProtocol.sendLine(self.messages.pop().message)
        else:
            lineProtocol.transport.loseConnection()

    def assertFinished(self, testInstance):
        """
        Finally make sure that the message stack is empty.

        """
        self.addAssertion(testInstance.assertEqual, self.messages, [])


class StateMachineClient(LineReceiver):
    """
    Simple ``LineReceiver`` implementation driven by a
    ``SendReceiveStateAsserter`` state machine.

    The state machine's deferred will be fired when the protocol
    loses connection.

    The ``testInstance`` attribute is the TestCase instance used
    by the state machine to record failures.

    """
    def __init__(self, messages, testInstance):
        """``messages`` is passed directly to the state machine."""
        self.testInstance = testInstance
        self.stateMachine = SendReceiveStateAsserter(messages)

    def connectionMade(self):
        """Flush any messages waiting to be sent."""
        self.stateMachine.transmitNext(self)

    def connectionLost(self, reason):
        """
        Call the final assertion method, and fire the state machine's
        deferred.

        """
        self.stateMachine.assertFinished(self.testInstance)
        self.stateMachine.deferred.callback(None)
        self.stateMachine.deferred = None

    def lineReceived(self, line):
        """
        Tell the state machine about the received message, and flush
        any messages waiting to be sent.

        """
        self.stateMachine.recordReceived(line, self.testInstance)
        self.stateMachine.transmitNext(self)


class SimpleFactory(Factory):
    """
    Passthrough Factory. clientClass is the class of the protocol to
    build, other positional and keyword args are passed to the
    protocol's constructor.

    """
    def __init__(self, clientClass, *args, **kwargs):
        self.clientClass = clientClass
        self.args = args
        self.kwargs = kwargs

    def buildProtocol(self, addr):
        return self.clientClass(*self.args, **self.kwargs)

While this code was really interesting to write, and I learned a lot by writing it, the details are mostly outside this article's scope. Now that we have a test suite to run, let's start diving in. I first ran the test suite against the original, and then against the redented one, and all the tests passed, so far, so good.

Start With What You Know

The first thing that jumped out at me was the big blocks of strings starting at line 49 of the original code. There are two arguments to the dt function, named m and l, and they both sure look like base-64 encoding. Here are the originals, the only change is the indentation and naming.

ORIGINAL_M_BASE64 = (
    'c3lzIHNpZ25hbCBzb2NrZXQgc2VsZWN0IGNvbGxlY3Rpb25'
    'zIGVycm5vIHR5cGVzIGl0ZXJ0b29scyByZQ=='
)
ORIGINAL_L_BASE64 = (
    'KioqIFNlcnZlciBnb2luZyBkb3duIX5'
    '+fioqKiBDdXJyZW50bHkgY29ubmVjdGVkOiB+fn4qKiogQXZhaWxhYmxlIGNvbW1hbmRzOg0KK'
    'ioqICAvaGVscCAtLSBnZXQgaGVscA0KKioqICAvcXVpdCAtLSBkaXNjb25uZWN0DQoqKiogIC9'
    'tZSAtLSBwZXJmb3JtIGFuIGFjdGlvbg0KKioqICAvd2hvIC0tIGxpc3QgY29ubmVjdGVkIHVzZ'
    'XJzDQoqKiogIC9uaWNrIC0tIGNoYW5nZSB5b3VyIG5pY2tuYW1lIHRvIHNvbWV0aGluZyBiZXR'
    '0ZXJ+fn4qKiogJXMgaXMgbm93IGtub3duIGFzICVzLn5+fioqKiBUaGF0IG5pY2tuYW1lIGlzI'
    'GludmFsaWQufn5+KioqIFR5cGUgIi9oZWxwIiBmb3IgaGVscC5+fn4qKiogSGVsbG8hIFdoYXQ'
    'gaXMgeW91ciBuaWNrbmFtZT9+fn4qKiogVGhhdCBuaWNrbmFtZSBpcyBhbHJlYWR5IGluIHVzZ'
    'S5+fn4qKiogSW5jb21pbmcgY29ubmVjdGlvbiF+fn4qKiogTm8gc3VjaCBjb21tYW5kOiAvJXN'
    '+fn4qKiogJXMgaGFzIGpvaW5lZC5+fn5eLyhbQS16XSopXHMqKC4qKSR+fn5eW0EtejAtOV9dK'
    'yR+fn4qKiogJXMgaGFzIGxlZnQufn5+KiAlcyAlc35+fjwlcz4gJXM='
)

And here are the same strings, as presented in the redented version, plus a simple test to make sure I didn't screw up with my reformatting.

REDENT_M_BASE64 = (
    'c3lzIHNpZ25hbCBzb2NrZXQgc2VsZWN0IGNvbGxlY3Rpb25zIGVycm5vIHR5cGV'
    'zIGl0ZXJ0b29scyByZQ=='
)
REDENT_L_BASE64 = (
    'KioqIFNlcnZlciBnb2luZyBkb3duIX5+fioqKiBDdXJyZW50bHkgY29ubmVjdGV'
    'kOiB+fn4qKiogQXZhaWxhYmxlIGNvbW1hbmRzOg0KKioqICAvaGVscCAtLSBnZXQg'
    'aGVscA0KKioqICAvcXVpdCAtLSBkaXNjb25uZWN0DQoqKiogIC9tZSAtLSBwZXJmb'
    '3JtIGFuIGFjdGlvbg0KKioqICAvd2hvIC0tIGxpc3QgY29ubmVjdGVkIHVzZXJzDQ'
    'oqKiogIC9uaWNrIC0tIGNoYW5nZSB5b3VyIG5pY2tuYW1lIHRvIHNvbWV0aGluZyB'
    'iZXR0ZXJ+fn4qKiogJXMgaXMgbm93IGtub3duIGFzICVzLn5+fioqKiBUaGF0IG5p'
    'Y2tuYW1lIGlzIGludmFsaWQufn5+KioqIFR5cGUgIi9oZWxwIiBmb3IgaGVscC5+f'
    'n4qKiogSGVsbG8hIFdoYXQgaXMgeW91ciBuaWNrbmFtZT9+fn4qKiogVGhhdCBuaW'
    'NrbmFtZSBpcyBhbHJlYWR5IGluIHVzZS5+fn4qKiogSW5jb21pbmcgY29ubmVjdGl'
    'vbiF+fn4qKiogTm8gc3VjaCBjb21tYW5kOiAvJXN+fn4qKiogJXMgaGFzIGpvaW5l'
    'ZC5+fn5eLyhbQS16XSopXHMqKC4qKSR+fn5eW0EtejAtOV9dKyR+fn4qKiogJXMga'
    'GFzIGxlZnQufn5+KiAlcyAlc35+fjwlcz4gJXM='
)
assert ORIGINAL_M_BASE64 == REDENT_M_BASE64
assert ORIGINAL_L_BASE64 == REDENT_L_BASE64

Those didn't blow up. Let's make it a bit nicer-looking with triple-quoted strings and strip the newlines. The asserts make sure they're equal.

M_BASE64 = """
c3lzIHNpZ25hbCBzb2NrZXQgc2VsZWN0IGNvbGxlY3Rpb25zIGVycm5vIHR5cGVzIGl0ZXJ0b29scy
ByZQ==
""".replace('\n', '')
L_BASE64 = """
KioqIFNlcnZlciBnb2luZyBkb3duIX5+fioqKiBDdXJyZW50bHkgY29ubmVjdGVkOiB+fn4qKiogQX
ZhaWxhYmxlIGNvbW1hbmRzOg0KKioqICAvaGVscCAtLSBnZXQgaGVscA0KKioqICAvcXVpdCAtLSBk
aXNjb25uZWN0DQoqKiogIC9tZSAtLSBwZXJmb3JtIGFuIGFjdGlvbg0KKioqICAvd2hvIC0tIGxpc3
QgY29ubmVjdGVkIHVzZXJzDQoqKiogIC9uaWNrIC0tIGNoYW5nZSB5b3VyIG5pY2tuYW1lIHRvIHNv
bWV0aGluZyBiZXR0ZXJ+fn4qKiogJXMgaXMgbm93IGtub3duIGFzICVzLn5+fioqKiBUaGF0IG5pY2
tuYW1lIGlzIGludmFsaWQufn5+KioqIFR5cGUgIi9oZWxwIiBmb3IgaGVscC5+fn4qKiogSGVsbG8h
IFdoYXQgaXMgeW91ciBuaWNrbmFtZT9+fn4qKiogVGhhdCBuaWNrbmFtZSBpcyBhbHJlYWR5IGluIH
VzZS5+fn4qKiogSW5jb21pbmcgY29ubmVjdGlvbiF+fn4qKiogTm8gc3VjaCBjb21tYW5kOiAvJXN+
fn4qKiogJXMgaGFzIGpvaW5lZC5+fn5eLyhbQS16XSopXHMqKC4qKSR+fn5eW0EtejAtOV9dKyR+fn
4qKiogJXMgaGFzIGxlZnQufn5+KiAlcyAlc35+fjwlcz4gJXM=
""".replace('\n', '')

assert M_BASE64 == ORIGINAL_M_BASE64
assert L_BASE64 == ORIGINAL_L_BASE64

Now let's see what we actually have.

import base64

M_BASE64_DECODED = base64.b64decode(M_BASE64)
L_BASE64_DECODED = base64.b64decode(L_BASE64)

print repr(M_BASE64_DECODED)

### Result:
# 'sys signal socket select collections errno types itertools re'

# Obviously these are some modules used. Let's make a list.
modules_list = M_BASE64.split()

That was pretty easy. How about the other one?

print repr(L_BASE64_DECODED)

### Result:
# '*** Server going down!~~~*** Currently connected: ~~~*** Available commands:\r\n***  /help -- get help\r\n***  /quit -- disconnect\r\n***  /me -- perform an action\r\n***  /who -- list connected users\r\n***  /nick -- change your nickname to something better~~~*** %s is now known as %s.~~~*** That nickname is invalid.~~~*** Type "/help" for help.~~~*** Hello! What is your nickname?~~~*** That nickname is already in use.~~~*** Incoming connection!~~~*** No such command: /%s~~~*** %s has joined.~~~^/([A-z]*)\\s*(.*)$~~~^[A-z0-9_]+$~~~*** %s has left.~~~* %s %s~~~<%s> %s'

Those look really familar! We saw most of that stuff when we ran the thing earlier. I didn't see any tildes in my interaction, and I vaguely remember a split involving tildes... so let's try that, and grab the indicies for good measure.

for i, msg in enumerate(L_BASE64_DECODED.split('~~~')):
    print '{0}: {1}'.format(i, repr(msg))

### Result:
# 0: '*** Server going down!'
# 1: '*** Currently connected: '
# 2: '*** Available commands:\r\n***  /help -- get help\r\n***  /quit -- disconnect\r\n***  /me -- perform an action\r\n***  /who -- list connected users\r\n***  /nick -- change your nickname to something better'
# 3: '*** %s is now known as %s.'
# 4: '*** That nickname is invalid.'
# 5: '*** Type "/help" for help.'
# 6: '*** Hello! What is your nickname?'
# 7: '*** That nickname is already in use.'
# 8: '*** Incoming connection!'
# 9: '*** No such command: /%s'
# 10: '*** %s has joined.'
# 11: '^/([A-z]*)\\s*(.*)$'
# 12: '^[A-z0-9_]+$'
# 13: '*** %s has left.'
# 14: '* %s %s'
# 15: '<%s> %s'

Progress! Most of these things are obvious. The regex patterns and the very simple format strings stick out, but it's not immediately obvious what they're for. Let's structure this a bit better so we can get an idea about the semantics of these strings, the non-obvious ones are marked with the reasoning I used to name them.

outputs_list = L_BASE64_DECODED.split('~~~')
outputs_keys = [
    'server_exit',              # 0: '*** Server going down!'
    'who_response_prefix',      # 1: '*** Currently connected: '
    'help_response',            # 2: '*** Available commands:\r\n*** <trunc>
    'nick_response_format',     # 3: '*** %s is now known as %s.'
    'nick_response_error',      # 4: '*** That nickname is invalid.'
    'help_message',             # 5: '*** Type "/help" for help.'
    'greeting_prompt',          # 6: '*** Hello! What is your nickname?'
    'nick_nonunique_error',     # 7: '*** That nickname is already in use.'
    'incoming_message',         # 8: '*** Incoming connection!'
    'command_error_format',     # 9: '*** No such command: /%s'
    'join_message_format',      # 10: '*** %s has joined.'
    ### This has to be the command pattern, since the anchor is followed
    ### immediately by a forward slash
    'command_pattern',          # 11: '^/([A-z]*)\\s*(.*)$'
    ### Not too sure what this is for yet, but it definitely matches a single
    ### word. Perhaps it's used for nick validation?
    'word_pattern',             # 12: '^[A-z0-9_]+$'
    'part_message_format',      # 13: '*** %s has left.'
    ### The only thing in the initial testing session that started with a
    ### single asterix was the result of the action command /me
    'action_message_format',    # 14: '* %s %s'
    ### Angle brackets around the nick, then the message.
    'chat_message_format',      # 15: '<%s> %s'
]

Unfortunately, this doesn't get us very far. Since the decoding and splitting happens far away from the strings themselves, I don't feel right just guessing. Let's file this away, and attack a more difficult thing that's closer to the meat of the code.

Yo Dawg, I Heard You Like type...

Regardless of how well-obfusicated the code is, there still has to be some structure. We already found some strings that allow the code to get modules and interact with the user, but maybe we can find something lower-level that provides some structure. At the end of the file, the type built-in gets called with three arguments. This form creates a new class based on the arguments, which are respectively name, base type(s), and a mapping that will form the class's __dict__ attribute.

type(
    '',
    (dict,),
    dict(
        __getattr__=lambda s, k:  s[k],
        u=lambda s, *a:  s.__setitem__(*a), # ** REMOVEME!!!
        __setattr__=lambda s, k, v: s.__setitem__(k, v),
        s=property(
            lambda s: type(
                '',
                (object,),
                dict(
                    __getitem__=lambda ss, k: reduce(
                        getattr,
                        k.split('.'),
                        s
                    )
                )
            )()
        )
    )
)

We can start writing this as a normal class statement. There are two distinct calls of type, let's tackle the outer one first.

class Unknown(dict):
    def __getattr__(self, k):
        return self[k]

    def u(self, *a):  # ** REMOVEME!!!
        return self.__setitem__(*a)  # ** REMOVEME!!!

    def __setattr__(self, k, v):
        return self.__setitem__(k, v)

The first three methods are pretty straightforward, but that property thing is kinda crazy. Let's reason through it. We can replace the explicit call to property with the decorator syntax, and make the method.

@property
def s(self):
    return type(
        '',
        (object,),
        dict(
            __getitem__=lambda ss, k: reduce(
                getattr,
                k.split('.'),
                self
            )
        )
    )()

Ok, that's a little better. Now let's change it to make the class with a normal statement. Note that the type isn't returned... an instance of it, notice the () after the type call?

@property
def s(self):
    class InnerUnknown(object):
        def __getitem__(innerself, k):
            return reduce(getattr, k.split('.'), self)
    return InnerUnknown()

All together now...

class Unknown(dict):
    def __getattr__(self, k):
        return self[k]

    def u(self, *a):  # ** REMOVEME!!!
        return self.__setitem__(*a)  # ** REMOVEME!!!

    def __setattr__(self, k, v):
        return self.__setitem__(k, v)

    @property
    def s(self):
        class InnerUnknown(object):
            def __getitem__(innerself, k):
                return reduce(getattr, k.split('.'), self)
        return InnerUnknown()

Phew. Before we try to figure out more, let's stick it in the redented version to replace the existing part...

--- chatservers/redent.py       2014-06-03 15:59:45.000000000 -0500
+++ chatservers/typedecoded.py  2014-06-03 15:59:45.000000000 -0500
@@ -1,3 +1,21 @@
+class UnknownMap(dict):
+    def __getattr__(self, k):
+        return self[k]
+
+    def u(self, *a):  # ** REMOVEME!!!
+        return self.__setitem__(*a)  # ** REMOVEME!!!
+
+    def __setattr__(self, k, v):
+        return self.__setitem__(k, v)
+
+    @property
+    def s(self):
+        class InnerUnknown(object):
+            def __getitem__(innerself, k):
+                return reduce(getattr, k.split('.'), self)
+        return InnerUnknown()
+
+
 main = lambda port: (
     lambda dt: (
         lambda mm: (
@@ -311,27 +329,4 @@
             'GFzIGxlZnQufn5+KiAlcyAlc35+fjwlcz4gJXM='
         )
     )
-)(
-    type(
-        '',
-        (dict,),
-        dict(
-            __getattr__=lambda s, k:  s[k],
-            u=lambda s, *a:  s.__setitem__(*a),
-            __setattr__=lambda s, k, v: s.__setitem__(k, v),
-            s=property(
-                lambda s: type(
-                    '',
-                    (object,),
-                    dict(
-                        __getitem__=lambda ss, k: reduce(
-                            getattr,
-                            k.split('.'),
-                            s
-                        )
-                    )
-                )()
-            )
-        )
-    )
-)
+)(UnknownMap)

...and run the test suite again...

test_chatserver
  ChatserverTestCase
    test_bogus_command_error ...                                           [OK]
    test_chat ...                                                          [OK]
    test_chat_broadcast_to_all_users ...                                   [OK]
    test_emote ...                                                         [OK]
    test_error_nonunique_nick ...                                          [OK]
    test_error_nonunique_nick_on_login ...                                 [OK]
    test_help ...                                                          [OK]
    test_invalid_nick ...                                                  [OK]
    test_invalid_nick_on_login ...                                         [OK]
    test_login_and_quit ...                                                [OK]
    test_nick ...                                                          [OK]
    test_simple_who ...                                                    [OK]

-------------------------------------------------------------------------------
Ran 12 tests in 0.925s

PASSED (successes=12)

Whoa, it worked... amazing!

Location, Location, Location

Now that we know the type is correct, let's try to figure out where it gets used. The lambda that gets called with that type as the argument has it named dt, and that name is only used in two places besides the lambda's signature. Therefore, I can see that the type gets instantiated once with no arguments, as well as once with the base64-encoded blobs we looked at earlier. Let's unwrap the call, and use the type directly in those places.

--- chatservers/typedecoded.py  2014-06-03 15:59:45.000000000 -0500
+++ chatservers/typedecoded_2.py        2014-06-03 15:59:45.000000000 -0500
@@ -17,7 +17,6 @@


 main = lambda port: (
-    lambda dt: (
         lambda mm: (
             lambda n: (
                 map(
@@ -310,9 +309,9 @@
                     ]
                 )
             )
-        )(dt())
+        )(UnknownMap())
     )(
-        dt(
+        UnknownMap(
             m='c3lzIHNpZ25hbCBzb2NrZXQgc2VsZWN0IGNvbGxlY3Rpb25zIGVycm5vIHR5cGV'
             'zIGl0ZXJ0b29scyByZQ==',
             l='KioqIFNlcnZlciBnb2luZyBkb3duIX5+fioqKiBDdXJyZW50bHkgY29ubmVjdGV'
@@ -329,4 +328,3 @@
             'GFzIGxlZnQufn5+KiAlcyAlc35+fjwlcz4gJXM='
         )
     )
-)(UnknownMap)

This doesn't look all that different, but look closely. We just eliminated an entire level of depth. If we can keep doing this, it won't take too long to see what the code does. So, let's try again! The function under the outermost one is now the one that takes a parameter called mm, which is the instance of our UnknownMap that was instantiated with the base64-encoded strings.

The mm name is used to access attributes named m and l in the majority of the code. Based on the implementation of UnknownMap, we can tell that the attribute access merely defers to __getitem__ of the superclass (dict), so therefore mm.m and mm.l refer (at least initially) to the base64-encoded module names and utility strings stored in the UnknownMap instance. The object mm is only used as a bare name one time, with this code:

lambda: setattr(
    mm, 'l', mm.l.decode('base64').split('~~~')
),

This tells us that at some point, the base64-encoded messages string at mm.l gets replaced with the list of decoded message strings. Cool! Unfortunately, since this happens at a time we can't be sure of yet, we can't simply replace mm with a better named instance created elsewhere. Well, probably not, anyway. Let's try, maybe we'll get lucky! I picked the name main_store on a guess that this object is a holder of a lot of things.

--- chatservers/typedecoded_2.py        2014-06-03 15:59:45.000000000 -0500
+++ chatservers/typedecoded_3.py        2014-06-03 15:59:45.000000000 -0500
@@ -16,8 +16,26 @@
         return InnerUnknown()


+# Used to be called "mm"
+main_store = UnknownMap(
+    m='c3lzIHNpZ25hbCBzb2NrZXQgc2VsZWN0IGNvbGxlY3Rpb25zIGVycm5vIHR5cGV'
+    'zIGl0ZXJ0b29scyByZQ==',
+    l='KioqIFNlcnZlciBnb2luZyBkb3duIX5+fioqKiBDdXJyZW50bHkgY29ubmVjdGV'
+    'kOiB+fn4qKiogQXZhaWxhYmxlIGNvbW1hbmRzOg0KKioqICAvaGVscCAtLSBnZXQg'
+    'aGVscA0KKioqICAvcXVpdCAtLSBkaXNjb25uZWN0DQoqKiogIC9tZSAtLSBwZXJmb'
+    '3JtIGFuIGFjdGlvbg0KKioqICAvd2hvIC0tIGxpc3QgY29ubmVjdGVkIHVzZXJzDQ'
+    'oqKiogIC9uaWNrIC0tIGNoYW5nZSB5b3VyIG5pY2tuYW1lIHRvIHNvbWV0aGluZyB'
+    'iZXR0ZXJ+fn4qKiogJXMgaXMgbm93IGtub3duIGFzICVzLn5+fioqKiBUaGF0IG5p'
+    'Y2tuYW1lIGlzIGludmFsaWQufn5+KioqIFR5cGUgIi9oZWxwIiBmb3IgaGVscC5+f'
+    'n4qKiogSGVsbG8hIFdoYXQgaXMgeW91ciBuaWNrbmFtZT9+fn4qKiogVGhhdCBuaW'
+    'NrbmFtZSBpcyBhbHJlYWR5IGluIHVzZS5+fn4qKiogSW5jb21pbmcgY29ubmVjdGl'
+    'vbiF+fn4qKiogTm8gc3VjaCBjb21tYW5kOiAvJXN+fn4qKiogJXMgaGFzIGpvaW5l'
+    'ZC5+fn5eLyhbQS16XSopXHMqKC4qKSR+fn5eW0EtejAtOV9dKyR+fn4qKiogJXMga'
+    'GFzIGxlZnQufn5+KiAlcyAlc35+fjwlcz4gJXM='
+)
+
+
 main = lambda port: (
-        lambda mm: (
             lambda n: (
                 map(
                     lambda r:  (
@@ -34,7 +52,7 @@
                                 *[
                                     (n, m, __import__(m))
                                     for m
-                                    in mm.m.decode('ba' 'se64').split()
+                                    in main_store.m.decode('ba' 'se64').split()
                                 ]
                             )
                         ),
@@ -46,7 +64,7 @@
                                     n.s['sys.exit']()
                                     if n.f
                                     else (
-                                        [n.sa(mm.l[0], n.o)] and
+                                        [n.sa(main_store.l[0], n.o)] and
                                         n.u('f', True) or
                                         n.fc(n.l)
                                     )
@@ -54,7 +72,7 @@
                             ] * 2
                         ),
                         lambda: setattr(
-                            mm, 'l', mm.l.decode('base64').split('~~~')
+                            main_store, 'l', main_store.l.decode('base64').split('~~~')
                         ),
                         lambda: (
                             'sw',
@@ -87,29 +105,29 @@
                         lambda: n.update(
                             si=(lambda o: o.__setitem__),
                             di=(lambda o: o.__delitem__),
-                            cr=n.s['re.compile'](mm.l[11]),
+                            cr=n.s['re.compile'](main_store.l[11]),
                             nn={},
                             dp=lambda s, d, c, l: n.dd.get(c, d)(s, l, c),
                             dd=dict(
                                 me=lambda s, l, c: (
-                                    n.sa(mm.l[14] % (n.nn[s], l))
+                                    n.sa(main_store.l[14] % (n.nn[s], l))
                                 ),
                                 quit=lambda s, l, c: n.c(s),
                                 who=lambda s, l, c: n.ws(
-                                    s, mm.l[1] + ', '.join(n.s['nn.values']())
+                                    s, main_store.l[1] + ', '.join(n.s['nn.values']())
                                 ),
-                                help=lambda s, l, c: n.ws(s, mm.l[2]),
+                                help=lambda s, l, c: n.ws(s, main_store.l[2]),
                                 nick=lambda s, l, c: (
                                     (
                                         (
-                                            [n.sa(mm.l[3] % (n.nn[s], l))] and
+                                            [n.sa(main_store.l[3] % (n.nn[s], l))] and
                                             n.si(n.nn)(s, l)
                                         )
                                         if n.nr.match(l)
-                                        else n.ws(s, mm.l[4])
+                                        else n.ws(s, main_store.l[4])
                                     )
                                     if l not in n.nn.values()
-                                    else n.ws(s, mm.l[7])
+                                    else n.ws(s, main_store.l[7])
                                 )
                             ),
                             so=(n.u('f', False) or [n.l]),
@@ -118,7 +136,7 @@
                             o=(lambda: filter(lambda s:  s is not n.l, n.so)),
                             w=(lambda: filter(n.ob.__getitem__, n.o())),
                             ws=(lambda s, l: n.ob[s].append(l + '\r\n')),
-                            nr=n.s['re.compile'](mm.l[12]),
+                            nr=n.s['re.compile'](main_store.l[12]),
                             sa=(
                                 lambda d, f=n.ro: map(
                                     lambda s: n.ws(s, d),
@@ -128,7 +146,7 @@
                             fs=set(),
                             c=(
                                 lambda s: [
-                                    n.sa(mm.l[13] % n.nn[s])
+                                    n.sa(main_store.l[13] % n.nn[s])
                                     if s in n.nn and s not in n.fs
                                     else None
                                 ] and n.s['fs.add'](s)
@@ -158,7 +176,7 @@
                                                     apply,
                                                     [
                                                         lambda: (
-                                                            n.sa(mm.l[8])
+                                                            n.sa(main_store.l[8])
                                                         ),
                                                         lambda: (
                                                             n.so.append(
@@ -167,7 +185,7 @@
                                                         ),
                                                         lambda: n.ws(
                                                             n.so[-1],
-                                                            mm.l[6]
+                                                            main_store.l[6]
                                                         )
                                                     ]
                                                 ) if s is n.l else map(
@@ -233,7 +251,7 @@
                                         lambda (s, l): (
                                             (
                                                 n.sa(
-                                                    mm.l[15] % (
+                                                    main_store.l[15] % (
                                                         n.nn[s],
                                                         l[1:]
                                                         if l.startswith('//')
@@ -250,7 +268,7 @@
                                                         lambda s, l, c: (
                                                             n.ws(
                                                                 s,
-                                                                mm.l[9] % c
+                                                                main_store.l[9] % c
                                                             )
                                                         ),
                                                         *n.s['cr.match'](
@@ -264,14 +282,14 @@
                                                 (
                                                     (
                                                         n.si(n.nn)(s, l) or
-                                                        [n.ws(s, mm.l[5])] and
-                                                        n.sa(mm.l[10] % l)
+                                                        [n.ws(s, main_store.l[5])] and
+                                                        n.sa(main_store.l[10] % l)
                                                     )
                                                     if n.nr.match(l)
-                                                    else n.ws(s, mm.l[4])
+                                                    else n.ws(s, main_store.l[4])
                                                 )
                                                 if l not in n.nn.values()
-                                                else n.ws(s, mm.l[7])
+                                                else n.ws(s, main_store.l[7])
                                             )
                                         ),
                                         [
@@ -310,21 +328,3 @@
                 )
             )
         )(UnknownMap())
-    )(
-        UnknownMap(
-            m='c3lzIHNpZ25hbCBzb2NrZXQgc2VsZWN0IGNvbGxlY3Rpb25zIGVycm5vIHR5cGV'
-            'zIGl0ZXJ0b29scyByZQ==',
-            l='KioqIFNlcnZlciBnb2luZyBkb3duIX5+fioqKiBDdXJyZW50bHkgY29ubmVjdGV'
-            'kOiB+fn4qKiogQXZhaWxhYmxlIGNvbW1hbmRzOg0KKioqICAvaGVscCAtLSBnZXQg'
-            'aGVscA0KKioqICAvcXVpdCAtLSBkaXNjb25uZWN0DQoqKiogIC9tZSAtLSBwZXJmb'
-            '3JtIGFuIGFjdGlvbg0KKioqICAvd2hvIC0tIGxpc3QgY29ubmVjdGVkIHVzZXJzDQ'
-            'oqKiogIC9uaWNrIC0tIGNoYW5nZSB5b3VyIG5pY2tuYW1lIHRvIHNvbWV0aGluZyB'
-            'iZXR0ZXJ+fn4qKiogJXMgaXMgbm93IGtub3duIGFzICVzLn5+fioqKiBUaGF0IG5p'
-            'Y2tuYW1lIGlzIGludmFsaWQufn5+KioqIFR5cGUgIi9oZWxwIiBmb3IgaGVscC5+f'
-            'n4qKiogSGVsbG8hIFdoYXQgaXMgeW91ciBuaWNrbmFtZT9+fn4qKiogVGhhdCBuaW'
-            'NrbmFtZSBpcyBhbHJlYWR5IGluIHVzZS5+fn4qKiogSW5jb21pbmcgY29ubmVjdGl'
-            'vbiF+fn4qKiogTm8gc3VjaCBjb21tYW5kOiAvJXN+fn4qKiogJXMgaGFzIGpvaW5l'
-            'ZC5+fn5eLyhbQS16XSopXHMqKC4qKSR+fn5eW0EtejAtOV9dKyR+fn4qKiogJXMga'
-            'GFzIGxlZnQufn5+KiAlcyAlc35+fjwlcz4gJXM='
-        )
-    )

And the test suite passes! Another level shallower!

Another One Bites the Dust

The other UnknownMap instance (called with no arguments) gets fed into a function that calls it n. This object is used quite a bit more than mm, so it might be tricky to swap it out. We can see that it gets used with module names, as well as that strange UnknownMap.s property. Additionally, it is used in the only explicit (though not directly evaluated) statements in the code: the part with compile, select, and the try/except. Still, we might as well try doing a search-and-replace like before, it's worked so well. Let's call it namespace, since there's a lot going on with modules and module attributes. By the way, we can tell now that main_store wasn't a good name for the other instance... but it's memorable enough now that we can leave it.

--- chatservers/typedecoded_3.py        2014-06-03 15:59:45.000000000 -0500
+++ chatservers/typedecoded_4.py        2014-06-03 15:59:45.000000000 -0500
@@ -34,13 +34,15 @@
     'GFzIGxlZnQufn5+KiAlcyAlc35+fjwlcz4gJXM='
 )

+# Was called "n"
+namespace = UnknownMap()
+

 main = lambda port: (
-            lambda n: (
                 map(
                     lambda r:  (
                         lambda rr: (
-                            setattr(n, *rr)
+                            setattr(namespace, *rr)
                             if (type(rr) is tuple and len(rr) == 2)
                             else None
                         )
@@ -50,23 +52,23 @@
                             setattr,
                             *zip(
                                 *[
-                                    (n, m, __import__(m))
+                                    (namespace, m, __import__(m))
                                     for m
                                     in main_store.m.decode('ba' 'se64').split()
                                 ]
                             )
                         ),
                         lambda: map(
-                            n.s['signal.signal'],
-                            (n.s['signal.SIGINT'], n.s['signal.SIGTERM']),
+                            namespace.s['signal.signal'],
+                            (namespace.s['signal.SIGINT'], namespace.s['signal.SIGTERM']),
                             [
                                 lambda s, f: (
-                                    n.s['sys.exit']()
-                                    if n.f
+                                    namespace.s['sys.exit']()
+                                    if namespace.f
                                     else (
-                                        [n.sa(main_store.l[0], n.o)] and
-                                        n.u('f', True) or
-                                        n.fc(n.l)
+                                        [namespace.sa(main_store.l[0], namespace.o)] and
+                                        namespace.u('f', True) or
+                                        namespace.fc(namespace.l)
                                     )
                                 )
                             ] * 2
@@ -76,89 +78,89 @@
                         ),
                         lambda: (
                             'sw',
-                            n.s['types.Functio' 'nType'](
+                            namespace.s['types.Functio' 'nType'](
                                 compile(
                                     "try:\n"
-                                    "\tv = n.select.select(n.so, n.w(), [])\n"
-                                    "except n.select.error, e:\n"
-                                    "\tif e[0] != n.errno.EINTR: raise\n"
+                                    "\tv = namespace.select.select(namespace.so, namespace.w(), [])\n"
+                                    "except namespace.select.error, e:\n"
+                                    "\tif e[0] != namespace.errno.EINTR: raise\n"
                                     "else:\n"
-                                    "\tn.u('sr', v)",
+                                    "\tnamespace.u('sr', v)",
                                     '',
                                     'exec'
                                 ),
-                                dict(n=n, OSError=OSError)
+                                dict(namespace=namespace, OSError=OSError)
                             )
                         ),
                         lambda: (
                             'l',
-                            n.s['socket.socket'](
-                                n.s['socket.AF_INET'],
-                                n.s['socket.SOCK_STREAM']
+                            namespace.s['socket.socket'](
+                                namespace.s['socket.AF_INET'],
+                                namespace.s['socket.SOCK_STREAM']
                             )
                         ),
-                        lambda: n.s['l.bind'](('', port)),
-                        lambda: n.s['l.listen'](5),
+                        lambda: namespace.s['l.bind'](('', port)),
+                        lambda: namespace.s['l.listen'](5),
                         lambda: (
-                            'ro', lambda: filter(lambda s: s in n.nn,  n.so)
+                            'ro', lambda: filter(lambda s: s in namespace.nn,  namespace.so)
                         ),
-                        lambda: n.update(
+                        lambda: namespace.update(
                             si=(lambda o: o.__setitem__),
                             di=(lambda o: o.__delitem__),
-                            cr=n.s['re.compile'](main_store.l[11]),
+                            cr=namespace.s['re.compile'](main_store.l[11]),
                             nn={},
-                            dp=lambda s, d, c, l: n.dd.get(c, d)(s, l, c),
+                            dp=lambda s, d, c, l: namespace.dd.get(c, d)(s, l, c),
                             dd=dict(
                                 me=lambda s, l, c: (
-                                    n.sa(main_store.l[14] % (n.nn[s], l))
+                                    namespace.sa(main_store.l[14] % (namespace.nn[s], l))
                                 ),
-                                quit=lambda s, l, c: n.c(s),
-                                who=lambda s, l, c: n.ws(
-                                    s, main_store.l[1] + ', '.join(n.s['nn.values']())
+                                quit=lambda s, l, c: namespace.c(s),
+                                who=lambda s, l, c: namespace.ws(
+                                    s, main_store.l[1] + ', '.join(namespace.s['nn.values']())
                                 ),
-                                help=lambda s, l, c: n.ws(s, main_store.l[2]),
+                                help=lambda s, l, c: namespace.ws(s, main_store.l[2]),
                                 nick=lambda s, l, c: (
                                     (
                                         (
-                                            [n.sa(main_store.l[3] % (n.nn[s], l))] and
-                                            n.si(n.nn)(s, l)
+                                            [namespace.sa(main_store.l[3] % (namespace.nn[s], l))] and
+                                            namespace.si(namespace.nn)(s, l)
                                         )
-                                        if n.nr.match(l)
-                                        else n.ws(s, main_store.l[4])
+                                        if namespace.nr.match(l)
+                                        else namespace.ws(s, main_store.l[4])
                                     )
-                                    if l not in n.nn.values()
-                                    else n.ws(s, main_store.l[7])
+                                    if l not in namespace.nn.values()
+                                    else namespace.ws(s, main_store.l[7])
                                 )
                             ),
-                            so=(n.u('f', False) or [n.l]),
-                            ib=n.s['collections' '.defaultdict'](list),
-                            ob=n.s['collections.defaultdict'](list),
-                            o=(lambda: filter(lambda s:  s is not n.l, n.so)),
-                            w=(lambda: filter(n.ob.__getitem__, n.o())),
-                            ws=(lambda s, l: n.ob[s].append(l + '\r\n')),
-                            nr=n.s['re.compile'](main_store.l[12]),
+                            so=(namespace.u('f', False) or [namespace.l]),
+                            ib=namespace.s['collections' '.defaultdict'](list),
+                            ob=namespace.s['collections.defaultdict'](list),
+                            o=(lambda: filter(lambda s:  s is not namespace.l, namespace.so)),
+                            w=(lambda: filter(namespace.ob.__getitem__, namespace.o())),
+                            ws=(lambda s, l: namespace.ob[s].append(l + '\r\n')),
+                            nr=namespace.s['re.compile'](main_store.l[12]),
                             sa=(
-                                lambda d, f=n.ro: map(
-                                    lambda s: n.ws(s, d),
+                                lambda d, f=namespace.ro: map(
+                                    lambda s: namespace.ws(s, d),
                                     f()
                                 )
                             ),
                             fs=set(),
                             c=(
                                 lambda s: [
-                                    n.sa(main_store.l[13] % n.nn[s])
-                                    if s in n.nn and s not in n.fs
+                                    namespace.sa(main_store.l[13] % namespace.nn[s])
+                                    if s in namespace.nn and s not in namespace.fs
                                     else None
-                                ] and n.s['fs.add'](s)
+                                ] and namespace.s['fs.add'](s)
                             ),
                             fc=(
                                 lambda s: (
                                     [s.close()] and
-                                    (n.so.remove(s) if s in n.so else None) or
-                                    (n.fs.remove(s) if s in n.fs else None) or
-                                    (n.di(n.ib)(s) if s in n.ib else None) or
-                                    (n.di(n.ob)(s) if s in n.ob else None) or
-                                    (n.di(n.nn)(s) if s in n.nn else None)
+                                    (namespace.so.remove(s) if s in namespace.so else None) or
+                                    (namespace.fs.remove(s) if s in namespace.fs else None) or
+                                    (namespace.di(namespace.ib)(s) if s in namespace.ib else None) or
+                                    (namespace.di(namespace.ob)(s) if s in namespace.ob else None) or
+                                    (namespace.di(namespace.nn)(s) if s in namespace.nn else None)
                                 )
                             )
                         ),
@@ -166,8 +168,8 @@
                             lambda f:  map(
                                 apply,
                                 [
-                                    lambda: n.u('sr', ([], [], [])),
-                                    lambda: n.sw(),
+                                    lambda: namespace.u('sr', ([], [], [])),
+                                    lambda: namespace.sw(),
                                     lambda: map(
                                         apply,
                                         [
@@ -176,29 +178,29 @@
                                                     apply,
                                                     [
                                                         lambda: (
-                                                            n.sa(main_store.l[8])
+                                                            namespace.sa(main_store.l[8])
                                                         ),
                                                         lambda: (
-                                                            n.so.append(
+                                                            namespace.so.append(
                                                                 s.accept()[0]
                                                             )
                                                         ),
-                                                        lambda: n.ws(
-                                                            n.so[-1],
+                                                        lambda: namespace.ws(
+                                                            namespace.so[-1],
                                                             main_store.l[6]
                                                         )
                                                     ]
-                                                ) if s is n.l else map(
+                                                ) if s is namespace.l else map(
                                                     apply,
                                                     [
                                                         lambda: (
-                                                            n.ib[s].append(
+                                                            namespace.ib[s].append(
                                                                 s.recv(4096)
                                                             ) or
                                                             (
-                                                                n.c(s)
+                                                                namespace.c(s)
                                                                 if (
-                                                                    n.ib[s][-1]
+                                                                    namespace.ib[s][-1]
                                                                     == ''
                                                                 )
                                                                 else None
@@ -211,48 +213,48 @@
                                             lambda *ss: map(
                                                 lambda s: (
                                                     lambda d: (
-                                                        n.si(n.ob)(
+                                                        namespace.si(namespace.ob)(
                                                             s,
                                                             [d[s.send(d):]]
                                                         ) or
                                                         (
-                                                            n.si(n.ob)(s, [])
-                                                            if n.ob[s] == ['']
+                                                            namespace.si(namespace.ob)(s, [])
+                                                            if namespace.ob[s] == ['']
                                                             else None
                                                         )
                                                     )
-                                                )(''.  join(n.ob[s])), ss),
+                                                )(''.  join(namespace.ob[s])), ss),
                                             lambda *ss: None
                                         ],
-                                        n.sr
+                                        namespace.sr
                                     ),
                                     lambda: [
-                                        n.di(v)(slice(None, None))
+                                        namespace.di(v)(slice(None, None))
                                         for k, v
-                                        in n.ob.items()
+                                        in namespace.ob.items()
                                         if v and not filter(None, v)
                                     ],
-                                    lambda: n.u('sl', {}),
+                                    lambda: namespace.u('sl', {}),
                                     lambda: map(
                                         (
                                             lambda (k, v): (
-                                                n.si(n.sl)(
+                                                namespace.si(namespace.sl)(
                                                     k,
                                                     ''.join(v).split('\r\n')
-                                                ) or n.si(n.ib)(
+                                                ) or namespace.si(namespace.ib)(
                                                     k,
-                                                    [n.sl[k].pop()]
+                                                    [namespace.sl[k].pop()]
                                                 )
                                             )
                                         ),
-                                        n.ib.items()
+                                        namespace.ib.items()
                                     ),
-                                    lambda: n.sl and map(
+                                    lambda: namespace.sl and map(
                                         lambda (s, l): (
                                             (
-                                                n.sa(
+                                                namespace.sa(
                                                     main_store.l[15] % (
-                                                        n.nn[s],
+                                                        namespace.nn[s],
                                                         l[1:]
                                                         if l.startswith('//')
                                                         else l
@@ -263,68 +265,67 @@
                                                     l.startswith('//')
                                                 )
                                                 else (
-                                                    n.dp(
+                                                    namespace.dp(
                                                         s,
                                                         lambda s, l, c: (
-                                                            n.ws(
+                                                            namespace.ws(
                                                                 s,
                                                                 main_store.l[9] % c
                                                             )
                                                         ),
-                                                        *n.s['cr.match'](
+                                                        *namespace.s['cr.match'](
                                                             l
                                                         ).groups()
                                                     )
                                                 )
                                             )
-                                            if s in n.nn
+                                            if s in namespace.nn
                                             else (
                                                 (
                                                     (
-                                                        n.si(n.nn)(s, l) or
-                                                        [n.ws(s, main_store.l[5])] and
-                                                        n.sa(main_store.l[10] % l)
+                                                        namespace.si(namespace.nn)(s, l) or
+                                                        [namespace.ws(s, main_store.l[5])] and
+                                                        namespace.sa(main_store.l[10] % l)
                                                     )
-                                                    if n.nr.match(l)
-                                                    else n.ws(s, main_store.l[4])
+                                                    if namespace.nr.match(l)
+                                                    else namespace.ws(s, main_store.l[4])
                                                 )
-                                                if l not in n.nn.values()
-                                                else n.ws(s, main_store.l[7])
+                                                if l not in namespace.nn.values()
+                                                else namespace.ws(s, main_store.l[7])
                                             )
                                         ),
                                         [
                                             (s, l.rstrip())
                                             for s, ll
-                                            in n.sl.items()
+                                            in namespace.sl.items()
                                             for l
                                             in ll
                                         ]
                                     ),
                                     lambda: (
-                                        n.f and
+                                        namespace.f and
                                         map(
                                             lambda (s, b): (
-                                                n.fc(s)
+                                                namespace.fc(s)
                                                 if not filter(None, b)
                                                 else None
                                             ),
-                                            n.ob.items()
+                                            namespace.ob.items()
                                         )
                                     ),
                                     lambda: map(
                                         lambda s: (
-                                            n.fc(s)
-                                            if not filter(None,  n.ob[s])
+                                            namespace.fc(s)
+                                            if not filter(None,  namespace.ob[s])
                                             else None
                                         ),
-                                        list(n.fs)
+                                        list(namespace.fs)
                                     )
                                 ]
                             ),
-                            iter(lambda: bool(n.so), False)
+                            iter(lambda: bool(namespace.so), False)
                         ),
-                        lambda: n.s['l.' 'close']()
+                        lambda: namespace.s['l.' 'close']()
                     ]
                 )
             )
-        )(UnknownMap())

I didn't realize that my search-and-replace of s/\([^a-zA-Z\\]\)n\([^a-zA-Z]\)/\1namespace\2/gc missed a bit in the select loop... the regex missed the n inside "\tn.u('sr', v)" because my regex isn't a full python parser. Good thing I have a test suite!

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "chatservers/typedecoded_4.py", line 328, in <lambda>
    lambda: namespace.s['l.' 'close']()
  File "chatservers/typedecoded_4.py", line 49, in <lambda>
    )(r()),
  File "chatservers/typedecoded_4.py", line 326, in <lambda>
    iter(lambda: bool(namespace.so), False)
  File "chatservers/typedecoded_4.py", line 316, in <lambda>
    lambda: map(
  File "chatservers/typedecoded_4.py", line 172, in <lambda>
    lambda: namespace.sw(),
  File "", line 6, in <module>

NameError: name 'n' is not defined

While that isn't a lot of help, it still pointed out what the error was, so I could hunt for it. The test suite passes now, and we've improved the code by three whole levels of depth. I'm starting to feel good about this.

Remove a Whole Instance

Looking over the usage of main_store (what used to be mm), we can see that only the l attribute is used, apart from in the following code.

lambda: map(
    setattr,
    *zip(
        *[  # ** REMOVEME!!!
            (namespace, m, __import__(m))
            for m
            in main_store.m.decode('ba' 'se64').split()
        ]
    )
),

That ('ba' 'se64') bit is just an artifact from imperfect redenting. Python performs implicit string concatenation inside parens, braces, and brackets, so it is actually interpreted as ('base64'). We can tell from the initializer of main_store that the m attribute is the base64-encoded string containing the module names, so let's simply replace the entire decode-and-split expression (main_store.m.decode('ba' 'se64').split()) with the list we got during our base64 investigation. We can also remove the base64 blob from the constructor of main_store.

--- chatservers/typedecoded_4.py        2014-06-03 15:59:45.000000000 -0500
+++ chatservers/replace_main_store.py   2014-06-03 15:59:45.000000000 -0500
@@ -18,8 +18,6 @@

 # Used to be called "mm"
 main_store = UnknownMap(
-    m='c3lzIHNpZ25hbCBzb2NrZXQgc2VsZWN0IGNvbGxlY3Rpb25zIGVycm5vIHR5cGV'
-    'zIGl0ZXJ0b29scyByZQ==',
     l='KioqIFNlcnZlciBnb2luZyBkb3duIX5+fioqKiBDdXJyZW50bHkgY29ubmVjdGV'
     'kOiB+fn4qKiogQXZhaWxhYmxlIGNvbW1hbmRzOg0KKioqICAvaGVscCAtLSBnZXQg'
     'aGVscA0KKioqICAvcXVpdCAtLSBkaXNjb25uZWN0DQoqKiogIC9tZSAtLSBwZXJmb'
@@ -37,6 +35,18 @@
 # Was called "n"
 namespace = UnknownMap()

+MODULES = [
+    'sys',
+    'signal',
+    'socket',
+    'select',
+    'collections',
+    'errno',
+    'types',
+    'itertools',
+    're'
+]
+

 main = lambda port: (
                 map(
@@ -54,7 +64,7 @@
                                 *[
                                     (namespace, m, __import__(m))
                                     for m
-                                    in main_store.m.decode('ba' 'se64').split()
+                                    in MODULES
                                 ]
                             )
                         ),

The test suite passes, so we're in good shape. Now let's try to eliminate the other attribute, so we can get rid of main_store entirely. The majority of the places where main_store.l is used is simple item access with integers. The only other place is this code:

lambda: setattr(
    main_store, 'l', main_store.l.decode('base64').split('~~~')
),

This function is just an obfusicated way to rebind the l attribute to a list of strings. Obviously, we could just replace the decode-and-split expression with the list of chat message strings from before, but we can do better than that with a bit of reasoning. First of all, I know this operation only happens once, since list doesn't have a decode method, therefore we can completely remove this function. Second, since l is the only remaining attribute used on main_store, I'm betting we can replace main_store.l entirely with the list of strings from the decoded and split base64 source. Since that's the only part left that uses main_store, we can get rid of it entirely. No more base64 blobs!

Here's another reason to have good test coverage: I might be missing something here that could cause my reasoning to be faulty, but since I can easily test the new version, I have no worries. Here's the result of those changes:

--- chatservers/replace_main_store.py   2014-06-03 15:59:45.000000000 -0500
+++ chatservers/replace_main_store_2.py 2014-06-03 15:59:45.000000000 -0500
@@ -16,22 +16,6 @@
         return InnerUnknown()


-# Used to be called "mm"
-main_store = UnknownMap(
-    l='KioqIFNlcnZlciBnb2luZyBkb3duIX5+fioqKiBDdXJyZW50bHkgY29ubmVjdGV'
-    'kOiB+fn4qKiogQXZhaWxhYmxlIGNvbW1hbmRzOg0KKioqICAvaGVscCAtLSBnZXQg'
-    'aGVscA0KKioqICAvcXVpdCAtLSBkaXNjb25uZWN0DQoqKiogIC9tZSAtLSBwZXJmb'
-    '3JtIGFuIGFjdGlvbg0KKioqICAvd2hvIC0tIGxpc3QgY29ubmVjdGVkIHVzZXJzDQ'
-    'oqKiogIC9uaWNrIC0tIGNoYW5nZSB5b3VyIG5pY2tuYW1lIHRvIHNvbWV0aGluZyB'
-    'iZXR0ZXJ+fn4qKiogJXMgaXMgbm93IGtub3duIGFzICVzLn5+fioqKiBUaGF0IG5p'
-    'Y2tuYW1lIGlzIGludmFsaWQufn5+KioqIFR5cGUgIi9oZWxwIiBmb3IgaGVscC5+f'
-    'n4qKiogSGVsbG8hIFdoYXQgaXMgeW91ciBuaWNrbmFtZT9+fn4qKiogVGhhdCBuaW'
-    'NrbmFtZSBpcyBhbHJlYWR5IGluIHVzZS5+fn4qKiogSW5jb21pbmcgY29ubmVjdGl'
-    'vbiF+fn4qKiogTm8gc3VjaCBjb21tYW5kOiAvJXN+fn4qKiogJXMgaGFzIGpvaW5l'
-    'ZC5+fn5eLyhbQS16XSopXHMqKC4qKSR+fn5eW0EtejAtOV9dKyR+fn4qKiogJXMga'
-    'GFzIGxlZnQufn5+KiAlcyAlc35+fjwlcz4gJXM='
-)
-
 # Was called "n"
 namespace = UnknownMap()

@@ -47,6 +31,31 @@
     're'
 ]

+MESSAGES_ETC = [
+    '*** Server going down!',
+    '*** Currently connected: ',
+    (
+        '*** Available commands:\r\n'
+        '***  /help -- get help\r\n'
+        '***  /quit -- disconnect\r\n'
+        '***  /me -- perform an action\r\n'
+        '***  /who -- list connected users\r\n'
+        '***  /nick -- change your nickname to something better'
+    ),
+    '*** %s is now known as %s.',
+    '*** That nickname is invalid.',
+    '*** Type "/help" for help.',
+    '*** Hello! What is your nickname?',
+    '*** That nickname is already in use.',
+    '*** Incoming connection!',
+    '*** No such command: /%s',
+    '*** %s has joined.',
+    '^/([A-z]*)\\s*(.*)$',
+    '^[A-z0-9_]+$',
+    '*** %s has left.',
+    '* %s %s',
+    '<%s> %s'
+]

 main = lambda port: (
                 map(
@@ -76,16 +85,13 @@
                                     namespace.s['sys.exit']()
                                     if namespace.f
                                     else (
-                                        [namespace.sa(main_store.l[0], namespace.o)] and
+                                        [namespace.sa(MESSAGES_ETC[0], namespace.o)] and
                                         namespace.u('f', True) or
                                         namespace.fc(namespace.l)
                                     )
                                 )
                             ] * 2
                         ),
-                        lambda: setattr(
-                            main_store, 'l', main_store.l.decode('base64').split('~~~')
-                        ),
                         lambda: (
                             'sw',
                             namespace.s['types.Functio' 'nType'](
@@ -117,29 +123,29 @@
                         lambda: namespace.update(
                             si=(lambda o: o.__setitem__),
                             di=(lambda o: o.__delitem__),
-                            cr=namespace.s['re.compile'](main_store.l[11]),
+                            cr=namespace.s['re.compile'](MESSAGES_ETC[11]),
                             nn={},
                             dp=lambda s, d, c, l: namespace.dd.get(c, d)(s, l, c),
                             dd=dict(
                                 me=lambda s, l, c: (
-                                    namespace.sa(main_store.l[14] % (namespace.nn[s], l))
+                                    namespace.sa(MESSAGES_ETC[14] % (namespace.nn[s], l))
                                 ),
                                 quit=lambda s, l, c: namespace.c(s),
                                 who=lambda s, l, c: namespace.ws(
-                                    s, main_store.l[1] + ', '.join(namespace.s['nn.values']())
+                                    s, MESSAGES_ETC[1] + ', '.join(namespace.s['nn.values']())
                                 ),
-                                help=lambda s, l, c: namespace.ws(s, main_store.l[2]),
+                                help=lambda s, l, c: namespace.ws(s, MESSAGES_ETC[2]),
                                 nick=lambda s, l, c: (
                                     (
                                         (
-                                            [namespace.sa(main_store.l[3] % (namespace.nn[s], l))] and
+                                            [namespace.sa(MESSAGES_ETC[3] % (namespace.nn[s], l))] and
                                             namespace.si(namespace.nn)(s, l)
                                         )
                                         if namespace.nr.match(l)
-                                        else namespace.ws(s, main_store.l[4])
+                                        else namespace.ws(s, MESSAGES_ETC[4])
                                     )
                                     if l not in namespace.nn.values()
-                                    else namespace.ws(s, main_store.l[7])
+                                    else namespace.ws(s, MESSAGES_ETC[7])
                                 )
                             ),
                             so=(namespace.u('f', False) or [namespace.l]),
@@ -148,7 +154,7 @@
                             o=(lambda: filter(lambda s:  s is not namespace.l, namespace.so)),
                             w=(lambda: filter(namespace.ob.__getitem__, namespace.o())),
                             ws=(lambda s, l: namespace.ob[s].append(l + '\r\n')),
-                            nr=namespace.s['re.compile'](main_store.l[12]),
+                            nr=namespace.s['re.compile'](MESSAGES_ETC[12]),
                             sa=(
                                 lambda d, f=namespace.ro: map(
                                     lambda s: namespace.ws(s, d),
@@ -158,7 +164,7 @@
                             fs=set(),
                             c=(
                                 lambda s: [
-                                    namespace.sa(main_store.l[13] % namespace.nn[s])
+                                    namespace.sa(MESSAGES_ETC[13] % namespace.nn[s])
                                     if s in namespace.nn and s not in namespace.fs
                                     else None
                                 ] and namespace.s['fs.add'](s)
@@ -188,7 +194,7 @@
                                                     apply,
                                                     [
                                                         lambda: (
-                                                            namespace.sa(main_store.l[8])
+                                                            namespace.sa(MESSAGES_ETC[8])
                                                         ),
                                                         lambda: (
                                                             namespace.so.append(
@@ -197,7 +203,7 @@
                                                         ),
                                                         lambda: namespace.ws(
                                                             namespace.so[-1],
-                                                            main_store.l[6]
+                                                            MESSAGES_ETC[6]
                                                         )
                                                     ]
                                                 ) if s is namespace.l else map(
@@ -263,7 +269,7 @@
                                         lambda (s, l): (
                                             (
                                                 namespace.sa(
-                                                    main_store.l[15] % (
+                                                    MESSAGES_ETC[15] % (
                                                         namespace.nn[s],
                                                         l[1:]
                                                         if l.startswith('//')
@@ -280,7 +286,7 @@
                                                         lambda s, l, c: (
                                                             namespace.ws(
                                                                 s,
-                                                                main_store.l[9] % c
+                                                                MESSAGES_ETC[9] % c
                                                             )
                                                         ),
                                                         *namespace.s['cr.match'](
@@ -294,14 +300,14 @@
                                                 (
                                                     (
                                                         namespace.si(namespace.nn)(s, l) or
-                                                        [namespace.ws(s, main_store.l[5])] and
-                                                        namespace.sa(main_store.l[10] % l)
+                                                        [namespace.ws(s, MESSAGES_ETC[5])] and
+                                                        namespace.sa(MESSAGES_ETC[10] % l)
                                                     )
                                                     if namespace.nr.match(l)
-                                                    else namespace.ws(s, main_store.l[4])
+                                                    else namespace.ws(s, MESSAGES_ETC[4])
                                                 )
                                                 if l not in namespace.nn.values()
-                                                else namespace.ws(s, main_store.l[7])
+                                                else namespace.ws(s, MESSAGES_ETC[7])
                                             )
                                         ),
                                         [

And again, the test suite passes! Now instead of using the list, we can replace the list entries and item accessors with simple constants.

--- chatservers/replace_main_store_2.py 2014-06-03 15:59:45.000000000 -0500
+++ chatservers/replace_main_store_3.py 2014-06-04 04:19:13.000000000 -0500
@@ -31,31 +31,29 @@
     're'
 ]

-MESSAGES_ETC = [
-    '*** Server going down!',
-    '*** Currently connected: ',
-    (
-        '*** Available commands:\r\n'
-        '***  /help -- get help\r\n'
-        '***  /quit -- disconnect\r\n'
-        '***  /me -- perform an action\r\n'
-        '***  /who -- list connected users\r\n'
-        '***  /nick -- change your nickname to something better'
-    ),
-    '*** %s is now known as %s.',
-    '*** That nickname is invalid.',
-    '*** Type "/help" for help.',
-    '*** Hello! What is your nickname?',
-    '*** That nickname is already in use.',
-    '*** Incoming connection!',
-    '*** No such command: /%s',
-    '*** %s has joined.',
-    '^/([A-z]*)\\s*(.*)$',
-    '^[A-z0-9_]+$',
-    '*** %s has left.',
-    '* %s %s',
-    '<%s> %s'
-]
+SERVER_EXIT_MESSAGE = '*** Server going down!'
+WHO_MESSAGE_PREFIX = '*** Currently connected: '
+HELP_RESPONSE = (
+    '*** Available commands:\r\n'
+    '***  /help -- get help\r\n'
+    '***  /quit -- disconnect\r\n'
+    '***  /me -- perform an action\r\n'
+    '***  /who -- list connected users\r\n'
+    '***  /nick -- change your nickname to something better'
+)
+NICK_RESPONSE_FORMAT = '*** %s is now known as %s.'
+NICK_RESPONSE_ERROR = '*** That nickname is invalid.'
+HELP_MESSAGE = '*** Type "/help" for help.'
+GREETING_PROMPT = '*** Hello! What is your nickname?'
+NICK_NONUNIQUE_ERROR = '*** That nickname is already in use.'
+INCOMING_MESSAGE = '*** Incoming connection!'
+COMMAND_ERROR_FORMAT = '*** No such command: /%s'
+JOIN_MESSAGE_FORMAT = '*** %s has joined.'
+COMMAND_PATTERN = '^/([A-z]*)\\s*(.*)$'
+WORD_PATTERN = '^[A-z0-9_]+$'
+PART_MESSAGE_FORMAT = '*** %s has left.'
+ACTION_MESSAGE_FORMAT = '* %s %s'
+CHAT_MESSAGE_FORMAT = '<%s> %s'

 main = lambda port: (
                 map(
@@ -85,7 +83,7 @@
                                     namespace.s['sys.exit']()
                                     if namespace.f
                                     else (
-                                        [namespace.sa(MESSAGES_ETC[0], namespace.o)] and
+                                        [namespace.sa(SERVER_EXIT_MESSAGE, namespace.o)] and
                                         namespace.u('f', True) or
                                         namespace.fc(namespace.l)
                                     )
@@ -123,29 +121,29 @@
                         lambda: namespace.update(
                             si=(lambda o: o.__setitem__),
                             di=(lambda o: o.__delitem__),
-                            cr=namespace.s['re.compile'](MESSAGES_ETC[11]),
+                            cr=namespace.s['re.compile'](COMMAND_PATTERN),
                             nn={},
                             dp=lambda s, d, c, l: namespace.dd.get(c, d)(s, l, c),
                             dd=dict(
                                 me=lambda s, l, c: (
-                                    namespace.sa(MESSAGES_ETC[14] % (namespace.nn[s], l))
+                                    namespace.sa(ACTION_MESSAGE_FORMAT % (namespace.nn[s], l))
                                 ),
                                 quit=lambda s, l, c: namespace.c(s),
                                 who=lambda s, l, c: namespace.ws(
-                                    s, MESSAGES_ETC[1] + ', '.join(namespace.s['nn.values']())
+                                    s, WHO_MESSAGE_PREFIX + ', '.join(namespace.s['nn.values']())
                                 ),
-                                help=lambda s, l, c: namespace.ws(s, MESSAGES_ETC[2]),
+                                help=lambda s, l, c: namespace.ws(s, HELP_RESPONSE),
                                 nick=lambda s, l, c: (
                                     (
                                         (
-                                            [namespace.sa(MESSAGES_ETC[3] % (namespace.nn[s], l))] and
+                                            [namespace.sa(NICK_RESPONSE_FORMAT % (namespace.nn[s], l))] and
                                             namespace.si(namespace.nn)(s, l)
                                         )
                                         if namespace.nr.match(l)
-                                        else namespace.ws(s, MESSAGES_ETC[4])
+                                        else namespace.ws(s, NICK_RESPONSE_ERROR)
                                     )
                                     if l not in namespace.nn.values()
-                                    else namespace.ws(s, MESSAGES_ETC[7])
+                                    else namespace.ws(s, NICK_NONUNIQUE_ERROR)
                                 )
                             ),
                             so=(namespace.u('f', False) or [namespace.l]),
@@ -154,7 +152,7 @@
                             o=(lambda: filter(lambda s:  s is not namespace.l, namespace.so)),
                             w=(lambda: filter(namespace.ob.__getitem__, namespace.o())),
                             ws=(lambda s, l: namespace.ob[s].append(l + '\r\n')),
-                            nr=namespace.s['re.compile'](MESSAGES_ETC[12]),
+                            nr=namespace.s['re.compile'](WORD_PATTERN),
                             sa=(
                                 lambda d, f=namespace.ro: map(
                                     lambda s: namespace.ws(s, d),
@@ -164,7 +162,7 @@
                             fs=set(),
                             c=(
                                 lambda s: [
-                                    namespace.sa(MESSAGES_ETC[13] % namespace.nn[s])
+                                    namespace.sa(PART_MESSAGE_FORMAT % namespace.nn[s])
                                     if s in namespace.nn and s not in namespace.fs
                                     else None
                                 ] and namespace.s['fs.add'](s)
@@ -194,7 +192,7 @@
                                                     apply,
                                                     [
                                                         lambda: (
-                                                            namespace.sa(MESSAGES_ETC[8])
+                                                            namespace.sa(INCOMING_MESSAGE)
                                                         ),
                                                         lambda: (
                                                             namespace.so.append(
@@ -203,7 +201,7 @@
                                                         ),
                                                         lambda: namespace.ws(
                                                             namespace.so[-1],
-                                                            MESSAGES_ETC[6]
+                                                            GREETING_PROMPT
                                                         )
                                                     ]
                                                 ) if s is namespace.l else map(
@@ -269,7 +267,7 @@
                                         lambda (s, l): (
                                             (
                                                 namespace.sa(
-                                                    MESSAGES_ETC[15] % (
+                                                    CHAT_MESSAGE_FORMAT % (
                                                         namespace.nn[s],
                                                         l[1:]
                                                         if l.startswith('//')
@@ -286,7 +284,7 @@
                                                         lambda s, l, c: (
                                                             namespace.ws(
                                                                 s,
-                                                                MESSAGES_ETC[9] % c
+                                                                COMMAND_ERROR_FORMAT % c
                                                             )
                                                         ),
                                                         *namespace.s['cr.match'](
@@ -300,14 +298,14 @@
                                                 (
                                                     (
                                                         namespace.si(namespace.nn)(s, l) or
-                                                        [namespace.ws(s, MESSAGES_ETC[5])] and
-                                                        namespace.sa(MESSAGES_ETC[10] % l)
+                                                        [namespace.ws(s, HELP_MESSAGE)] and
+                                                        namespace.sa(JOIN_MESSAGE_FORMAT % l)
                                                     )
                                                     if namespace.nr.match(l)
-                                                    else namespace.ws(s, MESSAGES_ETC[4])
+                                                    else namespace.ws(s, NICK_RESPONSE_ERROR)
                                                 )
                                                 if l not in namespace.nn.values()
-                                                else namespace.ws(s, MESSAGES_ETC[7])
+                                                else namespace.ws(s, NICK_NONUNIQUE_ERROR)
                                             )
                                         ),
                                         [

Much better, now insted of the nonobvious item access, we gave semantic meaning to at least part of the code.

Use the Structure, Luke!

Now that we've moved the namespace object creation to the top level, and eliminated the other UnknownMap instance, the code is substantially simpler. The main function now just returns the result of a map call.

main = lambda port: (
                map(
                    map_function,  # takes argument named "r"
                    [
                        # A bunch of lambdas (functions)
                    ]
                )
            )

This still isn't obvious, so let's rewrite it to be more readable.

def main(port):
    return map(
        map_function,  # takes argument named "r"
        [
            # A bunch of lambdas (functions)
        ]
    )

Better, but since main is the entry point of the whole program, the return value gets completely ignored. No need to return the result of the map, so we can just switch it to a normal for loop.

functions_to_run = [
    # A bunch of lambdas (functions)
]
def main(port):
    for func in functions_to_run:
        map_function(func)  # takes argument named "r"

That's a lot better, but we still have that probably-crazy higher-order function to deal with, although now we have a defined place to put it. Let's take a look.

map_function = lambda r:  (
    lambda rr: (
        setattr(namespace, *rr)  # ** REMOVEME!!!
        if (type(rr) is tuple and len(rr) == 2)
        else None
    )
)(r())

This is pretty hard to read. Let's use normal function defs.

def map_function(r):

    def inner(rr):
        return (
            setattr(namespace, *rr)  # ** REMOVEME!!!
            if (type(rr) is tuple and len(rr) == 2)
            else None
        )

    return inner(r())

Better, but there's no need to use the ternary if/else there. Also since the setattr only happens if rr has length 2, we can make it a bit more explicit. Not the else block just returns None, so it can be dropped completely.

def map_function(r):

    def inner(rr):
        if type(rr) is tuple and len(rr) == 2:
            setattr(namespace, rr[0], rr[1])

    return inner(r())

All together now...

functions_to_run = [
    # A bunch of lambdas (functions)
]

def map_function(r):

    def inner(rr):
        if type(rr) is tuple and len(rr) == 2:
            setattr(namespace, rr[0], rr[1])

    return inner(r())

def main(port):
    for func in functions_to_run:
        map_function(func)

And we can still do better. map_function always calls each function from the list (for the side effects), and it also does another operation if the function returns a 2-tuple... this can be changed to simply happen in that for loop.

functions_to_run = [
    # A bunch of lambdas (functions)
]

def main(port):
    for func in functions_to_run:
        result = func()
        if type(result) is tuple and len(result) == 2:
            name, value = result  # names for setattr args
            setattr(namespace, name, value)

That's way more readable, and understandable. Let's update the file with these changes. Here I left the indentation far out so the diff wouldn't be too long.

--- chatservers/replace_main_store_3.py 2014-06-04 04:19:13.000000000 -0500
+++ chatservers/normal_main_function.py 2014-06-06 04:25:56.000000000 -0500
@@ -55,16 +55,8 @@
 ACTION_MESSAGE_FORMAT = '* %s %s'
 CHAT_MESSAGE_FORMAT = '<%s> %s'

-main = lambda port: (
-                map(
-                    lambda r:  (
-                        lambda rr: (
-                            setattr(namespace, *rr)
-                            if (type(rr) is tuple and len(rr) == 2)
-                            else None
-                        )
-                    )(r()),
-                    [
+
+functions_to_run = [
                         lambda: map(
                             setattr,
                             *zip(
@@ -340,6 +332,12 @@
                             iter(lambda: bool(namespace.so), False)
                         ),
                         lambda: namespace.s['l.' 'close']()
-                    ]
-                )
-            )
+]
+
+
+def main(port):
+    for func in functions_to_run:
+        result = func()
+        if type(result) is tuple and len(result) == 2:
+            name, value = result  # names for setattr args
+            setattr(namespace, name, value)

Although when I run it...

/chatservers$ python -c 'import normal_main_function;
normal_main_function.main(6667)'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "normal_main_function.py", line 340, in main
    result = func()
  File "normal_main_function.py", line 108, in <lambda>
    lambda: namespace.s['l.bind'](('', port)),
NameError: global name 'port' is not defined

Whoops, I forgot about that. For the time being, the simplest fix is to just move the list literal inside the main function.

--- chatservers/normal_main_function.py 2014-06-06 04:25:56.000000000 -0500
+++ chatservers/normal_main_function_2.py       2014-06-06 04:53:24.000000000 -0500
@@ -56,7 +56,8 @@
 CHAT_MESSAGE_FORMAT = '<%s> %s'


-functions_to_run = [
+def main(port):
+    functions_to_run = [
                         lambda: map(
                             setattr,
                             *zip(
@@ -332,10 +333,7 @@
                             iter(lambda: bool(namespace.so), False)
                         ),
                         lambda: namespace.s['l.' 'close']()
-]
-
-
-def main(port):
+    ]
     for func in functions_to_run:
         result = func()
         if type(result) is tuple and len(result) == 2:

Since it runs, and the test suite passes, we can now start working through the list of functions.

Names are Good

Because the functions in this code are all lambdas, we have to actually look at the function code in order to reason about what they do. This takes up mental stack space that could be better used to reason about the semantics of the functions, as well as the program as a whole. Let's work through each function, and give them all names.

Refactor Before Rewriting

The first one looks a little scary, but look how far we've come! We can't just give up now!

lambda: map(
    setattr,
    *zip(
        *[  # ** REMOVEME!!
            (namespace, m, __import__(m))
            for m
            in MODULES
        ]
    )
)

As before, we can flip the map call into a simple for loop. However, the zip result and the argument to zip are tricky because of the * argument unpacking syntax, so let's handle that first.. Working from inside to out, we get this.

def noname():
    inner_list = [(namespace, m, __import__(m)) for m in MODULES]
    zip_result = zip(*inner_list)  # ** REMOVEME!!!
    # no return, since this map will be a list containing only None
    map(setattr, *zip_result)  # ** REMOVEME!!!

It's obvious that inner_list is a list of 3-tuples, each containing the namespace object, the module name, and the module object. The zip call is equivalent to this:

zip_result = zip(*inner_list)  # ** REMOVEME!!!
zip_result = zip(
    (namespace, firstmodule_name, firstmodule),
    (namespace, secondmodule_name, secondmodule),
    # and so on
)

So zip is going to zipper together these things, and we'll get a list of three tuples. The first tuple contains nothing but the namespace object, referenced len(MODULES) times. The second is all the module names, and the third is all the module objects themselves. Since the map call uses argument unpacking as well, it effectively gets called with four arguments, as shown here:

# assuming len(MODULES) == 3 for conciseness
zip_result = [
    (namespace, namespace, namespace),
    (firstmodule_name, secondmodule_name, thirdmodule_name),
    (firstmodule, secondmodule, thirdmodule)
]
map(setattr, namespaces_tuple, module_name_tuple, module_tuple)

And looking at the map documentation, we can see that in this form, the function will be applied to the three tuples in parallel, so setattr will end up being called like this:

setattr(namespace, firstmodule_name, firstmodule)
setattr(namespace, secondmodule_name, secondmodule)
setattr(namespace, thirdmodule_name, thirdmodule)

That's really easy to do in a for loop, and we only need to loop over the MODULES list. Here's the completed function, and now we can name it!

def import_to_namespace():
    for module_name in MODULES:
        setattr(namespace, module_name, __import__(module_name))

You might think this is an ugly thing because of the setattr and the import-by-name. Well, you'd be right. However, before we can make this better, we need to understand what other parts of the code require this. Let's be methodical, and work on making these functions more understandable before making sweeping changes.

Let's replace the funky lambda with this better function, and see if the tests pass. Note that I backed off the indent on the list of lambdas in an intermediate file before this diff, for clarity.

--- chatservers/reset_indent.py 2014-06-07 09:01:41.000000000 -0500
+++ chatservers/normal_other_functions.py       2014-06-07 09:03:49.000000000 -0500
@@ -55,19 +55,13 @@
 ACTION_MESSAGE_FORMAT = '* %s %s'
 CHAT_MESSAGE_FORMAT = '<%s> %s'

+def import_to_namespace():
+    for module_name in MODULES:
+        setattr(namespace, module_name, __import__(module_name))

 def main(port):
     functions_to_run = [
-        lambda: map(
-            setattr,
-            *zip(
-                *[
-                    (namespace, m, __import__(m))
-                    for m
-                    in MODULES
-                ]
-            )
-        ),
+        import_to_namespace,
         lambda: map(
             namespace.s['signal.signal'],
             (namespace.s['signal.SIGINT'], namespace.s['signal.SIGTERM']),

It works! Next victim!

Signal Handling

lambda: map(
    namespace.s['signal.signal'],
    (namespace.s['signal.SIGINT'], namespace.s['signal.SIGTERM']),
    [
        lambda s, f: (
            namespace.s['sys.exit']()
            if namespace.f
            else (
                [namespace.sa(SERVER_EXIT_MESSAGE, namespace.o)] and
                namespace.u('f', True) or
                namespace.fc(namespace.l)
            )
        )
    ] * 2
)

This uses the funky s property of the namespace object. Remember that? With the inner unknown type and the reduce call? Well, it'd be nice to get rid of that, but it's still to early for that. Let's use what we learned about map from the last function. We could just a normal for loop, but since the function will only be called twice (length of the map iterables is 2), we can just call the function explicitly. The function is namespace.s['signal.signal'] which must be a function of two parameters, since there are two iterable arguments to map.

def noname():
    mapped_func = namespace.s['signal.signal']
    first_args = (
        namespace.s['signal.SIGINT'],
        namespace.s['signal.SIGTERM']
    )
    second_args_func = lambda s, f: (
            namespace.s['sys.exit']()
            if namespace.f
            else (
                [namespace.sa(SERVER_EXIT_MESSAGE, namespace.o)] and
                namespace.u('f', True) or
                namespace.fc(namespace.l)
            )
        )
    second_args = [second_args_func, second_args_func]
    mapped_func(first_args[0], second_args[0])
    mapped_func(first_args[1], second_args[1])

Now let's take a look at that inner function. It has two parameters and doesn't appear to return anything useful, rather it is called for its side effects. There's that ternary expresision again, easy enough to make it into a normal if/else.

def second_args_func(s, f):
    if namespace.f:
        namespace.s['sys.exit']()
    else:
        (
            [namespace.sa(SERVER_EXIT_MESSAGE, namespace.o)] and
            namespace.u('f', True) or
            namespace.fc(namespace.l)
        )

That last bit is a little tricky. The or operator binds tighter than and, so a and b or c will be evaluated like a and (b or c). The left expression is always going to evaluate true, since it's a non-empty list, so the or expression will always be evaluated. The tricky part is how the or expression is evaluated. If the right side is true, the or short-circuits and doesn't evaluate the right side. With our example, this means b or c is equivalent to if not b: c. Here's the translation in the code itself:

def second_args_func(s, f):
    if namespace.f:
        namespace.s['sys.exit']()
    else:
        namespace.sa(SERVER_EXIT_MESSAGE, namespace.o)
        if not namespace.u('f', True):
            namespace.fc(namespace.l)

We can put that back into the outer function now.

def noname():
    mapped_func = namespace.s['signal.signal']
    first_args = (
        namespace.s['signal.SIGINT'],
        namespace.s['signal.SIGTERM']
    )

    def second_args_func(s, f):
        if namespace.f:
            namespace.s['sys.exit']()
        else:
            namespace.sa(SERVER_EXIT_MESSAGE, namespace.o)
            if not namespace.u('f', True):
                namespace.fc(namespace.l)

    second_args = [second_args_func, second_args_func]
    mapped_func(first_args[0], second_args[0])
    mapped_func(first_args[1], second_args[1])

And then clean it up a bit. We don't need those iterables at all.

def noname():
    mapped_func = namespace.s['signal.signal']

    def second_args_func(s, f):
        if namespace.f:
            namespace.s['sys.exit']()
        else:
            namespace.sa(SERVER_EXIT_MESSAGE, namespace.o)
            if not namespace.u('f', True):
                namespace.fc(namespace.l)

    mapped_func(namespace.s['signal.SIGINT'], second_args_func)
    mapped_func(namespace.s['signal.SIGTERM'], second_args_func)

Based on the item access on the namespace.s property, I have a feeling this sets up handlers for signals via the signal.signal function. This might be wrong, but we still need a few names to use.

def add_signal_handlers():
    signal_signal = namespace.s['signal.signal']

    def signal_handler(s, f):
        if namespace.f:
            namespace.s['sys.exit']()
        else:
            namespace.sa(SERVER_EXIT_MESSAGE, namespace.o)
            if not namespace.u('f', True):
                namespace.fc(namespace.l)

    signal_signal(namespace.s['signal.SIGINT'], signal_handler)
    signal_signal(namespace.s['signal.SIGTERM'], signal_handler)

Looks ok. Let's try it.

--- chatservers/normal_other_functions.py       2014-06-07 09:03:49.000000000 -0500
+++ chatservers/normal_other_functions_2.py     2014-06-07 10:19:53.000000000 -0500
@@ -59,24 +59,24 @@
     for module_name in MODULES:
         setattr(namespace, module_name, __import__(module_name))

+def add_signal_handlers():
+    signal_signal = namespace.s['signal.signal']
+
+    def signal_handler(s, f):
+        if namespace.f:
+            namespace.s['sys.exit']()
+        else:
+            namespace.sa(SERVER_EXIT_MESSAGE, namespace.o)
+            if not namespace.u('f', True):
+                namespace.fc(namespace.l)
+
+    signal_signal(namespace.s['signal.SIGINT'], signal_handler)
+    signal_signal(namespace.s['signal.SIGTERM'], signal_handler)
+
 def main(port):
     functions_to_run = [
         import_to_namespace,
-        lambda: map(
-            namespace.s['signal.signal'],
-            (namespace.s['signal.SIGINT'], namespace.s['signal.SIGTERM']),
-            [
-                lambda s, f: (
-                    namespace.s['sys.exit']()
-                    if namespace.f
-                    else (
-                        [namespace.sa(SERVER_EXIT_MESSAGE, namespace.o)] and
-                        namespace.u('f', True) or
-                        namespace.fc(namespace.l)
-                    )
-                )
-            ] * 2
-        ),
+        add_signal_handlers,
         lambda: (
             'sw',
             namespace.s['types.Functio' 'nType'](

The tests pass, and it also seems to behave the same when I interrupt the process with CTRL+C. On to the next function.

Select Your Weapon

lambda: (
    'sw',
    namespace.s['types.Functio' 'nType'](
        compile(
            "try:\n"
            "\tv = namespace.select.select(namespace.so, namespace.w(), [])\n"
            "except namespace.select.error, e:\n"
            "\tif e[0] != namespace.errno.EINTR: raise\n"
            "else:\n"
            "\tnamespace.u('sr', v)",
            '',
            'exec'
        ),
        dict(namespace=namespace, OSError=OSError)
    )
)

Right away I can tell this is somewhat special. First of all, it's the only part we see Python statements. Second, the function returns a tuple of length two, thus it will be treated specially by the for loop at the end of our main function, and we need to be sure to return that tuple. After the loop calls this function, it uses the two tuple elements as arguments to setattr, acting on the namespace object.

def select_loop_namespace_sw_attr():
    second_element = namespace.s['types.Functio' 'nType'](
        compile(
            "try:\n"
            "\tv = namespace.select.select(namespace.so, namespace.w(), [])\n"
            "except namespace.select.error, e:\n"
            "\tif e[0] != namespace.errno.EINTR: raise\n"
            "else:\n"
            "\tnamespace.u('sr', v)",
            '',
            'exec'
        ),
        dict(namespace=namespace, OSError=OSError)
    )
    return 'sw', second_element

Again, I'm weary of that s attribute from namespace, but it seems like it did do what I though it should. The second element of the tuple will be the result of a call to types.FunctionType. The docs don't tell us how to use that thing, but help() does!

>>> help(types.FunctionType)
Help on class function in module __builtin__:

class function(object)
 |  function(code, globals[, name[, argdefs[, closure]]])
 |
 |  Create a function object from a code object and a dictionary.
 |  The optional name string overrides the name from the code object.
 |  The optional argdefs tuple specifies the default argument values.
 |  The optional closure tuple supplies the bindings for free variables.

So it looks like the second argument is the global dictionary. This is starting to make sense. Let's try rewriting it.

def select_loop_namespace_sw_attr():

    def second_element():
        try:
            v = namespace.select.select(namespace.so, namespace.w(), [])
        except namespace.select.error, e:
            if e[0] != namespace.errno.EINTR:
                raise
        else:
            namespace.u('sr', v)

    return 'sw', second_element

Ok, I don't know much about using the select module, but I do know that it provides a way to wait on file descriptors. I'm betting this is meat of the main processing loop of the chat server. Instead of using this nested function, let's directly apply the inner function to the namespace.sw attribute. This will make things clearer, I hope.

--- chatservers/normal_other_functions_2.py     2014-06-07 10:19:53.000000000 -0500
+++ chatservers/normal_other_functions_3.py     2014-06-07 11:00:37.000000000 -0500
@@ -73,27 +73,22 @@
     signal_signal(namespace.s['signal.SIGINT'], signal_handler)
     signal_signal(namespace.s['signal.SIGTERM'], signal_handler)

+def do_select_routine():
+    try:
+        v = namespace.select.select(namespace.so, namespace.w(), [])
+    except namespace.select.error, e:
+        if e[0] != namespace.errno.EINTR:
+            raise
+    else:
+        namespace.u('sr', v)
+
 def main(port):
+    namespace.sw = do_select_routine
+
     functions_to_run = [
         import_to_namespace,
         add_signal_handlers,
         lambda: (
-            'sw',
-            namespace.s['types.Functio' 'nType'](
-                compile(
-                    "try:\n"
-                    "\tv = namespace.select.select(namespace.so, namespace.w(), [])\n"
-                    "except namespace.select.error, e:\n"
-                    "\tif e[0] != namespace.errno.EINTR: raise\n"
-                    "else:\n"
-                    "\tnamespace.u('sr', v)",
-                    '',
-                    'exec'
-                ),
-                dict(namespace=namespace, OSError=OSError)
-            )
-        ),
-        lambda: (
             'l',
             namespace.s['socket.socket'](
                 namespace.s['socket.AF_INET'],

Tests pass!

Keep It Simple, Stupid!

The next four functions are pretty simple. So is the final one in the list, we'll knock that one out too.

first = lambda: (
    'l',
    namespace.s['socket.socket'](
        namespace.s['socket.AF_INET'],
        namespace.s['socket.SOCK_STREAM']
    )
)
second = lambda: namespace.s['l.bind'](('', port))
third = lambda: namespace.s['l.listen'](5)
fourth = lambda: (
    'ro', lambda: filter(lambda s: s in namespace.nn,  namespace.so)
)
final = lambda: namespace.s['l.' 'close']()

The first one returns a 2-tuple, so its effect is to create a socket (with socket.socket) and assign it to the l attribute on the namespace object. The second and third make that socket bind and listen to the port. The fourth returns a 2-tuple, so its effect is to assign the inner filtering function to the ro attribute on namespace. The last one simply closes the socket. Here they are defined normally.

namespace.l = namespace.s['socket.socket'](
    namespace.s['socket.AF_INET'],
    namespace.s['socket.SOCK_STREAM']
)

def bind_socket():
    namespace.s['l.bind'](('', port))

def listen_socket():
    namespace.s['l.listen'](5)

def filter_namespace_ro_attrib():
    return filter(lambda s: s in namespace.nn,  namespace.so)
namespace.ro = filter_namespace_ro_attrib

def close_socket():
    namespace.s['l.close']()

Pretty good, but we can do better than the filter.

def filter_namespace_ro_attrib():
    return [s for s in namespace.so if s in namespace.so]
namespace.ro = filter_namespace_ro_attrib

The bind_socket function must be inside the main function, but the rest can go outside. Also, in order for the namespace.s['socket.socket'] part to work, we need to call import_to_namespace first. Here's what it becomes:

--- chatservers/normal_other_functions_3.py     2014-06-07 11:00:37.000000000 -0500
+++ chatservers/normal_other_functions_4.py     2014-06-07 13:47:23.000000000 -0500
@@ -82,24 +82,32 @@
     else:
         namespace.u('sr', v)

+def listen_socket():
+    namespace.s['l.listen'](5)
+
+def filter_namespace_ro_attrib():
+    return [s for s in namespace.so if s in namespace.so]
+
+def close_socket():
+    namespace.s['l.close']()
+
 def main(port):
+    import_to_namespace()
+
     namespace.sw = do_select_routine
+    namespace.l = namespace.s['socket.socket'](
+        namespace.s['socket.AF_INET'],
+        namespace.s['socket.SOCK_STREAM']
+    )
+    namespace.ro = filter_namespace_ro_attrib
+
+    def bind_socket():
+        namespace.s['l.bind'](('', port))

     functions_to_run = [
-        import_to_namespace,
         add_signal_handlers,
-        lambda: (
-            'l',
-            namespace.s['socket.socket'](
-                namespace.s['socket.AF_INET'],
-                namespace.s['socket.SOCK_STREAM']
-            )
-        ),
-        lambda: namespace.s['l.bind'](('', port)),
-        lambda: namespace.s['l.listen'](5),
-        lambda: (
-            'ro', lambda: filter(lambda s: s in namespace.nn,  namespace.so)
-        ),
+        bind_socket,
+        listen_socket,
         lambda: namespace.update(
             si=(lambda o: o.__setitem__),
             di=(lambda o: o.__delitem__),
@@ -321,7 +329,7 @@
             ),
             iter(lambda: bool(namespace.so), False)
         ),
-        lambda: namespace.s['l.' 'close']()
+        close_socket
     ]
     for func in functions_to_run:
         result = func()

All the tests pass. One more function can reasonably be tackled this way.

Update ALL the Names!

This one looks daunting, but that's really only because of how long it is. All it does call namespace.update with a lot of different things.

lambda: namespace.update(
    si=(lambda o: o.__setitem__),
    di=(lambda o: o.__delitem__),
    cr=namespace.s['re.compile'](COMMAND_PATTERN),
    nn={},
    dp=lambda s, d, c, l: namespace.dd.get(c, d)(s, l, c),
    dd=dict(
        me=lambda s, l, c: (
            namespace.sa(ACTION_MESSAGE_FORMAT % (namespace.nn[s], l))
        ),
        quit=lambda s, l, c: namespace.c(s),
        who=lambda s, l, c: namespace.ws(
            s, WHO_MESSAGE_PREFIX + ', '.join(namespace.s['nn.values']())
        ),
        help=lambda s, l, c: namespace.ws(s, HELP_RESPONSE),
        nick=lambda s, l, c: (
            (
                (
                    [namespace.sa(NICK_RESPONSE_FORMAT % (namespace.nn[s], l))] and
                    namespace.si(namespace.nn)(s, l)
                )
                if namespace.nr.match(l)
                else namespace.ws(s, NICK_RESPONSE_ERROR)
            )
            if l not in namespace.nn.values()
            else namespace.ws(s, NICK_NONUNIQUE_ERROR)
        )
    ),
    so=(namespace.u('f', False) or [namespace.l]),
    ib=namespace.s['collections' '.defaultdict'](list),
    ob=namespace.s['collections.defaultdict'](list),
    o=(lambda: filter(lambda s:  s is not namespace.l, namespace.so)),
    w=(lambda: filter(namespace.ob.__getitem__, namespace.o())),
    ws=(lambda s, l: namespace.ob[s].append(l + '\r\n')),
    nr=namespace.s['re.compile'](WORD_PATTERN),
    sa=(
        lambda d, f=namespace.ro: map(
            lambda s: namespace.ws(s, d),
            f()
        )
    ),
    fs=set(),
    c=(
        lambda s: [
            namespace.sa(PART_MESSAGE_FORMAT % namespace.nn[s])
            if s in namespace.nn and s not in namespace.fs
            else None
        ] and namespace.s['fs.add'](s)
    ),
    fc=(
        lambda s: (
            [s.close()] and
            (namespace.so.remove(s) if s in namespace.so else None) or
            (namespace.fs.remove(s) if s in namespace.fs else None) or
            (namespace.di(namespace.ib)(s) if s in namespace.ib else None) or
            (namespace.di(namespace.ob)(s) if s in namespace.ob else None) or
            (namespace.di(namespace.nn)(s) if s in namespace.nn else None)
        )
    )
)

Let's start by changing those lambdas into normal functions, and name the functions something more descriptive than the attribute names used. I'll leave the longer lambdas out, so we can detail porting them later.

def update_namespace_lots():
    #si=(lambda o: o.__setitem__)
    def get_setitem_method(obj):
        return obj.__setitem__

    #di=(lambda o: o.__delitem__)
    def get_delitem_method(obj):
        return obj.__delitem__

    #cr=namespace.s['re.compile'](COMMAND_PATTERN)
    command_regex_obj = namespace.s['re.compile'](COMMAND_PATTERN)

    # dp=lambda s, d, c, l: namespace.dd.get(c, d)(s, l, c)
    def call_dd_func(s, default_func, c, l):
        func = namespace.dd.get(c, default_func)
        return func(s, l, c)

    #me=lambda s, l, c: (
    #    namespace.sa(ACTION_MESSAGE_FORMAT % (namespace.nn[s], l))
    #)
    def action_response(s, l, c):
        action_message = ACTION_MESSAGE_FORMAT % (namespace.nn[s], l)
        return namespace.sa(action_message)

    #quit=lambda s, l, c: namespace.c(s)
    def quit(s, l, c):
        return namespace.c(s)

    #who=lambda s, l, c: namespace.ws(
    #    s, WHO_MESSAGE_PREFIX + ', '.join(namespace.s['nn.values']())
    #)
    def who_response(s, l, c):
        names = namespace.s['nn.values']()
        message = WHO_MESSAGE_PREFIX + ', '.join(names)
        return namespace.ws(s, message)

    #help=lambda s, l, c: namespace.ws(s, HELP_RESPONSE)
    def help_response(s, l, c):
        return namespace.ws(s, HELP_RESPONSE)

    #o=(lambda: filter(lambda s:  s is not namespace.l, namespace.so))
    def filter_namespace_so_not_l():
        return [s for s in namespace.so if s is not namespace.l]

    #w=(lambda: filter(namespace.ob.__getitem__, namespace.o()))
    def filter_namespace_ob_by_getitem():
        return [x for x in namespace.o() if namespace.ob[x]]

    #ws=(lambda s, l: namespace.ob[s].append(l + '\r\n')),
    def append_namespace_ob_with_l_and_crlf(s, l):
        return namespace.ob[s].append(l + '\r\n')

    #nr=namespace.s['re.compile'](WORD_PATTERN)
    word_regex_object = namespace.s['re.compile'](WORD_PATTERN)

    #sa=(
    #    lambda d, f=namespace.ro: map(
    #        lambda s: namespace.ws(s, d),
    #        f()
    #    )
    #)
    def map_namespace_ws_over_func(d, func=namespace.ro):
        return [namespace.ws(s, d) for s in func()]

    namespace.update(
        si=get_setitem_method,
        di=get_delitem_method,
        cr=command_regex_obj,
        nn={},
        dp=call_dd_func,
        dd={
            'me': action_response,
            'quit': quit,
            'who': who_response,
            'help': help_response,
            'nick': lambda s, l, c: (
                (
                    (
                        [namespace.sa(NICK_RESPONSE_FORMAT % (namespace.nn[s], l))] and
                        namespace.si(namespace.nn)(s, l)
                    )
                    if namespace.nr.match(l)
                    else namespace.ws(s, NICK_RESPONSE_ERROR)
                )
                if l not in namespace.nn.values()
                else namespace.ws(s, NICK_NONUNIQUE_ERROR)
            )
        },
        so=(namespace.u('f', False) or [namespace.l]),
        ib=namespace.s['collections.defaultdict'](list),
        ob=namespace.s['collections.defaultdict'](list),
        o=filter_namespace_so_not_l,
        w=filter_namespace_ob_by_getitem,
        ws=append_namespace_ob_with_crlf,
        nr=word_regex_object,
        sa=map_namespace_ws_over_func,
        fs=set(),
        c=(
            lambda s: [
                namespace.sa(PART_MESSAGE_FORMAT % namespace.nn[s])
                if s in namespace.nn and s not in namespace.fs
                else None
            ] and namespace.s['fs.add'](s)
        ),
        fc=(
            lambda s: (
                [s.close()] and
                (namespace.so.remove(s) if s in namespace.so else None) or
                (namespace.fs.remove(s) if s in namespace.fs else None) or
                (namespace.di(namespace.ib)(s) if s in namespace.ib else None) or
                (namespace.di(namespace.ob)(s) if s in namespace.ob else None) or
                (namespace.di(namespace.nn)(s) if s in namespace.nn else None)
            )
        )
    )

Of course, all of this has been pretty easy. The next part gets a lot harder. We'll look at the next phase in another article, when I get around to it.