Skip to content

Commit

Permalink
change Data [] behavior to return None or [] for missing keys
Browse files Browse the repository at this point in the history
Previously threw exceptions. This differs from python dict or
sequence behavior but is now consistent with get and getRange.
  • Loading branch information
industrial-sloth committed Feb 6, 2015
1 parent d5afd12 commit 8999872
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 16 deletions.
15 changes: 6 additions & 9 deletions python/test/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ def test_brackets(self):
assert_equals(0, vals[0][0])
assert_true(array_equal(self.ary1, vals[0][1]))

assert_raises(KeyError, self.images.__getitem__, 2) # equiv: self.images[2]
assert_is_none(self.images[2])

assert_raises(IndexError, self.images.__getitem__, slice(2, 3)) # equiv: self.images[2:3]
assert_equals([], self.images[2:3])


class TestSeriesGetters(PySparkTestCase):
Expand Down Expand Up @@ -250,11 +250,8 @@ def test_brackets(self):
assert_true(array_equal(self.dataLocal[0][1], vals[0][1]))
assert_true(array_equal(self.dataLocal[1][1], vals[1][1]))

# trying to getitem a key that doesn't exist raises KeyError
# this differs from `get` behavior but is consistent with python dict
# see object.__getitem__ in https://docs.python.org/2/reference/datamodel.html
assert_raises(KeyError, self.series.__getitem__, (25, 17)) # equiv: self.series[(25, 17)]
# trying to getitem a key that doesn't exist returns None
assert_is_none(self.series[(25, 17)])

# passing a range that is completely out of bounds throws IndexError
# note that if a range is only partly out of bounds, it will return what elements the slice does include
assert_raises(IndexError, self.series.__getitem__, [slice(2, 3), slice(None)]) # series[2:3,:]
# passing a range that is completely out of bounds returns []
assert_equals([], self.series[2:3, :])
9 changes: 2 additions & 7 deletions python/thunder/rdds/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,9 @@ def __getitem__(self, item):
isRangeQuery = True

if isRangeQuery:
retVals = self.getRange(item)
if not retVals:
raise IndexError("No keys found for slice(s): '%s'" % str(item))
return self.getRange(item)
else:
retVals = self.get(item)
if retVals is None:
raise KeyError("No key found matching '%s'" % str(item))
return retVals
return self.get(item)

def values(self):
""" Return values, ignoring keys
Expand Down

0 comments on commit 8999872

Please sign in to comment.