New Ordered Dictionery to Criticise

Fuzzyman fuzzyman at gmail.com
Tue Nov 29 04:23:06 EST 2005


Martin v. Löwis wrote:
> Fuzzyman wrote:
> > Criticism solicited (honestly) :-)
>
> A couple of minor points:
> - I would drop 2.2 compatibility

There are a lot of cheap hosting accounts where Python 2.2 is all that
is available. I would only drop support if there is some *compelling*
reason to do so.

> - self=self isn't needed in the functions, because of
>    nested scopes

Cool, I'll test this out.

> - popitem(self) can be rewritten as
>
>    def popitem(self):
>        try:
>            key = self._sequence.pop()
>        except IndexError:
>            # always try to give the same exception string as
>            # dict
>            raise KeyError("popitem(): dictionary is empty")
>        return key, self.pop(key)
>

Yup, that's nicer - thanks.

> - One problem with the FancyDict is that it allows
>    d.keys.append(100)

Are you sure ?

No file given.
Alias           System Command
------------------------------
cls             cls
copy            copy
ddir            dir /ad /on
dir             dir /on
echo            echo
ldir            dir /ad /on
ls              dir /on
mkdir           mkdir
ren             ren
rmdir           rmdir
------------------------------
Total number of aliases: 10
Movable Python
IPython Interactive Shell. See the manual for a list of features and
tips.
Ctrl-D to exit.

>>> from odict import FancyODict
>>> d = FancyODict()
>>> d
          Out[3]:{}

>>> d.keys
----------------------> d.keys()
           Out[4]:[]

>>> d.keys.append('anything')
---------------------------------------------------------------------------
exceptions.TypeError                                 Traceback (most
recent call
 last)

\configobj4\pythonutils\odict.py in append(self, item)
    713     def __iadd__(self, other): raise TypeError('Can\'t add in
place to keys')
    714     def __imul__(self, n): raise TypeError('Can\'t multiply
keys in place')
--> 715     def append(self, item): raise TypeError('Can\'t append
items to keys')
    716     def insert(self, i, item): raise TypeError('Can\'t insert
items into keys')
    717     def pop(self, i=-1): raise TypeError('Can\'t pop items from
keys')

TypeError: Can't append items to keys

>>>

All the best,

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml

> 
> Regards,
> Martin




More information about the Python-list mailing list