Skip to content
Browse files

Add QueryList.exclude(), QueryList.limit(), and QueryList.count

  • Loading branch information...
1 parent 9ede657 commit 9fe6e07a5650244813dbbec0c147b7ff9d78aaad @thomasw committed Nov 24, 2012
Showing with 53 additions and 2 deletions.
  1. +26 −2 querylist/querylist.py
  2. +27 −0 querylist/tests/querylist_tests.py
View
28 querylist/querylist.py
@@ -3,16 +3,22 @@
class QueryList(list):
- def __init__(self, data=None, wrapper=BetterDict):
+ def __init__(self, data=None, wrapper=BetterDict, wrap=True):
"""Create a QueryList from an iterable and a wrapper object."""
self._wrapper = wrapper
self.src_data = data
+ converted_data = data or []
# Wrap our src_data with wrapper
- converted_data = self._convert_iterable(data) if data else []
+ if wrap:
+ converted_data = self._convert_iterable(data) if data else []
super(QueryList, self).__init__(converted_data)
+ @property
+ def count(self):
+ return len(self)
+
def _convert_iterable(self, iterable):
"""Converts elements returned by an iterable into instances of
self._wrapper
@@ -48,6 +54,24 @@ def get(self, **kwargs):
raise NotFound("Element with specified attributes not found.")
+ def exclude(self, **kwargs):
+ """Returns a new QueryList containing the subset of objects from
+ this QueryList that do not match the provided lookup parameters.
+
+ """
+ return QueryList(
+ data=(x for x in self if not self._check_element(kwargs, x)),
+ wrapper=self._wrapper, wrap=False)
+
+ def limit(self, **kwargs):
+ """Returns a new QueryList containing the subset of objects from this
+ QueryList that match the provided lookup parameters
+
+ """
+ return QueryList(
+ data=(x for x in self if self._check_element(kwargs, x)),
+ wrapper=self._wrapper, wrap=False)
+
class NotFound(Exception):
pass
View
27 querylist/tests/querylist_tests.py
@@ -19,6 +19,9 @@ def test_sets_src_data_attirbute_to_iterable_passed_to_constructor(self):
def test_converts_src_data_members_to_wrapper_type(self):
self.assertEqual(QueryList([1, 2, 3], str), ['1', '2', '3'])
+ def test_with_wrap_disabled_doesnt_touch_member_objects(self):
+ self.assertEqual(QueryList([1, 2, 3], str, False), [1, 2, 3])
+
class QueryListConverIterableTests(unittest2.TestCase):
"""QueryList._convert_iterable()"""
@@ -57,6 +60,12 @@ def setUp(self):
self.ql = QueryList(SITE_LIST)
+class QueryListCountPropertyTests(QueryListMethodTests):
+ "QueryList.count"
+ def test_returns_how_many_objects_are_in_the_querylist(self):
+ self.assertEqual(self.ql.count, len(self.src_list))
+
+
class QueryListGetMethodTests(QueryListMethodTests):
"""QueryList.get()"""
def test_returns_first_encountered_match(self):
@@ -74,3 +83,21 @@ def test_works_correctly_with_multiple_lookups(self):
def test_works_correctly_with_relational_lookups(self):
self.assertEqual(
self.ql.get(meta__keywords=['Catsup', 'dogs']), self.src_list[1])
+
+
+class QueryListExcludeMethodTests(QueryListMethodTests):
+ """QueryList.exclude()"""
+ def test_excludes_the_matching_set_of_elements(self):
+ self.assertEqual(self.ql.exclude(published=True), [self.src_list[2]])
+
+ def test_returns_an_empty_querylist_if_no_items_match(self):
+ self.assertFalse(self.ql.exclude(meta__description='My cool site'))
+
+
+class QueryListLimitMethodTests(QueryListMethodTests):
+ """QueryList.limit()"""
+ def test_returns_subset_of_matching_elements(self):
+ self.assertEqual(self.ql.limit(published=False), [self.src_list[2]])
+
+ def test_returns_an_empty_querylist_if_no_items_match(self):
+ self.assertFalse(self.ql.limit(id=1000))

0 comments on commit 9fe6e07

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