Commit c820892e authored by Tim Graham's avatar Tim Graham
Browse files

Removed django.utils.datastructures.SortedDict per deprecation timeline.

parent 41f0d3d3
Loading
Loading
Loading
Loading
+0 −125
Original line number Diff line number Diff line
import copy
import warnings
from collections import OrderedDict

from django.utils import six
from django.utils.deprecation import RemovedInDjango19Warning


class SortedDict(dict):
    """
    A dictionary that keeps its keys in the order in which they're inserted.
    """
    def __new__(cls, *args, **kwargs):
        instance = super(SortedDict, cls).__new__(cls, *args, **kwargs)
        instance.keyOrder = []
        return instance

    def __init__(self, data=None):
        warnings.warn(
            "SortedDict is deprecated and will be removed in Django 1.9.",
            RemovedInDjango19Warning, stacklevel=2
        )
        if data is None or isinstance(data, dict):
            data = data or []
            super(SortedDict, self).__init__(data)
            self.keyOrder = list(data) if data else []
        else:
            super(SortedDict, self).__init__()
            super_set = super(SortedDict, self).__setitem__
            for key, value in data:
                # Take the ordering from first key
                if key not in self:
                    self.keyOrder.append(key)
                # But override with last value in data (dict() does this)
                super_set(key, value)

    def __deepcopy__(self, memo):
        return self.__class__([(key, copy.deepcopy(value, memo))
                               for key, value in self.items()])

    def __copy__(self):
        # The Python's default copy implementation will alter the state
        # of self. The reason for this seems complex but is likely related to
        # subclassing dict.
        return self.copy()

    def __setitem__(self, key, value):
        if key not in self:
            self.keyOrder.append(key)
        super(SortedDict, self).__setitem__(key, value)

    def __delitem__(self, key):
        super(SortedDict, self).__delitem__(key)
        self.keyOrder.remove(key)

    def __iter__(self):
        return iter(self.keyOrder)

    def __reversed__(self):
        return reversed(self.keyOrder)

    def pop(self, k, *args):
        result = super(SortedDict, self).pop(k, *args)
        try:
            self.keyOrder.remove(k)
        except ValueError:
            # Key wasn't in the dictionary in the first place. No problem.
            pass
        return result

    def popitem(self):
        result = super(SortedDict, self).popitem()
        self.keyOrder.remove(result[0])
        return result

    def _iteritems(self):
        for key in self.keyOrder:
            yield key, self[key]

    def _iterkeys(self):
        for key in self.keyOrder:
            yield key

    def _itervalues(self):
        for key in self.keyOrder:
            yield self[key]

    if six.PY3:
        items = _iteritems
        keys = _iterkeys
        values = _itervalues
    else:
        iteritems = _iteritems
        iterkeys = _iterkeys
        itervalues = _itervalues

        def items(self):
            return [(k, self[k]) for k in self.keyOrder]

        def keys(self):
            return self.keyOrder[:]

        def values(self):
            return [self[k] for k in self.keyOrder]

    def update(self, dict_):
        for k, v in six.iteritems(dict_):
            self[k] = v

    def setdefault(self, key, default):
        if key not in self:
            self.keyOrder.append(key)
        return super(SortedDict, self).setdefault(key, default)

    def copy(self):
        """Returns a copy of this object."""
        # This way of initializing the copy means it works for subclasses, too.
        return self.__class__(self)

    def __repr__(self):
        """
        Replaces the normal dict.__repr__ with a version that returns the keys
        in their sorted order.
        """
        return '{%s}' % ', '.join('%r: %r' % (k, v) for k, v in six.iteritems(self))

    def clear(self):
        super(SortedDict, self).clear()
        self.keyOrder = []


class OrderedSet(object):
+0 −28
Original line number Diff line number Diff line
@@ -97,34 +97,6 @@ need to distinguish caches by the ``Accept-language`` header.
    cache, this just means that we have to build the response once to get at
    the Vary header and so at the list of headers to use for the cache key.

``django.utils.datastructures``
===============================

.. module:: django.utils.datastructures
   :synopsis: Data structures that aren't in Python's standard library.

.. class:: SortedDict

.. deprecated:: 1.7
    ``SortedDict`` is deprecated and will be removed in Django 1.9. Use
    :class:`collections.OrderedDict` instead.

    The :class:`django.utils.datastructures.SortedDict` class is a dictionary
    that keeps its keys in the order in which they're inserted.

Creating a new SortedDict
-------------------------

Creating a new ``SortedDict`` must be done in a way where ordering is
guaranteed. For example::

    SortedDict({'b': 1, 'a': 2, 'c': 3})

will not work. Passing in a basic Python ``dict`` could produce unreliable
results. Instead do::

    SortedDict([('b', 1), ('a', 2), ('c', 3)])

``django.utils.dateparse``
==========================

+2 −2
Original line number Diff line number Diff line
@@ -714,12 +714,12 @@ Data structures
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

``django.newforms.forms.SortedDictFromList`` was removed.
:class:`django.utils.datastructures.SortedDict` can now be instantiated with
``django.utils.datastructures.SortedDict`` can now be instantiated with
a sequence of tuples.

To update your code:

1. Use :class:`django.utils.datastructures.SortedDict` wherever you were
1. Use ``django.utils.datastructures.SortedDict`` wherever you were
   using ``django.newforms.forms.SortedDictFromList``.

2. Because ``django.utils.datastructures.SortedDict.copy`` doesn't
+1 −2
Original line number Diff line number Diff line
@@ -1511,8 +1511,7 @@ Python versions, this module isn't useful anymore. It has been deprecated. Use
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

As :class:`~collections.OrderedDict` was added to the standard library in
Python 2.7, :class:`~django.utils.datastructures.SortedDict` is no longer
needed and has been deprecated.
Python 2.7, ``SortedDict`` is no longer needed and has been deprecated.

Custom SQL location for models package
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+1 −1
Original line number Diff line number Diff line
@@ -181,7 +181,7 @@ versions of Python.

:meth:`dict.keys`, :meth:`dict.items` and :meth:`dict.values` return lists in
Python 2 and iterators in Python 3. :class:`~django.http.QueryDict` and the
:class:`dict`-like classes defined in :mod:`django.utils.datastructures`
:class:`dict`-like classes defined in ``django.utils.datastructures``
behave likewise in Python 3.

six_ provides compatibility functions to work around this change:
Loading