_proxied.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 Joshua Bronson. All Rights Reserved.
  3. #
  4. # This Source Code Form is subject to the terms of the Mozilla Public
  5. # License, v. 2.0. If a copy of the MPL was not distributed with this
  6. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  7. """Provides :class:`_ProxiedKeysVals` and :class:`_ProxiedKeysValsItems."""
  8. from .compat import PY2
  9. class _ProxiedKeysVals(object):
  10. r"""Mixin providing more efficient implementations
  11. of :meth:`keys` and :meth:`values`
  12. (as well as their iter\* and view\* counterparts on Python 2)
  13. by proxying calls through to the backing dicts so they run at native speed,
  14. compared to the implementations from :class:`collections.abc.Mapping`.
  15. Similar functionality for :meth:`items` is moved
  16. into the :class:`_ProxiedKeysValsItems` subclass below
  17. so that :class:`FrozenOrderedBidict` can benefit from using just this mixin;
  18. the :meth:`_ProxiedKeysValsItems.items` implementation below
  19. is not useful for :class:`FrozenOrderedBidict`.
  20. """
  21. __slots__ = ()
  22. # pylint: disable=no-member
  23. def keys(self):
  24. """A set-like object providing a view on the contained keys."""
  25. return self._fwdm.keys()
  26. def values(self):
  27. """A set-like object providing a view on the contained values.
  28. Note that because the values of a :class:`~bidict.BidirectionalMapping`
  29. are the keys of its inverse,
  30. this returns a :class:`~collections.abc.KeysView`
  31. rather than a :class:`~collections.abc.ValuesView`,
  32. which has the advantages of constant-time containment checks
  33. and supporting set operations.
  34. """
  35. return self._invm.keys()
  36. if PY2:
  37. def viewkeys(self): # pylint: disable=missing-docstring
  38. return self._fwdm.viewkeys()
  39. viewkeys.__doc__ = keys.__doc__
  40. keys.__doc__ = 'A list of the contained keys.'
  41. def viewvalues(self): # pylint: disable=missing-docstring
  42. return self._invm.viewkeys()
  43. viewvalues.__doc__ = values.__doc__
  44. values.__doc__ = 'A list of the contained values.'
  45. def iterkeys(self):
  46. """An iterator over the contained keys."""
  47. return self._fwdm.iterkeys()
  48. def itervalues(self):
  49. """An iterator over the contained values."""
  50. return self._invm.iterkeys()
  51. class _ProxiedKeysValsItems(_ProxiedKeysVals):
  52. r"""Extend :class:`_ProxiedKeysVals` with
  53. a similarly-more-efficient implementation of :meth:`items`
  54. (as well as its iter\* and view\* counterparts on Python 2).
  55. """
  56. __slots__ = ()
  57. # pylint: disable=no-member
  58. def items(self):
  59. """A set-like object providing a view on the contained items."""
  60. return self._fwdm.items()
  61. if PY2:
  62. def viewitems(self): # pylint: disable=missing-docstring
  63. return self._fwdm.viewitems()
  64. viewitems.__doc__ = items.__doc__
  65. items.__doc__ = 'A list of the contained items.'
  66. def iteritems(self):
  67. """An iterator over the contained items."""
  68. return self._fwdm.iteritems()