Added different queries & unittests
This commit is contained in:
0
src/cpl_query/_query/__init__.py
Normal file
0
src/cpl_query/_query/__init__.py
Normal file
7
src/cpl_query/_query/any_query.py
Normal file
7
src/cpl_query/_query/any_query.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from cpl_query._query.where_query import where_query
|
||||
from cpl_query.extension.iterable_abc import IterableABC
|
||||
|
||||
|
||||
def any_query(_list: IterableABC, _func: str) -> bool:
|
||||
result = where_query(_list, _func)
|
||||
return len(result) > 0
|
17
src/cpl_query/_query/first_query.py
Normal file
17
src/cpl_query/_query/first_query.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from typing import Optional
|
||||
|
||||
from cpl_query.extension.iterable_abc import IterableABC
|
||||
|
||||
|
||||
def first_query(_list: IterableABC) -> any:
|
||||
if len(_list) == 0:
|
||||
raise Exception('Index out of range')
|
||||
|
||||
return _list[0]
|
||||
|
||||
|
||||
def first_or_default_query(_list: IterableABC) -> Optional[any]:
|
||||
if len(_list) == 0:
|
||||
return None
|
||||
|
||||
return _list[0]
|
8
src/cpl_query/_query/for_each_query.py
Normal file
8
src/cpl_query/_query/for_each_query.py
Normal file
@@ -0,0 +1,8 @@
|
||||
from collections import Callable
|
||||
|
||||
from cpl_query.extension.iterable_abc import IterableABC
|
||||
|
||||
|
||||
def for_each_query(_list: IterableABC, func: Callable):
|
||||
for element in _list:
|
||||
func(element)
|
21
src/cpl_query/_query/single_query.py
Normal file
21
src/cpl_query/_query/single_query.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from typing import Optional
|
||||
|
||||
from cpl_query.extension.iterable_abc import IterableABC
|
||||
|
||||
|
||||
def single_query(_list: IterableABC) -> any:
|
||||
if len(_list) > 1:
|
||||
raise Exception('Found more than one element')
|
||||
elif len(_list) == 0:
|
||||
raise Exception('Found no element')
|
||||
|
||||
return _list[0]
|
||||
|
||||
|
||||
def single_or_default_query(_list: IterableABC) -> Optional[any]:
|
||||
if len(_list) > 1:
|
||||
raise Exception('Index out of range')
|
||||
elif len(_list) == 0:
|
||||
return None
|
||||
|
||||
return _list[0]
|
13
src/cpl_query/_query/where_query.py
Normal file
13
src/cpl_query/_query/where_query.py
Normal file
@@ -0,0 +1,13 @@
|
||||
from cpl_query.extension.iterable_abc import IterableABC
|
||||
|
||||
|
||||
def where_query(_list: IterableABC, _func: str) -> IterableABC:
|
||||
result = IterableABC()
|
||||
for element in _list:
|
||||
element_type = type(element).__name__
|
||||
if element_type in _func:
|
||||
func = _func.replace(element_type, 'element')
|
||||
if eval(func):
|
||||
result.append(element)
|
||||
|
||||
return result
|
Reference in New Issue
Block a user