Commit d1a7f701 authored by Edward J. Stembler's avatar Edward J. Stembler

Reformatted code, corrected spacing, etc.

parent 43c3b0a5
Copyright (c) 2009 Edward J. Stembler
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
\ No newline at end of file
......@@ -42,13 +42,25 @@ print b.sum(lambda x: x > 2) # 7
print b.average() # 2
```
License Terms
MIT License
-------------
Copyright (C) 2009 by Edward J. Stembler
Copyright (c) 2009 Edward J. Stembler
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
\ No newline at end of file
......@@ -5,86 +5,72 @@ __date__ = "$Apr 6, 2009 11:48 PM$"
__module_name__ = "Queryable List"
__version__ = "1.0"
from itertools import ifilter
from itertools import imap
from itertools import islice
from itertools import izip
import UserList
def returns_new(func):
def inner(self, args):
return QueryableList(list(func(self, args)))
return inner
class QueryableList(UserList.UserList):
def __init__(self, data):
super(QueryableList, self).__init__()
self.data = data
def returns_new(func):
def inner(self, args):
return QueryableList(list(func(self, args)))
return inner
@returns_new
def take(self, count):
return islice(self.data, count)
@returns_new
def skip(self, count):
return islice(self.data, count, None)
@returns_new
def join(self, stream):
return ((left, right) for left in self.data for right in stream)
@returns_new
def select(self, func):
return imap(func, self.data)
@returns_new
def where(self, func):
return ifilter(func, self.data)
def single_or_default(self, func=None):
try:
return self.data[0] if func is None else self.where(func).data[0]
except IndexError:
return None
def single(self, func=None):
result = self.single_or_default(func)
if result is None or result == []:
raise ValueError
return result
def order_by(self, func):
self.data.sort(func)
return QueryableList(self.data)
def order_by_descending(self):
return self.order_by(lambda x, y: y-x)
return self.order_by(lambda x, y: y - x)
def contains(self, item):
return self.where(lambda x: x == item) != []
def count(self, func=None):
return len(self.data) if func is None else self.where(func).count()
def sum(self, func=None):
return sum(self.data) if func is None else self.where(func).sum()
def average(self, func=None):
return self.sum() / len(self.data) if func is None else self.where(func).average()
\ No newline at end of file
return self.sum() / len(self.data) if func is None else self.where(func).average()
......@@ -5,76 +5,64 @@ __date__ = "$Apr 28, 2009 2:34 PM$"
__module_name__ = "Tests for QueryableList"
__version__ = "1.0"
from QueryableList import QueryableList
from queryable_list import QueryableList
import unittest
class QueryableListTestCase(unittest.TestCase):
class QueryableListTest(unittest.TestCase):
def setUp(self):
self.queryable_list = QueryableList(range(20))
def tearDown(self):
self.queryable_list = None
def test_take_where_order_by_descending(self):
expected_result = [4, 3, 2, 1, 0]
result = self.queryable_list.take(10).where(lambda x: x < 5).order_by_descending()
self.assertEqual(result, expected_result)
def test_count(self):
expected_result = 20
result = self.queryable_list.count()
self.assertEqual(result, expected_result)
def test_count_func(self):
expected_result = 2
result = self.queryable_list.count(lambda x: x < 2)
self.assertEqual(result, expected_result)
def test_contains(self):
self.assertEquals(self.queryable_list.contains(4), True)
self.assertEquals(self.queryable_list.contains(21), False)
def test_single(self):
self.assertEqual(self.queryable_list.single(), 0)
self.assertEqual(self.queryable_list.order_by_descending().single(), 19)
def test_single_func(self):
self.assertEqual(self.queryable_list.single(lambda x: x < 3), 0)
self.assertEqual(self.queryable_list.single(lambda x: x > 10), 11)
l = lambda x: x > 100
self.assertRaises(ValueError, self.queryable_list.single, l)
greater_than_100 = lambda x: x > 100
self.assertRaises(ValueError, self.queryable_list.single, greater_than_100)
def test_single_or_default(self):
self.assertEqual(self.queryable_list.single_or_default(lambda x: x == 7), 7)
self.assertEqual(self.queryable_list.single_or_default(lambda x: x == 30), None)
def test_sum(self):
self.assertEqual(self.queryable_list.sum(), 190)
def test_sum_func(self):
self.assertEqual(self.queryable_list.sum(lambda x: x > 10), 135)
def test_average(self):
self.assertEqual(self.queryable_list.average(), 9)
if __name__ == '__main__':
unittest.main()
\ No newline at end of file
unittest.main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment