Skip to content

Commit

Permalink
remove multi-handle support because it add too much ambiguity that, s…
Browse files Browse the repository at this point in the history
…o far, is not warrented. advanced plucking should be done with custom functions or a simple for loop instead
  • Loading branch information
dpep committed Apr 28, 2018
1 parent d7730cc commit 80232a3
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 110 deletions.
17 changes: 7 additions & 10 deletions pluckit/pluck.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
from .pluckit import pluckit


def pluck(collection, *handles):
__all__ = [ 'pluck' ]


def pluck(collection, handle):
if collection is None:
# nothing to pluck
return None
Expand All @@ -17,7 +20,7 @@ def pluck(collection, *handles):
clone.clear()

clone.update(
{ k : pluckit(v, *handles) for k,v in collection.items() }
{ k : pluckit(v, handle) for k,v in collection.items() }
)
return clone

Expand All @@ -29,13 +32,7 @@ def pluck(collection, *handles):
clone = copy(collection)
clone.clear()

if len(handles) <= 1:
res = { pluckit(x, *handles) for x in collection }
else:
# lists are unhashable, so cast to a tuple
res = { tuple(pluckit(x, *handles)) for x in collection }

clone.update(res)
clone.update({ pluckit(x, handle) for x in collection })
return clone

if isinstance(collection, list) or hasattr(collection, '__iter__'):
Expand All @@ -51,7 +48,7 @@ def pluck(collection, *handles):
# fall back to regular list
clone = []

clone += [ pluckit(x, *handles) for x in collection ]
clone += [ pluckit(x, handle) for x in collection ]
return clone

raise TypeError('unpluckable type: %s' % type(collection))
12 changes: 10 additions & 2 deletions pluckit/pluckable.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
from .pluck import pluck


__all__ = [
'Pluckable',
'PluckableList',
'PluckableDict',
'PluckableSet',
]


class Pluckable():
def pluck(self, *handles):
return pluck(self, *handles)
def pluck(self, handle):
return pluck(self, handle)


class PluckableList(list, Pluckable): pass
Expand Down
15 changes: 2 additions & 13 deletions pluckit/pluckit.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,11 @@
__all__ = [ 'pluckit' ]


def pluckit(obj, *handles):
if obj == None:
def pluckit(obj, handle):
if obj is None or handle is None:
# None has nothing to pluck...
return None

if len(handles) == 0:
# they don't want to pluck anything?
return obj

if len(handles) == 1:
return __pluck_single(obj, handles[0])

return [ __pluck_single(obj, handle) for handle in handles ]


def __pluck_single(obj, handle):
# function pointer
if callable(handle):
return handle(obj)
Expand Down
29 changes: 7 additions & 22 deletions tests/CornerCasesTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,22 @@

class CornerCasesTest(unittest.TestCase):
def test_null_handle(self):
data = {
'a' : [1, 2],
'b' : [3, 4],
}

with self.assertRaises(TypeError):
self.assertEquals(data, pluck(data, None))
data = [1, 2, 3]
self.assertEquals(data, pluck(data, None))


def test_empty(self):
self.assertEquals([], pluck([], 'k'))
self.assertEquals([], pluck([], 'k', 'v'))

self.assertEquals({}, pluck({}, 'k'))
self.assertEquals({}, pluck({}, 'k', 'v'))

self.assertEquals(set(), pluck(set(), 'k'))


def test_noop(self):
data = [1, 2, 3]
self.assertEquals(data, pluck(data))

data = {'a' : 1, 'b' : 2}
self.assertEquals(data, pluck(data))


def test_none(self):
self.assertEquals(None, pluck(None))
self.assertEquals(None, pluck(None, 123))
def test_null(self):
self.assertEquals(None, pluck(None, None))
self.assertEquals(None, pluck(None, 123))


def test_null_values(self):
data = {
None : [1, 2],
'b' : [3, 4],
Expand Down
9 changes: 0 additions & 9 deletions tests/DictTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,6 @@ def test_basic(self):
pluck(data, 'v')
)

self.assertEquals(
{
'a': [ 1, 2 ],
'b': [ 3, 4 ],
'c': [ 5, 6 ],
},
pluck(data, 'k', 'v')
)


def test_indicies(self):
data = {
Expand Down
5 changes: 0 additions & 5 deletions tests/ListTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ def test_indicies(self):
pluck(data, 0)
)

self.assertEquals(
[ [ 0, 1 ], [ 5, 6 ] ],
pluck(data, 0, 1)
)


def test_fn(self):
# build-in
Expand Down
14 changes: 0 additions & 14 deletions tests/NativeTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ def test_list(self):
data.pluck('x')
)

self.assertEquals(
[ [1, 2], [2, 4], [3, 6] ],
data.pluck('x', 'y')
)


def test_dict(self):
data = {
Expand All @@ -45,15 +40,6 @@ def test_dict(self):
data.pluck('v')
)

self.assertEquals(
{
'a': [ 1, 2 ],
'b': [ 3, 4 ],
'c': [ 5, 6 ],
},
data.pluck('k', 'v')
)


def test_set(self):
Coordinate = namedtuple('Coordinate', ['x', 'y']) # hashable
Expand Down
16 changes: 0 additions & 16 deletions tests/PluckTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ def test_list(self):
pluckit(data, lambda x: sum(x))
)

self.assertEquals(
[ 2, 1 ],
pluckit(data, 2, 1),
)


def test_dict(self):
abc = { 'a' : 1, 'b' : 2, 'c' : 3 }
Expand All @@ -61,11 +56,6 @@ def test_dict(self):
pluckit(abc, len)
)

self.assertEquals(
[ 1, 2 ],
pluckit(abc, 'a', 'b')
)


def test_set(self):
data = set([ 1, 2, 3 ])
Expand Down Expand Up @@ -144,14 +134,8 @@ def prop(self):
pluckit(Foo, '__name__')
)

self.assertEquals(
[ 'Foo', 123 ],
pluckit(Foo, '__name__', 'CONST')
)


def test_none(self):
self.assertEquals(None, pluckit(None))
self.assertEquals(None, pluckit(None, 'abc'))
self.assertEquals(None, pluckit(None, 123))
self.assertEquals(None, pluckit(None, str))
Expand Down
14 changes: 0 additions & 14 deletions tests/PluckablesTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ def test_list(self):
data.pluck('x')
)

self.assertEquals(
[ [1, 2], [2, 4], [3, 6] ],
data.pluck('x', 'y')
)

# type is preserved
self.assertEquals(PluckableList, type(data.pluck('x')))

Expand All @@ -48,15 +43,6 @@ def test_dict(self):
data.pluck('v')
)

self.assertEquals(
{
'a': [ 1, 2 ],
'b': [ 3, 4 ],
'c': [ 5, 6 ],
},
data.pluck('k', 'v')
)

# type is preserved
self.assertEquals(
PluckableDict,
Expand Down
5 changes: 0 additions & 5 deletions tests/SetTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ def test_basic(self):
pluck(data, 'x')
)

self.assertEquals(
{ (1, 2), (2, 4), (3, 6) },
pluck(data, 'x', 'y')
)



if __name__ == '__main__':
Expand Down

0 comments on commit 80232a3

Please sign in to comment.