Skip to content

Commit 8999872

Browse files
change Data [] behavior to return None or [] for missing keys
Previously threw exceptions. This differs from python dict or sequence behavior but is now consistent with get and getRange.
1 parent d5afd12 commit 8999872

File tree

2 files changed

+8
-16
lines changed

2 files changed

+8
-16
lines changed

python/test/test_data.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ def test_brackets(self):
9999
assert_equals(0, vals[0][0])
100100
assert_true(array_equal(self.ary1, vals[0][1]))
101101

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

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

106106

107107
class TestSeriesGetters(PySparkTestCase):
@@ -250,11 +250,8 @@ def test_brackets(self):
250250
assert_true(array_equal(self.dataLocal[0][1], vals[0][1]))
251251
assert_true(array_equal(self.dataLocal[1][1], vals[1][1]))
252252

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

258-
# passing a range that is completely out of bounds throws IndexError
259-
# note that if a range is only partly out of bounds, it will return what elements the slice does include
260-
assert_raises(IndexError, self.series.__getitem__, [slice(2, 3), slice(None)]) # series[2:3,:]
256+
# passing a range that is completely out of bounds returns []
257+
assert_equals([], self.series[2:3, :])

python/thunder/rdds/data.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -249,14 +249,9 @@ def __getitem__(self, item):
249249
isRangeQuery = True
250250

251251
if isRangeQuery:
252-
retVals = self.getRange(item)
253-
if not retVals:
254-
raise IndexError("No keys found for slice(s): '%s'" % str(item))
252+
return self.getRange(item)
255253
else:
256-
retVals = self.get(item)
257-
if retVals is None:
258-
raise KeyError("No key found matching '%s'" % str(item))
259-
return retVals
254+
return self.get(item)
260255

261256
def values(self):
262257
""" Return values, ignoring keys

0 commit comments

Comments
 (0)