summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2014-10-10 11:25:23 -0400
committerBenjamin Peterson <benjamin@python.org>2014-10-10 11:25:23 -0400
commitd73857d4005ec2a0a7087434eacb5c857db6ea56 (patch)
tree63b4b68ae1db6739681ae3c177f2828df71194bd
parent61e0bcdf6f57d77c7315a9d801b912359a71eb9a (diff)
parentce75a6122644c41e7a8ca5358ca61c12866d142d (diff)
downloadsix-d73857d4005ec2a0a7087434eacb5c857db6ea56.tar.gz
Merged in brettcannon/six/view (pull request #51)
Introduce viewkeys(), viewvalues(), and viewitems()
-rw-r--r--.hgignore1
-rw-r--r--documentation/index.rst21
-rw-r--r--six.py12
-rw-r--r--test_six.py18
4 files changed, 52 insertions, 0 deletions
diff --git a/.hgignore b/.hgignore
index 2a22acf..8815294 100644
--- a/.hgignore
+++ b/.hgignore
@@ -4,3 +4,4 @@ dist
MANIFEST
documentation/_build
.tox
+six.egg-info
diff --git a/documentation/index.rst b/documentation/index.rst
index 0adadc2..93b1daa 100644
--- a/documentation/index.rst
+++ b/documentation/index.rst
@@ -203,6 +203,27 @@ functions and methods is the stdlib :mod:`py3:inspect` module.
*kwargs* are passed through to the underlying method.
+.. function:: viewkeys(dictionary)
+
+ Return a view over *dictionary*\'s keys. This replaces
+ ``dictionary.viewkeys()`` on Python 2.7 and ``dictionary.keys()`` on
+ Python 3.
+
+
+.. function:: viewvalues(dictionary)
+
+ Return a view over *dictionary*\'s values. This replaces
+ ``dictionary.viewvalues()`` on Python 2.7 and ``dictionary.values()`` on
+ Python 3.
+
+
+.. function:: viewitems(dictionary)
+
+ Return a view over *dictionary*\'s items. This replaces
+ ``dictionary.viewitems()`` on Python 2.7 and ``dictionary.items()`` on
+ Python 3.
+
+
.. function:: create_bound_method(func, obj)
Return a method object wrapping *func* and bound to *obj*. On both Python 2
diff --git a/six.py b/six.py
index 21b0e80..4252fdf 100644
--- a/six.py
+++ b/six.py
@@ -554,6 +554,12 @@ if PY3:
def iterlists(d, **kw):
return iter(d.lists(**kw))
+
+ viewkeys = operator.methodcaller("keys")
+
+ viewvalues = operator.methodcaller("values")
+
+ viewitems = operator.methodcaller("items")
else:
def iterkeys(d, **kw):
return iter(d.iterkeys(**kw))
@@ -567,6 +573,12 @@ else:
def iterlists(d, **kw):
return iter(d.iterlists(**kw))
+ viewkeys = operator.methodcaller("viewkeys")
+
+ viewvalues = operator.methodcaller("viewvalues")
+
+ viewitems = operator.methodcaller("viewitems")
+
_add_doc(iterkeys, "Return an iterator over the keys of a dictionary.")
_add_doc(itervalues, "Return an iterator over the values of a dictionary.")
_add_doc(iteritems,
diff --git a/test_six.py b/test_six.py
index 0125d6b..4163520 100644
--- a/test_six.py
+++ b/test_six.py
@@ -389,6 +389,24 @@ def test_dictionary_iterators(monkeypatch):
monkeypatch.undo()
+@py.test.mark.skipif(sys.version_info[:2] < (2, 7),
+ reason="view methods on dictionaries only available on 2.7+")
+def test_dictionary_views():
+ def stock_method_name(viewwhat):
+ """Given a method suffix like "keys" or "values", return the name
+ of the dict method that delivers those on the version of Python
+ we're running in."""
+ if six.PY3:
+ return viewwhat
+ return 'view' + viewwhat
+
+ d = dict(zip(range(10), (range(11, 20))))
+ for name in "keys", "values", "items":
+ meth = getattr(six, "view" + name)
+ view = meth(d)
+ assert set(view) == set(getattr(d, name)())
+
+
def test_advance_iterator():
assert six.next is six.advance_iterator
l = [1, 2]