summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrett Cannon <brett@python.org>2014-10-03 14:13:36 -0400
committerBrett Cannon <brett@python.org>2014-10-03 14:13:36 -0400
commit4711b7ae8befb52017ed5abdd272f6e5eff61606 (patch)
tree629a959ab488856ad9de1be04e0326aa2762be28
parent61e0bcdf6f57d77c7315a9d801b912359a71eb9a (diff)
downloadsix-4711b7ae8befb52017ed5abdd272f6e5eff61606.tar.gz
Introduce viewkeys(), viewvalues(), and viewitems().
Closes issue #92
-rw-r--r--.hgignore1
-rw-r--r--documentation/index.rst21
-rw-r--r--six.py22
-rw-r--r--test_six.py29
4 files changed, 73 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..594976b 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, **kwargs)
+
+ Returns a view over *dictionary*\'s keys. This replaces
+ ``dictionary.viewkeys()`` on Python 2.7 and ``dictionary.keys()`` on
+ Python 3. *kwargs* are passed through to the underlying method.
+
+
+.. function:: viewvalues(dictionary, **kwargs)
+
+ Returns a view over *dictionary*\'s values. This replaces
+ ``dictionary.viewvalues()`` on Python 2.7 and ``dictionary.values()`` on
+ Python 3. *kwargs* are passed through to the underlying method.
+
+
+.. function:: viewitems(dictionary, **kwargs)
+
+ Returns a view over *dictionary*\'s items. This replaces
+ ``dictionary.viewitems()`` on Python 2.7 and ``dictionary.items()`` on
+ Python 3. *kwargs* are passed through to the underlying method.
+
+
.. 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..5efb62b 100644
--- a/six.py
+++ b/six.py
@@ -554,6 +554,15 @@ if PY3:
def iterlists(d, **kw):
return iter(d.lists(**kw))
+
+ def viewkeys(d, **kw):
+ return d.keys(**kw)
+
+ def viewvalues(d, **kw):
+ return d.values(**kw)
+
+ def viewitems(d, **kw):
+ return d.items(**kw)
else:
def iterkeys(d, **kw):
return iter(d.iterkeys(**kw))
@@ -567,12 +576,25 @@ else:
def iterlists(d, **kw):
return iter(d.iterlists(**kw))
+ def viewkeys(d, **kw):
+ return d.viewkeys(**kw)
+
+ def viewvalues(d, **kw):
+ return d.viewvalues(**kw)
+
+ def viewitems(d, **kw):
+ return d.viewitems(**kw)
+
_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,
"Return an iterator over the (key, value) pairs of a dictionary.")
_add_doc(iterlists,
"Return an iterator over the (key, [values]) pairs of a dictionary.")
+_add_doc(viewkeys, "Return a view over the keys of a dictionary.")
+_add_doc(viewvalues, "Retun a view over the values of a dictionary.")
+_add_doc(viewitems,
+ "Return a view over the (key, value) pairs of a dictionary.")
if PY3:
diff --git a/test_six.py b/test_six.py
index 0125d6b..4ca3200 100644
--- a/test_six.py
+++ b/test_six.py
@@ -389,6 +389,35 @@ def test_dictionary_iterators(monkeypatch):
monkeypatch.undo()
+def test_dictionary_views(monkeypatch):
+ if sys.version_info[:2] <= (2, 6):
+ py.test.skip("view methods on dictionaries only available on 2.7+")
+ 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
+
+ class MyDict(dict):
+ pass
+
+ d = MyDict(zip(range(10), reversed(range(10))))
+ for name in "keys", "values", "items":
+ meth = getattr(six, "view" + name)
+ view = meth(d)
+ assert set(view) == set(getattr(d, name)())
+ record = []
+ def with_kw(*args, **kw):
+ record.append(kw["kw"])
+ return old(*args)
+ old = getattr(MyDict, stock_method_name(name))
+ monkeypatch.setattr(MyDict, stock_method_name(name), with_kw)
+ meth(d, kw=42)
+ assert record == [42]
+ monkeypatch.undo()
+
def test_advance_iterator():
assert six.next is six.advance_iterator
l = [1, 2]