Repository: planetarypy/pvl

Issue:
items is not subscriptable in python3
As pointed out in #23, in python3 ``items()`` returns an [ItemsView](https://github.com/planetarypy/pvl/blob/master/pvl/_collections.py#L38) object which is not subscriptable
```
In [1]: import pvl

In [2]: label = pvl.loads("""
   ...:     foo = bar
   ...:     monty = python
   ...: """)

In [3]: items = label.items()

In [4]: items[0]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-95f461411437> in <module>()
----> 1 items[0]

TypeError: 'ItemsView' object does not support indexing
```

But we should get something similar to:
```
In [4]: items[0]
Out [4]: ('foo', 'bar')
```

Discussion:

Patch:
diff --git a/pvl/_collections.py b/pvl/_collections.py
index afeab25..2abaf3a 100644
--- a/pvl/_collections.py
+++ b/pvl/_collections.py
@@ -34,6 +34,13 @@ class KeysView(MappingView):
         for key, _ in self._mapping:
             yield key
 
+    def __getitem__(self, index):
+        return self._mapping[index][0]
+
+    def __repr__(self):
+        keys = [key for key, _ in self._mapping]
+        return '%s(%r)' % (type(self).__name__, keys)
+
 
 class ItemsView(MappingView):
     def __contains__(self, item):
@@ -44,6 +51,9 @@ class ItemsView(MappingView):
         for item in self._mapping:
             yield item
 
+    def __getitem__(self, index):
+        return self._mapping[index]
+
 
 class ValuesView(MappingView):
     def __contains__(self, value):
@@ -56,6 +66,13 @@ class ValuesView(MappingView):
         for _, value in self._mapping:
             yield value
 
+    def __getitem__(self, index):
+        return self._mapping[index][1]
+
+    def __repr__(self):
+        values = [value for _, value in self._mapping]
+        return '%s(%r)' % (type(self).__name__, values)
+
 
 class OrderedMultiDict(dict, MutableMapping):
     """A ``dict`` like container.


Test patch:
diff --git a/tests/test_collections.py b/tests/test_collections.py
index a77f9dc..50df73d 100644
--- a/tests/test_collections.py
+++ b/tests/test_collections.py
@@ -368,6 +368,39 @@ def test_py2_items():
     assert module.values() == [1, 2, 3]
 
 
+@pytest.mark.skipif(six.PY2, reason='requires python3')
+def test_py3_items():
+    module = pvl.PVLModule()
+
+    assert isinstance(module.items(), pvl._collections.ItemsView)
+    with pytest.raises(IndexError):
+        module.items()[0]
+
+    assert isinstance(module.keys(), pvl._collections.KeysView)
+    with pytest.raises(IndexError):
+        module.keys()[0]
+
+    assert isinstance(module.values(), pvl._collections.ValuesView)
+    with pytest.raises(IndexError):
+        module.values()[0]
+
+    module = pvl.PVLModule([
+        ('a', 1),
+        ('b', 2),
+        ('a', 3),
+    ])
+
+    assert isinstance(module.items(), pvl._collections.ItemsView)
+    assert module.items()[0] == ('a', 1)
+
+    assert isinstance(module.keys(), pvl._collections.KeysView)
+    assert module.keys()[0] == 'a'
+
+    assert isinstance(module.values(), pvl._collections.ValuesView)
+    assert module.values()[0] == 1
+
+
+
 if six.PY3:
     def iteritems(module):
         return module.items()