[Python-3000] A few small py3k wishes

Adam DePrince adam.deprince at gmail.com
Mon Apr 3 05:04:55 CEST 2006


On Sun, 2006-04-02 at 21:39 +0000, Talin wrote:
> Just a few things that have wanted while doing python
> programming. Maybe some of these are already doable
> or are already on someone's wish list...
> 
> -- The __main__ module should contain a file path like imported
> modules. in other words, every module should know where it
> came from. In fact, I see no reason why the __main__ module
> should have any different attributes than an imported module
> other than the name.
> 
> -- An easy way to make a case-insensitive, case-preserving
> dict that works with regular string keys.

One of my more recent abominations, my views PEP, would have addressed
this nicely.  And so would ... 

class caselessdict( dict ):
    
    def __new__( self ):
        retval = set.__new__( caselessdict )
        retval.cC = {}
        retval.Cc = {}
        return retval

    def __setitem__( self, key, value ):
        lkey = key.lower()
        self.cC[lkey] = key # need for deletion
        self.Cc[key] = lkey
        dict.__setitem__( self, key, value )

    def __getitem__( self, key  ):
        lkey = key.lower()
        dict.__getitem__( self, lkey )

    def myCapitalization( self, key ):
        return self.cC[key]
    
    def iteritems( self ):
        return (self.cC[key], values in dict.iteritems( self ))

    del __delitem__( self, key ):
        del( retval.Cc[ retval.cC[ key.lower()]])
        del( retval.cC[key.lower()] )
        dict.__delitem__( self, key.low..... 

... sorry, I'm all out of coffee.


> 
> -- An easy way to iterate over key, value pairs in a dict in
> sorted order. Currently you have to get the list of keys,
> sort them, and then lookup each value, OR you have to
> get the list of tuples and call sorted() with a key= arg
> containing a lambda function that extracts the first
> tuple element.

sorted( dict.items() )  

Doesn't Zope have a nice tree to do this in-place?

> 
> (Another one of my wild ideas was an "order by"
> clause for list comprehensions, but let's not go there.)

[x for x in sorted( l )] ??





More information about the Python-3000 mailing list