from __future__ import * 6.1.2004

2004-01-06

PEP 326 -- A Case for Top and Bottom Values

PEP 326 gets my vote, with one condition: change cmp.high and cmp.low! Why not just use max and min respectively? This follows the discussion Josiah Carlson and I had on his livejournal.

For the uninitiated, PEP 326 proposes that Python should have builtins that represent the smallest and largest possible object values regardless of object type. The smallest value will always return -1 for cmp unless it is compared with itself, in which case it returns 0. It's rather obvious what the largest value will do :)

posted at 18:19:28    #    comment []    trackback []
 

Abusing __getitem__ is fun

class SliceAbuser(object):
    def __init__(self, value):
        self.value = value
        self.items = []

    def __getitem__(self, items):
        if not isinstance(items, tuple):
            items = items,
        append = self.items.append
        for item in items:
            append((item.start, item.stop))
        return self

It doesn't look like much, right? Well, this hack basically allows you to use dict syntax on a __getitem__! Check this out:

s = SliceAbuser
resource, index, help, style, more, moreindex = 'resource', 'index', 'help', 'style', 'more', 'moreindex'
o = s(resource)[
    'index.html': s(index),
    'help.html': s(help),
    'style.css': s(style),
    'more': s(more)[
        'index.html': s(moreindex),
    ],
]

Yeah, it's a whole sitemap in Python syntax. Instead of strings, pretend that resource, index, etc. are all whatever kind of Resource objects your web framework uses (I'm thinking in Twisted). Don't believe me? Look at this:

def printSiteMap(obj, parents = ()):
    print '    ' * len(parents), '/'.join(parents), '->', obj.value
    for key, value in obj.items:
        printSiteMap(value, parents+(key,))
printSiteMap(o)
-> resource
   index.html -> index
   help.html -> help
   style.css -> style
   more -> more
       more/index.html -> moreindex
posted at 18:01:20    #    comment []    trackback []
January
MoTuWeThFrSaSu
    1 2 3 4
5 6 7 8 91011
12131415161718
19202122232425
262728293031 
Dec Feb

Bob's Rants

XML-Image Letterimage

© 2004, Bob Ippolito