Skip to content
Browse files

filtering by method

  • Loading branch information...
1 parent a15b63f commit ce503759f495016923885c8967ccd4b5cd1b1c85 @dir01 committed
Showing with 9 additions and 3 deletions.
  1. +3 −2 filterator/constraints.py
  2. +6 −1 filterator/tests.py
View
5 filterator/constraints.py
@@ -12,6 +12,8 @@ def resolve_value(self, item):
item = getattr(item, attr)
if item is None:
return None
+ if hasattr(item, '__call__'):
+ return item()
return item
def fits(self, item):
@@ -115,6 +117,7 @@ def fits(self, item):
class ConstraintsFactory(object):
KEYWORD_SEPARATOR = '__'
+ DEFAULT_CONSTRAINT_CLASS = ExactConstraint
KEYWORD_TO_CONSTRAINT_CLASS_MAP = {
'exact': ExactConstraint,
@@ -133,8 +136,6 @@ class ConstraintsFactory(object):
'count': CountConstraint,
}
- DEFAULT_CONSTRAINT_CLASS = ExactConstraint
-
def __init__(self, name, value):
self.name = name
self.value = value
View
7 filterator/tests.py
@@ -7,7 +7,9 @@
class FilteratorTestCase(unittest2.TestCase):
def setUp(self):
- Person = namedtuple('Person', 'name age sex children vehicle')
+ class Person(namedtuple('Person', 'name age sex children vehicle')):
+ def is_car_driver(self):
+ return self.vehicle and self.vehicle.type == 'car'
Vehicle = namedtuple('Vehicle', 'type manufacturer')
self.car = Vehicle('car', 'ford')
self.bicycle = Vehicle('bicycle', 'nsbikes')
@@ -43,6 +45,9 @@ def test_filter_by_string(self):
def test_filter_by_int(self):
self.assertItemsEqual([self.alice], self.people.filter(age=23))
+ def test_filter_by_method(self):
+ self.assertItemsEqual([self.bob], self.people.filter(is_car_driver=True))
+
def test_filter_iexact(self):
self.assertItemsEqual([self.bob], self.people.filter(name__iexact='bob'))

0 comments on commit ce50375

Please sign in to comment.
Something went wrong with that request. Please try again.