Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Added .invoke() support
Browse files Browse the repository at this point in the history
  • Loading branch information
dir01 committed Jun 28, 2012
1 parent beb7ed4 commit 7b8be06
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
15 changes: 14 additions & 1 deletion filterator/commands.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from itertools import imap
from operator import attrgetter
from operator import attrgetter, methodcaller

from errors import MultipleValuesReturned
from constraints import ConstraintsFactory, CallableConstraint
Expand All @@ -14,6 +14,7 @@
'CountCommand',
'SumCommand',
'ExistsCommand',
'InvokeCommand',
)


Expand Down Expand Up @@ -178,6 +179,18 @@ def execute(self):
return self.context.filter(*self.args, **self.kwargs).get()


class InvokeCommand(BaseCommand):
def __init__(self, context, iterable, method_name, *args, **kwargs):
self.method_name = method_name
super(InvokeCommand, self).__init__(context, iterable, *args, **kwargs)

def execute(self):
return map(
methodcaller(self.method_name, *self.args, **self.kwargs),
self.iterable
)


class CountCommand(BaseCommand):
def execute(self):
return len(self.iterable)
Expand Down
3 changes: 3 additions & 0 deletions filterator/filterator.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ def order_by(self, *keys):
def get(self, *callables, **constrains):
return self.__execute_command(GetCommand, *callables, **constrains)

def invoke(self, method_name, *args, **kwargs):
return self.__execute_command(InvokeCommand, method_name, *args, **kwargs)

def count(self):
return self.__execute_command(CountCommand)

Expand Down
30 changes: 29 additions & 1 deletion filterator/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@

class Person(namedtuple('Person', 'name age sex children vehicle')):
def is_car_driver(self):
return self.vehicle and self.vehicle.type == 'car'
return self.vehicle is not None and self.vehicle.type == 'car'

def is_age_dividable_by(self, num):
return self.age % num == 0

def get_vehicle_manufacturer(self, default=None):
if self.is_car_driver():
return self.vehicle.manufacturer
return default


Vehicle = namedtuple('Vehicle', 'type manufacturer')
Expand Down Expand Up @@ -231,5 +239,25 @@ def test_order_by_method(self):
self.assertEqual([self.dog, self.human, self.spider], self.creatures.order_by('get_name'))


class TestInvoke(FilteratorTestCase):
def test_no_arguments(self):
self.assertEqual(
[False, False, False, True],
self.people.invoke('is_car_driver')
)

def test_args(self):
self.assertEqual(
[True, False, False, False],
self.people.invoke('is_age_dividable_by', 2)
)

def test_kwargs(self):
self.assertEqual(
['foo', 'foo', 'foo', 'ford'],
self.people.invoke('get_vehicle_manufacturer', default='foo')
)


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

0 comments on commit 7b8be06

Please sign in to comment.