[PATCH 1 of 2] sortdict: add iteritems method

Didly didlybom at gmail.com
Fri Nov 7 02:11:06 CST 2014


On Fri, Nov 7, 2014 at 12:09 AM, Sean Farley
<sean.michael.farley at gmail.com> wrote:
> # HG changeset patch
> # User Sean Farley <sean.michael.farley at gmail.com>
> # Date 1413475012 25200
> #      Thu Oct 16 08:56:52 2014 -0700
> # Node ID 45fd23c47bb429c84e28a3303fb2a1097c5082e2
> # Parent  2d54aa5397cdb1c697673ba10b7618d5ac25c69e
> sortdict: add iteritems method
>
> Nothing really fancy, just adding this method to make sure when iteritems is
> used that it is iterated in correct order.
>
> diff --git a/mercurial/util.py b/mercurial/util.py
> --- a/mercurial/util.py
> +++ b/mercurial/util.py
> @@ -367,10 +367,12 @@ class sortdict(dict):
>              pass
>      def keys(self):
>          return self._list
>      def iterkeys(self):
>          return self._list.__iter__()
> +    def iteritems(self):
> +        return self.items().__iter__()
>
>  class lrucachedict(object):
>      '''cache most recent gets from or sets to this dictionary'''
>      def __init__(self, maxsize):
>          self._cache = {}

Won't this code construct the items() list first, and then iterate
over it? If so, maybe it would be best to do it the other way around,
i.e. write a proper sortdict.iteritems method, using a for loop and
yield and then make the sortdict.items method return
list(self.iteritems()).

Of course this would not matter much unless the sortdict is big, but
then you would not need iteritems unless that were the case, right?

Cheers,

Angel


More information about the Mercurial-devel mailing list