2021.10 #41
@ -1,9 +1,16 @@
|
|||||||
from collections import Callable
|
from collections import Callable
|
||||||
|
|
||||||
from cpl_query._query.where_query import where_query
|
from cpl_query._query.where_query import where_query
|
||||||
|
from cpl_query.exceptions import ExceptionArgument, ArgumentNoneException
|
||||||
from cpl_query.extension.iterable_abc import IterableABC
|
from cpl_query.extension.iterable_abc import IterableABC
|
||||||
|
|
||||||
|
|
||||||
def all_query(_list: IterableABC, _func: Callable) -> bool:
|
def all_query(_list: IterableABC, _func: Callable) -> bool:
|
||||||
|
if _list is None:
|
||||||
|
raise ArgumentNoneException(ExceptionArgument.list)
|
||||||
|
|
||||||
|
if _func is None:
|
||||||
|
raise ArgumentNoneException(ExceptionArgument.func)
|
||||||
|
|
||||||
result = where_query(_list, _func)
|
result = where_query(_list, _func)
|
||||||
return len(result) == len(_list)
|
return len(result) == len(_list)
|
||||||
|
@ -1,9 +1,16 @@
|
|||||||
from collections import Callable
|
from collections import Callable
|
||||||
|
|
||||||
from cpl_query._query.where_query import where_query
|
from cpl_query._query.where_query import where_query
|
||||||
|
from cpl_query.exceptions import ArgumentNoneException, ExceptionArgument
|
||||||
from cpl_query.extension.iterable_abc import IterableABC
|
from cpl_query.extension.iterable_abc import IterableABC
|
||||||
|
|
||||||
|
|
||||||
def any_query(_list: IterableABC, _func: Callable) -> bool:
|
def any_query(_list: IterableABC, _func: Callable) -> bool:
|
||||||
|
if _list is None:
|
||||||
|
raise ArgumentNoneException(ExceptionArgument.list)
|
||||||
|
|
||||||
|
if _func is None:
|
||||||
|
raise ArgumentNoneException(ExceptionArgument.func)
|
||||||
|
|
||||||
result = where_query(_list, _func)
|
result = where_query(_list, _func)
|
||||||
return len(result) > 0
|
return len(result) > 0
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
from typing import Callable, Union
|
from typing import Callable, Union
|
||||||
|
|
||||||
from cpl_query.exceptions import InvalidTypeException, WrongTypeException
|
from cpl_query.exceptions import InvalidTypeException, WrongTypeException, ExceptionArgument, ArgumentNoneException
|
||||||
from cpl_query.extension.iterable_abc import IterableABC
|
from cpl_query.extension.iterable_abc import IterableABC
|
||||||
|
|
||||||
|
|
||||||
@ -8,6 +8,12 @@ def avg_query(_list: IterableABC, _t: type, _func: Callable) -> Union[int, float
|
|||||||
average = 0
|
average = 0
|
||||||
count = len(_list)
|
count = len(_list)
|
||||||
|
|
||||||
|
if _list is None:
|
||||||
|
raise ArgumentNoneException(ExceptionArgument.list)
|
||||||
|
|
||||||
|
if _func is None:
|
||||||
|
raise ArgumentNoneException(ExceptionArgument.func)
|
||||||
|
|
||||||
if _t != int and _t != float and _t != complex:
|
if _t != int and _t != float and _t != complex:
|
||||||
raise InvalidTypeException()
|
raise InvalidTypeException()
|
||||||
|
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
|
from cpl_query.exceptions import ArgumentNoneException, ExceptionArgument
|
||||||
from cpl_query.extension.iterable_abc import IterableABC
|
from cpl_query.extension.iterable_abc import IterableABC
|
||||||
|
|
||||||
|
|
||||||
def first_query(_list: IterableABC) -> any:
|
def first_query(_list: IterableABC) -> any:
|
||||||
|
if _list is None:
|
||||||
|
raise ArgumentNoneException(ExceptionArgument.list)
|
||||||
|
|
||||||
if len(_list) == 0:
|
if len(_list) == 0:
|
||||||
raise Exception('Index out of range')
|
raise Exception('Index out of range')
|
||||||
|
|
||||||
@ -11,6 +15,9 @@ def first_query(_list: IterableABC) -> any:
|
|||||||
|
|
||||||
|
|
||||||
def first_or_default_query(_list: IterableABC) -> Optional[any]:
|
def first_or_default_query(_list: IterableABC) -> Optional[any]:
|
||||||
|
if _list is None:
|
||||||
|
raise ArgumentNoneException(ExceptionArgument.list)
|
||||||
|
|
||||||
if len(_list) == 0:
|
if len(_list) == 0:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@ -1,8 +1,15 @@
|
|||||||
from collections import Callable
|
from collections import Callable
|
||||||
|
|
||||||
|
from cpl_query.exceptions import ExceptionArgument, ArgumentNoneException
|
||||||
from cpl_query.extension.iterable_abc import IterableABC
|
from cpl_query.extension.iterable_abc import IterableABC
|
||||||
|
|
||||||
|
|
||||||
def for_each_query(_list: IterableABC, func: Callable):
|
def for_each_query(_list: IterableABC, _func: Callable):
|
||||||
|
if _list is None:
|
||||||
|
raise ArgumentNoneException(ExceptionArgument.list)
|
||||||
|
|
||||||
|
if _func is None:
|
||||||
|
raise ArgumentNoneException(ExceptionArgument.func)
|
||||||
|
|
||||||
for element in _list:
|
for element in _list:
|
||||||
func(element)
|
_func(element)
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
from collections import Callable
|
from collections import Callable
|
||||||
|
|
||||||
|
from cpl_query.exceptions import ExceptionArgument, ArgumentNoneException
|
||||||
from cpl_query.extension.iterable_abc import IterableABC
|
from cpl_query.extension.iterable_abc import IterableABC
|
||||||
from cpl_query.extension.ordered_iterable_abc import OrderedIterableABC
|
from cpl_query.extension.ordered_iterable_abc import OrderedIterableABC
|
||||||
|
|
||||||
@ -12,6 +13,12 @@ def order_by_query(_list: IterableABC, _func: Callable) -> OrderedIterableABC:
|
|||||||
|
|
||||||
|
|
||||||
def order_by_descending_query(_list: IterableABC, _func: Callable) -> OrderedIterableABC:
|
def order_by_descending_query(_list: IterableABC, _func: Callable) -> OrderedIterableABC:
|
||||||
|
if _list is None:
|
||||||
|
raise ArgumentNoneException(ExceptionArgument.list)
|
||||||
|
|
||||||
|
if _func is None:
|
||||||
|
raise ArgumentNoneException(ExceptionArgument.func)
|
||||||
|
|
||||||
result = OrderedIterableABC(_func)
|
result = OrderedIterableABC(_func)
|
||||||
_list.sort(key=_func, reverse=True)
|
_list.sort(key=_func, reverse=True)
|
||||||
result.extend(_list)
|
result.extend(_list)
|
||||||
@ -19,10 +26,22 @@ def order_by_descending_query(_list: IterableABC, _func: Callable) -> OrderedIte
|
|||||||
|
|
||||||
|
|
||||||
def then_by_query(_list: OrderedIterableABC, _func: Callable) -> OrderedIterableABC:
|
def then_by_query(_list: OrderedIterableABC, _func: Callable) -> OrderedIterableABC:
|
||||||
|
if _list is None:
|
||||||
|
raise ArgumentNoneException(ExceptionArgument.list)
|
||||||
|
|
||||||
|
if _func is None:
|
||||||
|
raise ArgumentNoneException(ExceptionArgument.func)
|
||||||
|
|
||||||
_list.sort(key=_func)
|
_list.sort(key=_func)
|
||||||
return _list
|
return _list
|
||||||
|
|
||||||
|
|
||||||
def then_by_descending_query(_list: OrderedIterableABC, _func: Callable) -> OrderedIterableABC:
|
def then_by_descending_query(_list: OrderedIterableABC, _func: Callable) -> OrderedIterableABC:
|
||||||
|
if _list is None:
|
||||||
|
raise ArgumentNoneException(ExceptionArgument.list)
|
||||||
|
|
||||||
|
if _func is None:
|
||||||
|
raise ArgumentNoneException(ExceptionArgument.func)
|
||||||
|
|
||||||
_list.sort(key=_func, reverse=True)
|
_list.sort(key=_func, reverse=True)
|
||||||
return _list
|
return _list
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
|
from cpl_query.exceptions import ArgumentNoneException, ExceptionArgument
|
||||||
from cpl_query.extension.iterable_abc import IterableABC
|
from cpl_query.extension.iterable_abc import IterableABC
|
||||||
|
|
||||||
|
|
||||||
def single_query(_list: IterableABC) -> any:
|
def single_query(_list: IterableABC) -> any:
|
||||||
|
if _list is None:
|
||||||
|
raise ArgumentNoneException(ExceptionArgument.list)
|
||||||
|
|
||||||
if len(_list) > 1:
|
if len(_list) > 1:
|
||||||
raise Exception('Found more than one element')
|
raise Exception('Found more than one element')
|
||||||
elif len(_list) == 0:
|
elif len(_list) == 0:
|
||||||
@ -13,6 +17,9 @@ def single_query(_list: IterableABC) -> any:
|
|||||||
|
|
||||||
|
|
||||||
def single_or_default_query(_list: IterableABC) -> Optional[any]:
|
def single_or_default_query(_list: IterableABC) -> Optional[any]:
|
||||||
|
if _list is None:
|
||||||
|
raise ArgumentNoneException(ExceptionArgument.list)
|
||||||
|
|
||||||
if len(_list) > 1:
|
if len(_list) > 1:
|
||||||
raise Exception('Index out of range')
|
raise Exception('Index out of range')
|
||||||
elif len(_list) == 0:
|
elif len(_list) == 0:
|
||||||
|
@ -1,9 +1,16 @@
|
|||||||
from collections import Callable
|
from collections import Callable
|
||||||
|
|
||||||
|
from cpl_query.exceptions import ArgumentNoneException, ExceptionArgument
|
||||||
from cpl_query.extension.iterable_abc import IterableABC
|
from cpl_query.extension.iterable_abc import IterableABC
|
||||||
|
|
||||||
|
|
||||||
def where_query(_list: IterableABC, _func: Callable) -> IterableABC:
|
def where_query(_list: IterableABC, _func: Callable) -> IterableABC:
|
||||||
|
if _list is None:
|
||||||
|
raise ArgumentNoneException(ExceptionArgument.list)
|
||||||
|
|
||||||
|
if _func is None:
|
||||||
|
raise ArgumentNoneException(ExceptionArgument.func)
|
||||||
|
|
||||||
result = IterableABC()
|
result = IterableABC()
|
||||||
for element in _list:
|
for element in _list:
|
||||||
if _func(element):
|
if _func(element):
|
||||||
|
@ -1,3 +1,20 @@
|
|||||||
|
from enum import Enum
|
||||||
|
|
||||||
|
|
||||||
|
# models
|
||||||
|
class ExceptionArgument(Enum):
|
||||||
|
list = 'list'
|
||||||
|
func = 'func'
|
||||||
|
type = 'type'
|
||||||
|
|
||||||
|
|
||||||
|
# exceptions
|
||||||
|
class ArgumentNoneException(Exception):
|
||||||
|
|
||||||
|
def __init__(self, arg: ExceptionArgument):
|
||||||
|
Exception.__init__(self, f'Argument {arg} is None')
|
||||||
|
|
||||||
|
|
||||||
class InvalidTypeException(Exception):
|
class InvalidTypeException(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -18,11 +18,4 @@ class IterableTest(unittest.TestCase):
|
|||||||
self._list.append(3)
|
self._list.append(3)
|
||||||
|
|
||||||
self.assertEqual(self._list, [1, 2, 3])
|
self.assertEqual(self._list, [1, 2, 3])
|
||||||
self._clear()
|
|
||||||
|
|
||||||
def test_append_wrong_type(self):
|
|
||||||
self._list.append(1)
|
|
||||||
self._list.append(2)
|
|
||||||
|
|
||||||
self.assertRaises(Exception, lambda v: self._list.append(v), '3')
|
self.assertRaises(Exception, lambda v: self._list.append(v), '3')
|
||||||
self._clear()
|
|
||||||
|
@ -3,7 +3,7 @@ import unittest
|
|||||||
from random import randint
|
from random import randint
|
||||||
|
|
||||||
from cpl.utils import String
|
from cpl.utils import String
|
||||||
from cpl_query.exceptions import InvalidTypeException, WrongTypeException
|
from cpl_query.exceptions import InvalidTypeException, WrongTypeException, ArgumentNoneException
|
||||||
from cpl_query.extension.list import List
|
from cpl_query.extension.list import List
|
||||||
from cpl_query.tests.models import User, Address
|
from cpl_query.tests.models import User, Address
|
||||||
|
|
||||||
@ -70,17 +70,14 @@ class QueryTest(unittest.TestCase):
|
|||||||
|
|
||||||
self.assertEqual(res, avg)
|
self.assertEqual(res, avg)
|
||||||
|
|
||||||
def test_avg_invalid(self):
|
def invalid():
|
||||||
def _():
|
e_res = self._tests.average(str, lambda u: u.address.nr)
|
||||||
res = self._tests.average(str, lambda u: u.address.nr)
|
|
||||||
|
|
||||||
self.assertRaises(InvalidTypeException, _)
|
def wrong():
|
||||||
|
e_res = self._tests.average(int, lambda u: u.address.street)
|
||||||
|
|
||||||
def test_avg_wrong(self):
|
self.assertRaises(InvalidTypeException, invalid)
|
||||||
def _():
|
self.assertRaises(WrongTypeException, wrong)
|
||||||
res = self._tests.average(int, lambda u: u.address.street)
|
|
||||||
|
|
||||||
self.assertRaises(WrongTypeException, _)
|
|
||||||
|
|
||||||
def test_first(self):
|
def test_first(self):
|
||||||
results = []
|
results = []
|
||||||
@ -179,3 +176,8 @@ class QueryTest(unittest.TestCase):
|
|||||||
|
|
||||||
res = self._tests.where(lambda u: u.address.nr == 5)
|
res = self._tests.where(lambda u: u.address.nr == 5)
|
||||||
self.assertEqual(len(results), len(res))
|
self.assertEqual(len(results), len(res))
|
||||||
|
|
||||||
|
def ex():
|
||||||
|
e_res = self._tests.where(None)
|
||||||
|
|
||||||
|
self.assertRaises(ArgumentNoneException, ex)
|
||||||
|
Loading…
Reference in New Issue
Block a user