Skip to content

Commit

Permalink
iter over non-iterable singular results, dict keys
Browse files Browse the repository at this point in the history
  • Loading branch information
randomir committed Jun 20, 2017
1 parent fea886a commit 0d1f916
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
6 changes: 5 additions & 1 deletion plucky/structural.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,8 @@ def __iter__(self):
if self.empty:
return iter([])

return iter(self.value)
val = self.value
try:
return iter(val)
except Exception as e:
return iter([val])
8 changes: 7 additions & 1 deletion tests/test_pluckable.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,18 @@ def test_attrgetter_namedtuple(self):
Point = namedtuple("Point", "x y z")
self.assertEqual(pluckable(Point(3, 2, 1)).x.value, 3)

def test_iter(self):
def test_iter_list(self):
self.assertEqual(list(iter(self.obj.users.name.last)), ['smith', 'bonobo'])

def test_iter_empty(self):
self.assertEqual(list(iter(pluckable('x').y)), [])

def test_iter_noniterable_singular_value(self):
self.assertEqual(list(iter(pluckable({'x':1}).x)), [1])

def test_iter_dictkeys(self):
self.assertEqual(list(iter(pluckable({'x':{'y':2}}).x)), ['y'])


if __name__ == '__main__':
unittest.main()

0 comments on commit 0d1f916

Please sign in to comment.