Compare commits
5 Commits
2025.09.23
...
2025.09.24
| Author | SHA1 | Date | |
|---|---|---|---|
| 2a927ebdd5 | |||
| 01a2ff7166 | |||
| 2da6d679ad | |||
| a1cfe76047 | |||
| c71a3df62c |
@@ -4,8 +4,10 @@ from cpl import api
|
|||||||
from cpl.api.application.web_app import WebApp
|
from cpl.api.application.web_app import WebApp
|
||||||
from cpl.application import ApplicationBuilder
|
from cpl.application import ApplicationBuilder
|
||||||
from cpl.auth.permission.permissions import Permissions
|
from cpl.auth.permission.permissions import Permissions
|
||||||
|
from cpl.auth.schema import AuthUser, Role
|
||||||
from cpl.core.configuration import Configuration
|
from cpl.core.configuration import Configuration
|
||||||
from cpl.core.environment import Environment
|
from cpl.core.environment import Environment
|
||||||
|
from cpl.core.utils.cache import Cache
|
||||||
from service import PingService
|
from service import PingService
|
||||||
|
|
||||||
|
|
||||||
@@ -21,6 +23,9 @@ def main():
|
|||||||
builder.services.add_transient(PingService)
|
builder.services.add_transient(PingService)
|
||||||
builder.services.add_module(api)
|
builder.services.add_module(api)
|
||||||
|
|
||||||
|
builder.services.add_cache(AuthUser)
|
||||||
|
builder.services.add_cache(Role)
|
||||||
|
|
||||||
app = builder.build()
|
app = builder.build()
|
||||||
app.with_logging()
|
app.with_logging()
|
||||||
app.with_database()
|
app.with_database()
|
||||||
@@ -31,6 +36,10 @@ def main():
|
|||||||
app.with_route(path="/route1", fn=lambda r: JSONResponse("route1"), method="GET", authentication=True, permissions=[Permissions.administrator])
|
app.with_route(path="/route1", fn=lambda r: JSONResponse("route1"), method="GET", authentication=True, permissions=[Permissions.administrator])
|
||||||
app.with_routes_directory("routes")
|
app.with_routes_directory("routes")
|
||||||
|
|
||||||
|
provider = builder.service_provider
|
||||||
|
user_cache = provider.get_service(Cache[AuthUser])
|
||||||
|
role_cache = provider.get_service(Cache[Role])
|
||||||
|
|
||||||
app.run()
|
app.run()
|
||||||
|
|
||||||
|
|
||||||
60
example/custom/query/main.py
Normal file
60
example/custom/query/main.py
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
from cpl.core.console import Console
|
||||||
|
from cpl.core.utils.benchmark import Benchmark
|
||||||
|
from cpl.query.enumerable import Enumerable
|
||||||
|
from cpl.query.immutable_list import ImmutableList
|
||||||
|
from cpl.query.list import List
|
||||||
|
from cpl.query.set import Set
|
||||||
|
|
||||||
|
|
||||||
|
def _default():
|
||||||
|
Console.write_line(Enumerable.empty().to_list())
|
||||||
|
|
||||||
|
Console.write_line(Enumerable.range(0, 100).length)
|
||||||
|
Console.write_line(Enumerable.range(0, 100).to_list())
|
||||||
|
|
||||||
|
Console.write_line(Enumerable.range(0, 100).where(lambda x: x % 2 == 0).length)
|
||||||
|
Console.write_line(
|
||||||
|
Enumerable.range(0, 100).where(lambda x: x % 2 == 0).to_list().select(lambda x: str(x)).to_list()
|
||||||
|
)
|
||||||
|
Console.write_line(List)
|
||||||
|
|
||||||
|
s =Enumerable.range(0, 10).to_set()
|
||||||
|
Console.write_line(s)
|
||||||
|
s.add(1)
|
||||||
|
Console.write_line(s)
|
||||||
|
|
||||||
|
data = Enumerable(
|
||||||
|
[
|
||||||
|
{"name": "Alice", "age": 30},
|
||||||
|
{"name": "Dave", "age": 35},
|
||||||
|
{"name": "Charlie", "age": 25},
|
||||||
|
{"name": "Bob", "age": 25},
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
Console.write_line(data.order_by(lambda x: x["age"]).to_list())
|
||||||
|
Console.write_line(data.order_by(lambda x: x["age"]).then_by(lambda x: x["name"]).to_list())
|
||||||
|
Console.write_line(data.order_by(lambda x: x["name"]).then_by(lambda x: x["age"]).to_list())
|
||||||
|
|
||||||
|
|
||||||
|
def t_benchmark(data: list):
|
||||||
|
Benchmark.all("Enumerable", lambda: Enumerable(data).where(lambda x: x % 2 == 0).select(lambda x: x * 2).to_list())
|
||||||
|
Benchmark.all("Set", lambda: Set(data).where(lambda x: x % 2 == 0).select(lambda x: x * 2).to_list())
|
||||||
|
Benchmark.all("List", lambda: List(data).where(lambda x: x % 2 == 0).select(lambda x: x * 2).to_list())
|
||||||
|
Benchmark.all(
|
||||||
|
"ImmutableList", lambda: ImmutableList(data).where(lambda x: x % 2 == 0).select(lambda x: x * 2).to_list()
|
||||||
|
)
|
||||||
|
Benchmark.all("List comprehension", lambda: [x * 2 for x in data if x % 2 == 0])
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
N = 10_000_000
|
||||||
|
data = list(range(N))
|
||||||
|
#t_benchmark(data)
|
||||||
|
|
||||||
|
Console.write_line()
|
||||||
|
_default()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -1,7 +1,8 @@
|
|||||||
import inspect
|
import inspect
|
||||||
|
from typing import Type
|
||||||
|
|
||||||
from cpl.core.log import LoggerABC, LogLevel
|
from cpl.core.log import LoggerABC, LogLevel
|
||||||
from cpl.core.typing import Messages, Source
|
from cpl.core.typing import Messages
|
||||||
from cpl.dependency.service_provider_abc import ServiceProviderABC
|
from cpl.dependency.service_provider_abc import ServiceProviderABC
|
||||||
|
|
||||||
|
|
||||||
@@ -11,18 +12,20 @@ class WrappedLogger(LoggerABC):
|
|||||||
LoggerABC.__init__(self)
|
LoggerABC.__init__(self)
|
||||||
assert file_prefix is not None and file_prefix != "", "file_prefix must be a non-empty string"
|
assert file_prefix is not None and file_prefix != "", "file_prefix must be a non-empty string"
|
||||||
|
|
||||||
t_logger = ServiceProviderABC.get_global_service(LoggerABC)
|
|
||||||
self._t_logger = type(t_logger) if t_logger is not None else None
|
|
||||||
self._source = None
|
self._source = None
|
||||||
self._file_prefix = file_prefix
|
self._file_prefix = file_prefix
|
||||||
|
|
||||||
self._set_logger()
|
self._set_logger()
|
||||||
|
|
||||||
def _set_logger(self):
|
@ServiceProviderABC.inject
|
||||||
if self._t_logger is None:
|
def _set_logger(self, services: ServiceProviderABC):
|
||||||
|
from cpl.core.log import Logger
|
||||||
|
|
||||||
|
t_logger: Type[Logger] = services.get_service_type(LoggerABC)
|
||||||
|
if t_logger is None:
|
||||||
raise Exception("No LoggerABC service registered in ServiceProviderABC")
|
raise Exception("No LoggerABC service registered in ServiceProviderABC")
|
||||||
|
|
||||||
self._logger = self._t_logger(self._source, self._file_prefix)
|
self._logger = t_logger(self._source, self._file_prefix)
|
||||||
|
|
||||||
def set_level(self, level: LogLevel):
|
def set_level(self, level: LogLevel):
|
||||||
self._logger.set_level(level)
|
self._logger.set_level(level)
|
||||||
|
|||||||
@@ -14,3 +14,4 @@ UuidId = str | UUID
|
|||||||
SerialId = int
|
SerialId = int
|
||||||
|
|
||||||
Id = UuidId | SerialId
|
Id = UuidId | SerialId
|
||||||
|
TNumber = int | float | complex
|
||||||
|
|||||||
57
src/cpl-core/cpl/core/utils/benchmark.py
Normal file
57
src/cpl-core/cpl/core/utils/benchmark.py
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
import time
|
||||||
|
import tracemalloc
|
||||||
|
from typing import List, Callable
|
||||||
|
|
||||||
|
from cpl.core.console import Console
|
||||||
|
|
||||||
|
|
||||||
|
class Benchmark:
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def all(label: str, func: Callable, iterations: int = 5):
|
||||||
|
times: List[float] = []
|
||||||
|
mems: List[float] = []
|
||||||
|
|
||||||
|
for _ in range(iterations):
|
||||||
|
start = time.perf_counter()
|
||||||
|
func()
|
||||||
|
end = time.perf_counter()
|
||||||
|
times.append(end - start)
|
||||||
|
|
||||||
|
for _ in range(iterations):
|
||||||
|
tracemalloc.start()
|
||||||
|
func()
|
||||||
|
current, peak = tracemalloc.get_traced_memory()
|
||||||
|
tracemalloc.stop()
|
||||||
|
mems.append(peak)
|
||||||
|
|
||||||
|
avg_time = sum(times) / len(times)
|
||||||
|
avg_mem = sum(mems) / len(mems) / (1024 * 1024)
|
||||||
|
Console.write_line(f"{label:20s} -> min {min(times):.6f}s avg {avg_time:.6f}s mem {avg_mem:.8f} MB")
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def time(label: str, func: Callable, iterations: int = 5):
|
||||||
|
times: List[float] = []
|
||||||
|
|
||||||
|
for _ in range(iterations):
|
||||||
|
start = time.perf_counter()
|
||||||
|
func()
|
||||||
|
end = time.perf_counter()
|
||||||
|
times.append(end - start)
|
||||||
|
|
||||||
|
avg_time = sum(times) / len(times)
|
||||||
|
Console.write_line(f"{label:20s} -> min {min(times):.6f}s avg {avg_time:.6f}s")
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def memory(label: str, func: Callable, iterations: int = 5):
|
||||||
|
mems: List[float] = []
|
||||||
|
|
||||||
|
for _ in range(iterations):
|
||||||
|
tracemalloc.start()
|
||||||
|
func()
|
||||||
|
current, peak = tracemalloc.get_traced_memory()
|
||||||
|
tracemalloc.stop()
|
||||||
|
mems.append(peak)
|
||||||
|
|
||||||
|
avg_mem = sum(mems) / len(mems) / (1024 * 1024)
|
||||||
|
Console.write_line(f"{label:20s} -> mem {avg_mem:.2f} MB")
|
||||||
100
src/cpl-core/cpl/core/utils/cache.py
Normal file
100
src/cpl-core/cpl/core/utils/cache.py
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
import threading
|
||||||
|
import time
|
||||||
|
from typing import Generic
|
||||||
|
|
||||||
|
from cpl.core.typing import T
|
||||||
|
|
||||||
|
|
||||||
|
class Cache(Generic[T]):
|
||||||
|
def __init__(self, default_ttl: int = None, cleanup_interval: int = 60, t: type = None):
|
||||||
|
self._store = {}
|
||||||
|
self._default_ttl = default_ttl
|
||||||
|
self._lock = threading.Lock()
|
||||||
|
self._cleanup_interval = cleanup_interval
|
||||||
|
self._stop_event = threading.Event()
|
||||||
|
|
||||||
|
self._type = t
|
||||||
|
|
||||||
|
# Start background cleanup thread
|
||||||
|
self._thread = threading.Thread(target=self._auto_cleanup, daemon=True)
|
||||||
|
self._thread.start()
|
||||||
|
|
||||||
|
def set(self, key: str, value: T, ttl: int = None) -> None:
|
||||||
|
"""Store a value in the cache with optional TTL override."""
|
||||||
|
expire_at = None
|
||||||
|
ttl = ttl if ttl is not None else self._default_ttl
|
||||||
|
if ttl is not None:
|
||||||
|
expire_at = time.time() + ttl
|
||||||
|
|
||||||
|
with self._lock:
|
||||||
|
self._store[key] = (value, expire_at)
|
||||||
|
|
||||||
|
def get(self, key: str) -> T | None:
|
||||||
|
"""Retrieve a value from the cache if not expired."""
|
||||||
|
with self._lock:
|
||||||
|
item = self._store.get(key)
|
||||||
|
if not item:
|
||||||
|
return None
|
||||||
|
value, expire_at = item
|
||||||
|
if expire_at and expire_at < time.time():
|
||||||
|
# Expired -> remove and return None
|
||||||
|
del self._store[key]
|
||||||
|
return None
|
||||||
|
return value
|
||||||
|
|
||||||
|
def get_all(self) -> list[T]:
|
||||||
|
"""Retrieve all non-expired values from the cache."""
|
||||||
|
now = time.time()
|
||||||
|
with self._lock:
|
||||||
|
valid_items = []
|
||||||
|
expired_keys = []
|
||||||
|
for k, (v, exp) in self._store.items():
|
||||||
|
if exp and exp < now:
|
||||||
|
expired_keys.append(k)
|
||||||
|
else:
|
||||||
|
valid_items.append(v)
|
||||||
|
for k in expired_keys:
|
||||||
|
del self._store[k]
|
||||||
|
return valid_items
|
||||||
|
|
||||||
|
def has(self, key: str) -> bool:
|
||||||
|
"""Check if a key exists and is not expired."""
|
||||||
|
with self._lock:
|
||||||
|
item = self._store.get(key)
|
||||||
|
if not item:
|
||||||
|
return False
|
||||||
|
_, expire_at = item
|
||||||
|
if expire_at and expire_at < time.time():
|
||||||
|
# Expired -> remove and return False
|
||||||
|
del self._store[key]
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
def delete(self, key: str) -> None:
|
||||||
|
"""Remove an item from the cache."""
|
||||||
|
with self._lock:
|
||||||
|
self._store.pop(key, None)
|
||||||
|
|
||||||
|
def clear(self) -> None:
|
||||||
|
"""Clear the entire cache."""
|
||||||
|
with self._lock:
|
||||||
|
self._store.clear()
|
||||||
|
|
||||||
|
def _auto_cleanup(self):
|
||||||
|
"""Background thread to clean expired items."""
|
||||||
|
while not self._stop_event.is_set():
|
||||||
|
self.cleanup()
|
||||||
|
self._stop_event.wait(self._cleanup_interval)
|
||||||
|
|
||||||
|
def cleanup(self) -> None:
|
||||||
|
"""Remove expired items immediately."""
|
||||||
|
now = time.time()
|
||||||
|
with self._lock:
|
||||||
|
expired_keys = [k for k, (_, exp) in self._store.items() if exp and exp < now]
|
||||||
|
for k in expired_keys:
|
||||||
|
del self._store[k]
|
||||||
|
|
||||||
|
def stop(self):
|
||||||
|
"""Stop the background cleanup thread."""
|
||||||
|
self._stop_event.set()
|
||||||
|
self._thread.join()
|
||||||
48
src/cpl-core/cpl/core/utils/number.py
Normal file
48
src/cpl-core/cpl/core/utils/number.py
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
from typing import Any
|
||||||
|
|
||||||
|
|
||||||
|
class Number:
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def is_number(value: Any) -> bool:
|
||||||
|
"""Check if the value is a number (int or float)."""
|
||||||
|
return isinstance(value, (int, float, complex))
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def to_number(value: Any) -> int | float | complex:
|
||||||
|
"""
|
||||||
|
Convert a given value into int, float, or complex.
|
||||||
|
Raises ValueError if conversion is not possible.
|
||||||
|
"""
|
||||||
|
|
||||||
|
if isinstance(value, (int, float, complex)):
|
||||||
|
return value
|
||||||
|
|
||||||
|
if isinstance(value, str):
|
||||||
|
value = value.strip()
|
||||||
|
for caster in (int, float, complex):
|
||||||
|
try:
|
||||||
|
return caster(value)
|
||||||
|
except ValueError:
|
||||||
|
continue
|
||||||
|
raise ValueError(f"Cannot convert string '{value}' to number.")
|
||||||
|
|
||||||
|
if isinstance(value, bool):
|
||||||
|
return int(value)
|
||||||
|
|
||||||
|
try:
|
||||||
|
return int(value)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
try:
|
||||||
|
return float(value)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
try:
|
||||||
|
return complex(value)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
raise ValueError(f"Cannot convert type {type(value)} to number.")
|
||||||
@@ -2,6 +2,7 @@ from typing import Union, Type, Callable, Self
|
|||||||
|
|
||||||
from cpl.core.log.logger_abc import LoggerABC
|
from cpl.core.log.logger_abc import LoggerABC
|
||||||
from cpl.core.typing import T, Service
|
from cpl.core.typing import T, Service
|
||||||
|
from cpl.core.utils.cache import Cache
|
||||||
from cpl.dependency.service_descriptor import ServiceDescriptor
|
from cpl.dependency.service_descriptor import ServiceDescriptor
|
||||||
from cpl.dependency.service_lifetime_enum import ServiceLifetimeEnum
|
from cpl.dependency.service_lifetime_enum import ServiceLifetimeEnum
|
||||||
from cpl.dependency.service_provider import ServiceProvider
|
from cpl.dependency.service_provider import ServiceProvider
|
||||||
@@ -96,3 +97,7 @@ class ServiceCollection:
|
|||||||
for wrapper in WrappedLogger.__subclasses__():
|
for wrapper in WrappedLogger.__subclasses__():
|
||||||
self.add_transient(wrapper)
|
self.add_transient(wrapper)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
def add_cache(self, t: Type[T]):
|
||||||
|
self._service_descriptors.append(ServiceDescriptor(Cache(t=t), ServiceLifetimeEnum.singleton, Cache[t]))
|
||||||
|
return self
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import copy
|
import copy
|
||||||
import typing
|
import typing
|
||||||
from inspect import signature, Parameter, Signature
|
from inspect import signature, Parameter, Signature
|
||||||
from typing import Optional
|
from typing import Optional, Type
|
||||||
|
|
||||||
from cpl.core.configuration import Configuration
|
from cpl.core.configuration import Configuration
|
||||||
from cpl.core.configuration.configuration_model_abc import ConfigurationModelABC
|
from cpl.core.configuration.configuration_model_abc import ConfigurationModelABC
|
||||||
@@ -37,8 +37,23 @@ class ServiceProvider(ServiceProviderABC):
|
|||||||
self._scope: Optional[ScopeABC] = None
|
self._scope: Optional[ScopeABC] = None
|
||||||
|
|
||||||
def _find_service(self, service_type: type) -> Optional[ServiceDescriptor]:
|
def _find_service(self, service_type: type) -> Optional[ServiceDescriptor]:
|
||||||
|
origin_type = typing.get_origin(service_type) or service_type
|
||||||
|
type_args = list(typing.get_args(service_type))
|
||||||
|
|
||||||
for descriptor in self._service_descriptors:
|
for descriptor in self._service_descriptors:
|
||||||
if descriptor.service_type == service_type or issubclass(descriptor.base_type, service_type):
|
descriptor_base_type = typing.get_origin(descriptor.base_type) or descriptor.base_type
|
||||||
|
descriptor_type_args = list(typing.get_args(descriptor.base_type))
|
||||||
|
|
||||||
|
if descriptor_base_type == origin_type and len(descriptor_type_args) == 0 and len(type_args) == 0:
|
||||||
|
return descriptor
|
||||||
|
|
||||||
|
if descriptor_base_type != origin_type or len(descriptor_type_args) != len(type_args):
|
||||||
|
continue
|
||||||
|
|
||||||
|
if descriptor_base_type == origin_type and type_args != descriptor_type_args:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if descriptor.service_type == origin_type or issubclass(descriptor.base_type, origin_type):
|
||||||
return descriptor
|
return descriptor
|
||||||
|
|
||||||
return None
|
return None
|
||||||
@@ -158,6 +173,12 @@ class ServiceProvider(ServiceProviderABC):
|
|||||||
|
|
||||||
return implementation
|
return implementation
|
||||||
|
|
||||||
|
def get_service_type(self, service_type: Type[T]) -> Optional[Type[T]]:
|
||||||
|
for descriptor in self._service_descriptors:
|
||||||
|
if descriptor.service_type == service_type or issubclass(descriptor.service_type, service_type):
|
||||||
|
return descriptor.service_type
|
||||||
|
return None
|
||||||
|
|
||||||
def get_services(self, service_type: T, *args, **kwargs) -> list[Optional[R]]:
|
def get_services(self, service_type: T, *args, **kwargs) -> list[Optional[R]]:
|
||||||
implementations = []
|
implementations = []
|
||||||
|
|
||||||
@@ -167,3 +188,10 @@ class ServiceProvider(ServiceProviderABC):
|
|||||||
implementations.extend(self._get_services(service_type))
|
implementations.extend(self._get_services(service_type))
|
||||||
|
|
||||||
return implementations
|
return implementations
|
||||||
|
|
||||||
|
def get_service_types(self, service_type: Type[T]) -> list[Type[T]]:
|
||||||
|
types = []
|
||||||
|
for descriptor in self._service_descriptors:
|
||||||
|
if descriptor.service_type == service_type or issubclass(descriptor.service_type, service_type):
|
||||||
|
types.append(descriptor.service_type)
|
||||||
|
return types
|
||||||
|
|||||||
@@ -85,6 +85,20 @@ class ServiceProviderABC(ABC):
|
|||||||
Object of type Optional[:class:`cpl.core.type.T`]
|
Object of type Optional[:class:`cpl.core.type.T`]
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def get_service_type(self, instance_type: Type[T]) -> Optional[Type[T]]:
|
||||||
|
r"""Returns the registered service type for loggers
|
||||||
|
|
||||||
|
Parameter
|
||||||
|
---------
|
||||||
|
instance_type: :class:`cpl.core.type.T`
|
||||||
|
The type of the searched instance
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
Object of type Optional[:class:`type`]
|
||||||
|
"""
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def get_services(self, service_type: Type[T], *args, **kwargs) -> list[Optional[T]]:
|
def get_services(self, service_type: Type[T], *args, **kwargs) -> list[Optional[T]]:
|
||||||
r"""Returns instance of given type
|
r"""Returns instance of given type
|
||||||
@@ -99,6 +113,20 @@ class ServiceProviderABC(ABC):
|
|||||||
Object of type list[Optional[:class:`cpl.core.type.T`]
|
Object of type list[Optional[:class:`cpl.core.type.T`]
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def get_service_types(self, service_type: Type[T]) -> list[Type[T]]:
|
||||||
|
r"""Returns all registered service types
|
||||||
|
|
||||||
|
Parameter
|
||||||
|
---------
|
||||||
|
service_type: :class:`cpl.core.type.T`
|
||||||
|
The type of the searched instance
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
Object of type list[:class:`type`]
|
||||||
|
"""
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def inject(cls, f=None):
|
def inject(cls, f=None):
|
||||||
r"""Decorator to allow injection into static and class methods
|
r"""Decorator to allow injection into static and class methods
|
||||||
|
|||||||
@@ -1 +1,7 @@
|
|||||||
|
from .array import Array
|
||||||
|
from .enumerable import Enumerable
|
||||||
|
from .immutable_list import ImmutableList
|
||||||
|
from .immutable_set import ImmutableSet
|
||||||
|
from .list import List
|
||||||
|
from .ordered_enumerable import OrderedEnumerable
|
||||||
|
from .set import Set
|
||||||
|
|||||||
@@ -1,2 +0,0 @@
|
|||||||
def is_number(t: type) -> bool:
|
|
||||||
return issubclass(t, int) or issubclass(t, float) or issubclass(t, complex)
|
|
||||||
44
src/cpl-query/cpl/query/array.py
Normal file
44
src/cpl-query/cpl/query/array.py
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
from typing import Generic, Iterable, Optional
|
||||||
|
|
||||||
|
from cpl.core.typing import T
|
||||||
|
from cpl.query.list import List
|
||||||
|
from cpl.query.enumerable import Enumerable
|
||||||
|
|
||||||
|
|
||||||
|
class Array(Generic[T], List[T]):
|
||||||
|
def __init__(self, length: int, source: Optional[Iterable[T]] = None):
|
||||||
|
List.__init__(self, source)
|
||||||
|
self._length = length
|
||||||
|
|
||||||
|
@property
|
||||||
|
def length(self) -> int:
|
||||||
|
return len(self._source)
|
||||||
|
|
||||||
|
def add(self, item: T) -> None:
|
||||||
|
if self._length == self.length:
|
||||||
|
raise IndexError("Array is full")
|
||||||
|
self._source.append(item)
|
||||||
|
|
||||||
|
def extend(self, items: Iterable[T]) -> None:
|
||||||
|
if self._length == self.length:
|
||||||
|
raise IndexError("Array is full")
|
||||||
|
self._source.extend(items)
|
||||||
|
|
||||||
|
def insert(self, index: int, item: T) -> None:
|
||||||
|
if index < 0 or index > self.length:
|
||||||
|
raise IndexError("Index out of range")
|
||||||
|
self._source.insert(index, item)
|
||||||
|
|
||||||
|
def remove(self, item: T) -> None:
|
||||||
|
self._source.remove(item)
|
||||||
|
|
||||||
|
def pop(self, index: int = -1) -> T:
|
||||||
|
return self._source.pop(index)
|
||||||
|
|
||||||
|
def clear(self) -> None:
|
||||||
|
self._source.clear()
|
||||||
|
|
||||||
|
def to_enumerable(self) -> "Enumerable[T]":
|
||||||
|
from cpl.query.enumerable import Enumerable
|
||||||
|
|
||||||
|
return Enumerable(self._source)
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
from .default_lambda import default_lambda
|
|
||||||
from .ordered_queryable import OrderedQueryable
|
|
||||||
from .sequence import Sequence
|
|
||||||
from .ordered_queryable_abc import OrderedQueryableABC
|
|
||||||
from .queryable_abc import QueryableABC
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
def default_lambda(x: object):
|
|
||||||
return x
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
from collections.abc import Callable
|
|
||||||
|
|
||||||
from cpl.query.base.ordered_queryable_abc import OrderedQueryableABC
|
|
||||||
from cpl.query.exceptions import ArgumentNoneException, ExceptionArgument
|
|
||||||
|
|
||||||
|
|
||||||
class OrderedQueryable(OrderedQueryableABC):
|
|
||||||
r"""Implementation of :class: `cpl.query.base.ordered_queryable_abc.OrderedQueryableABC`"""
|
|
||||||
|
|
||||||
def __init__(self, _t: type, _values: OrderedQueryableABC = None, _func: Callable = None):
|
|
||||||
OrderedQueryableABC.__init__(self, _t, _values, _func)
|
|
||||||
|
|
||||||
def then_by(self, _func: Callable) -> OrderedQueryableABC:
|
|
||||||
if self is None:
|
|
||||||
raise ArgumentNoneException(ExceptionArgument.list)
|
|
||||||
|
|
||||||
if _func is None:
|
|
||||||
raise ArgumentNoneException(ExceptionArgument.func)
|
|
||||||
|
|
||||||
self._funcs.append(_func)
|
|
||||||
|
|
||||||
return OrderedQueryable(self.type, sorted(self, key=lambda *args: [f(*args) for f in self._funcs]), _func)
|
|
||||||
|
|
||||||
def then_by_descending(self, _func: Callable) -> OrderedQueryableABC:
|
|
||||||
if self is None:
|
|
||||||
raise ArgumentNoneException(ExceptionArgument.list)
|
|
||||||
|
|
||||||
if _func is None:
|
|
||||||
raise ArgumentNoneException(ExceptionArgument.func)
|
|
||||||
|
|
||||||
self._funcs.append(_func)
|
|
||||||
return OrderedQueryable(
|
|
||||||
self.type, sorted(self, key=lambda *args: [f(*args) for f in self._funcs], reverse=True), _func
|
|
||||||
)
|
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
from abc import abstractmethod
|
|
||||||
from collections.abc import Callable
|
|
||||||
from typing import Iterable
|
|
||||||
|
|
||||||
from cpl.query.base.queryable_abc import QueryableABC
|
|
||||||
|
|
||||||
|
|
||||||
class OrderedQueryableABC(QueryableABC):
|
|
||||||
@abstractmethod
|
|
||||||
def __init__(self, _t: type, _values: Iterable = None, _func: Callable = None):
|
|
||||||
QueryableABC.__init__(self, _t, _values)
|
|
||||||
self._funcs: list[Callable] = []
|
|
||||||
if _func is not None:
|
|
||||||
self._funcs.append(_func)
|
|
||||||
|
|
||||||
@abstractmethod
|
|
||||||
def then_by(self, func: Callable) -> OrderedQueryableABC:
|
|
||||||
r"""Sorts OrderedList in ascending order by function
|
|
||||||
|
|
||||||
Parameter:
|
|
||||||
func: :class:`Callable`
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
list of :class:`cpl.query.base.ordered_queryable_abc.OrderedQueryableABC`
|
|
||||||
"""
|
|
||||||
|
|
||||||
@abstractmethod
|
|
||||||
def then_by_descending(self, func: Callable) -> OrderedQueryableABC:
|
|
||||||
r"""Sorts OrderedList in descending order by function
|
|
||||||
|
|
||||||
Parameter:
|
|
||||||
func: :class:`Callable`
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
list of :class:`cpl.query.base.ordered_queryable_abc.OrderedQueryableABC`
|
|
||||||
"""
|
|
||||||
@@ -1,569 +0,0 @@
|
|||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
from typing import Optional, Callable, Union, Iterable, Any
|
|
||||||
|
|
||||||
from cpl.query._helper import is_number
|
|
||||||
from cpl.query.base import default_lambda
|
|
||||||
from cpl.query.base.sequence import Sequence
|
|
||||||
from cpl.query.exceptions import (
|
|
||||||
InvalidTypeException,
|
|
||||||
ArgumentNoneException,
|
|
||||||
ExceptionArgument,
|
|
||||||
IndexOutOfRangeException,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class QueryableABC(Sequence):
|
|
||||||
def __init__(self, t: type, values: Iterable = None):
|
|
||||||
Sequence.__init__(self, t, values)
|
|
||||||
|
|
||||||
def all(self, _func: Callable = None) -> bool:
|
|
||||||
r"""Checks if every element of list equals result found by function
|
|
||||||
|
|
||||||
Parameter
|
|
||||||
---------
|
|
||||||
func: :class:`Callable`
|
|
||||||
selected value
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
bool
|
|
||||||
"""
|
|
||||||
if _func is None:
|
|
||||||
_func = default_lambda
|
|
||||||
|
|
||||||
return self.count(_func) == self.count()
|
|
||||||
|
|
||||||
def any(self, _func: Callable = None) -> bool:
|
|
||||||
r"""Checks if list contains result found by function
|
|
||||||
|
|
||||||
Parameter
|
|
||||||
---------
|
|
||||||
func: :class:`Callable`
|
|
||||||
selected value
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
bool
|
|
||||||
"""
|
|
||||||
if _func is None:
|
|
||||||
_func = default_lambda
|
|
||||||
|
|
||||||
return self.where(_func).count() > 0
|
|
||||||
|
|
||||||
def average(self, _func: Callable = None) -> Union[int, float, complex]:
|
|
||||||
r"""Returns average value of list
|
|
||||||
|
|
||||||
Parameter
|
|
||||||
---------
|
|
||||||
func: :class:`Callable`
|
|
||||||
selected value
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
Union[int, float, complex]
|
|
||||||
"""
|
|
||||||
if _func is None and not is_number(self.type):
|
|
||||||
raise InvalidTypeException()
|
|
||||||
|
|
||||||
return self.sum(_func) / self.count()
|
|
||||||
|
|
||||||
def contains(self, _value: object) -> bool:
|
|
||||||
r"""Checks if list contains value given by function
|
|
||||||
|
|
||||||
Parameter
|
|
||||||
---------
|
|
||||||
value: :class:`object`
|
|
||||||
value
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
bool
|
|
||||||
"""
|
|
||||||
if _value is None:
|
|
||||||
raise ArgumentNoneException(ExceptionArgument.value)
|
|
||||||
|
|
||||||
return self.where(lambda x: x == _value).count() > 0
|
|
||||||
|
|
||||||
def count(self, _func: Callable = None) -> int:
|
|
||||||
r"""Returns length of list or count of found elements
|
|
||||||
|
|
||||||
Parameter
|
|
||||||
---------
|
|
||||||
func: :class:`Callable`
|
|
||||||
selected value
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
int
|
|
||||||
"""
|
|
||||||
if _func is None:
|
|
||||||
return self.__len__()
|
|
||||||
|
|
||||||
return self.where(_func).count()
|
|
||||||
|
|
||||||
def distinct(self, _func: Callable = None) -> QueryableABC:
|
|
||||||
r"""Returns list without redundancies
|
|
||||||
|
|
||||||
Parameter
|
|
||||||
---------
|
|
||||||
func: :class:`Callable`
|
|
||||||
selected value
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
:class: `cpl.query.base.queryable_abc.QueryableABC`
|
|
||||||
"""
|
|
||||||
if _func is None:
|
|
||||||
_func = default_lambda
|
|
||||||
|
|
||||||
result = []
|
|
||||||
known_values = []
|
|
||||||
for element in self:
|
|
||||||
value = _func(element)
|
|
||||||
if value in known_values:
|
|
||||||
continue
|
|
||||||
|
|
||||||
known_values.append(value)
|
|
||||||
result.append(element)
|
|
||||||
|
|
||||||
return type(self)(self._type, result)
|
|
||||||
|
|
||||||
def element_at(self, _index: int) -> any:
|
|
||||||
r"""Returns element at given index
|
|
||||||
|
|
||||||
Parameter
|
|
||||||
---------
|
|
||||||
_index: :class:`int`
|
|
||||||
index
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
Value at _index: any
|
|
||||||
"""
|
|
||||||
if _index is None:
|
|
||||||
raise ArgumentNoneException(ExceptionArgument.index)
|
|
||||||
|
|
||||||
if _index < 0 or _index >= self.count():
|
|
||||||
raise IndexOutOfRangeException
|
|
||||||
|
|
||||||
result = self._values[_index]
|
|
||||||
if result is None:
|
|
||||||
raise IndexOutOfRangeException
|
|
||||||
|
|
||||||
return result
|
|
||||||
|
|
||||||
def element_at_or_default(self, _index: int) -> Optional[any]:
|
|
||||||
r"""Returns element at given index or None
|
|
||||||
|
|
||||||
Parameter
|
|
||||||
---------
|
|
||||||
_index: :class:`int`
|
|
||||||
index
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
Value at _index: Optional[any]
|
|
||||||
"""
|
|
||||||
if _index is None:
|
|
||||||
raise ArgumentNoneException(ExceptionArgument.index)
|
|
||||||
|
|
||||||
try:
|
|
||||||
return self._values[_index]
|
|
||||||
except IndexError:
|
|
||||||
return None
|
|
||||||
|
|
||||||
def first(self) -> any:
|
|
||||||
r"""Returns first element
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
First element of list: any
|
|
||||||
"""
|
|
||||||
if self.count() == 0:
|
|
||||||
raise IndexOutOfRangeException()
|
|
||||||
|
|
||||||
return self._values[0]
|
|
||||||
|
|
||||||
def first_or_default(self) -> any:
|
|
||||||
r"""Returns first element or None
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
First element of list: Optional[any]
|
|
||||||
"""
|
|
||||||
if self.count() == 0:
|
|
||||||
return None
|
|
||||||
|
|
||||||
return self._values[0]
|
|
||||||
|
|
||||||
def for_each(self, _func: Callable = None):
|
|
||||||
r"""Runs given function for each element of list
|
|
||||||
|
|
||||||
Parameter
|
|
||||||
---------
|
|
||||||
func: :class: `Callable`
|
|
||||||
function to call
|
|
||||||
"""
|
|
||||||
if _func is not None:
|
|
||||||
for element in self:
|
|
||||||
_func(element)
|
|
||||||
|
|
||||||
return self
|
|
||||||
|
|
||||||
def group_by(self, _func: Callable = None) -> QueryableABC:
|
|
||||||
r"""Groups by func
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
Grouped list[list[any]]: any
|
|
||||||
"""
|
|
||||||
if _func is None:
|
|
||||||
_func = default_lambda
|
|
||||||
groups = {}
|
|
||||||
|
|
||||||
for v in self:
|
|
||||||
value = _func(v)
|
|
||||||
if v not in groups:
|
|
||||||
groups[value] = []
|
|
||||||
|
|
||||||
groups[value].append(v)
|
|
||||||
|
|
||||||
v = []
|
|
||||||
for g in groups.values():
|
|
||||||
v.append(type(self)(object, g))
|
|
||||||
x = type(self)(type(self), v)
|
|
||||||
return x
|
|
||||||
|
|
||||||
def last(self) -> any:
|
|
||||||
r"""Returns last element
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
Last element of list: any
|
|
||||||
"""
|
|
||||||
if self.count() == 0:
|
|
||||||
raise IndexOutOfRangeException()
|
|
||||||
|
|
||||||
return self._values[self.count() - 1]
|
|
||||||
|
|
||||||
def last_or_default(self) -> any:
|
|
||||||
r"""Returns last element or None
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
Last element of list: Optional[any]
|
|
||||||
"""
|
|
||||||
if self.count() == 0:
|
|
||||||
return None
|
|
||||||
|
|
||||||
return self._values[self.count() - 1]
|
|
||||||
|
|
||||||
def max(self, _func: Callable = None) -> object:
|
|
||||||
r"""Returns the highest value
|
|
||||||
|
|
||||||
Parameter
|
|
||||||
---------
|
|
||||||
func: :class:`Callable`
|
|
||||||
selected value
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
object
|
|
||||||
"""
|
|
||||||
if _func is None and not is_number(self.type):
|
|
||||||
raise InvalidTypeException()
|
|
||||||
|
|
||||||
if _func is None:
|
|
||||||
_func = default_lambda
|
|
||||||
|
|
||||||
return _func(max(self, key=_func))
|
|
||||||
|
|
||||||
def median(self, _func=None) -> Union[int, float]:
|
|
||||||
r"""Return the median value of data elements
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
Union[int, float]
|
|
||||||
"""
|
|
||||||
if _func is None:
|
|
||||||
_func = default_lambda
|
|
||||||
|
|
||||||
result = self.order_by(_func).select(_func).to_list()
|
|
||||||
length = len(result)
|
|
||||||
i = int(length / 2)
|
|
||||||
return result[i] if length % 2 == 1 else (float(result[i - 1]) + float(result[i])) / float(2)
|
|
||||||
|
|
||||||
def min(self, _func: Callable = None) -> object:
|
|
||||||
r"""Returns the lowest value
|
|
||||||
|
|
||||||
Parameter
|
|
||||||
---------
|
|
||||||
func: :class:`Callable`
|
|
||||||
selected value
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
object
|
|
||||||
"""
|
|
||||||
if _func is None and not is_number(self.type):
|
|
||||||
raise InvalidTypeException()
|
|
||||||
|
|
||||||
if _func is None:
|
|
||||||
_func = default_lambda
|
|
||||||
|
|
||||||
return _func(min(self, key=_func))
|
|
||||||
|
|
||||||
def order_by(self, _func: Callable = None) -> "OrderedQueryableABC":
|
|
||||||
r"""Sorts elements by function in ascending order
|
|
||||||
|
|
||||||
Parameter
|
|
||||||
---------
|
|
||||||
func: :class:`Callable`
|
|
||||||
selected value
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
:class: `cpl.query.base.ordered_queryable_abc.OrderedQueryableABC`
|
|
||||||
"""
|
|
||||||
if _func is None:
|
|
||||||
_func = default_lambda
|
|
||||||
|
|
||||||
from cpl.query.base.ordered_queryable import OrderedQueryable
|
|
||||||
|
|
||||||
return OrderedQueryable(self.type, sorted(self, key=_func), _func)
|
|
||||||
|
|
||||||
def order_by_descending(self, _func: Callable = None) -> "OrderedQueryableABC":
|
|
||||||
r"""Sorts elements by function in descending order
|
|
||||||
|
|
||||||
Parameter
|
|
||||||
---------
|
|
||||||
func: :class:`Callable`
|
|
||||||
selected value
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
:class: `cpl.query.base.ordered_queryable_abc.OrderedQueryableABC`
|
|
||||||
"""
|
|
||||||
if _func is None:
|
|
||||||
_func = default_lambda
|
|
||||||
|
|
||||||
from cpl.query.base.ordered_queryable import OrderedQueryable
|
|
||||||
|
|
||||||
return OrderedQueryable(self.type, sorted(self, key=_func, reverse=True), _func)
|
|
||||||
|
|
||||||
def reverse(self) -> QueryableABC:
|
|
||||||
r"""Reverses list
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
:class: `cpl.query.base.queryable_abc.QueryableABC`
|
|
||||||
"""
|
|
||||||
return type(self)(self._type, reversed(self._values))
|
|
||||||
|
|
||||||
def select(self, _func: Callable) -> QueryableABC:
|
|
||||||
r"""Formats each element of list to a given format
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
:class: `cpl.query.base.queryable_abc.QueryableABC`
|
|
||||||
"""
|
|
||||||
if _func is None:
|
|
||||||
_func = default_lambda
|
|
||||||
|
|
||||||
_l = [_func(_o) for _o in self]
|
|
||||||
_t = type(_l[0]) if len(_l) > 0 else Any
|
|
||||||
|
|
||||||
return type(self)(_t, _l)
|
|
||||||
|
|
||||||
def select_many(self, _func: Callable) -> QueryableABC:
|
|
||||||
r"""Flattens resulting lists to one
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
:class: `cpl.query.base.queryable_abc.QueryableABC`
|
|
||||||
"""
|
|
||||||
# The line below is pain. I don't understand anything of it...
|
|
||||||
# written on 09.11.2022 by Sven Heidemann
|
|
||||||
return type(self)(object, [_a for _o in self for _a in _func(_o)])
|
|
||||||
|
|
||||||
def single(self) -> any:
|
|
||||||
r"""Returns one single element of list
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
Found value: any
|
|
||||||
|
|
||||||
Raises
|
|
||||||
------
|
|
||||||
ArgumentNoneException: when argument is None
|
|
||||||
Exception: when argument is None or found more than one element
|
|
||||||
"""
|
|
||||||
if self.count() > 1:
|
|
||||||
raise Exception("Found more than one element")
|
|
||||||
elif self.count() == 0:
|
|
||||||
raise Exception("Found no element")
|
|
||||||
|
|
||||||
return self._values[0]
|
|
||||||
|
|
||||||
def single_or_default(self) -> Optional[any]:
|
|
||||||
r"""Returns one single element of list
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
Found value: Optional[any]
|
|
||||||
"""
|
|
||||||
if self.count() > 1:
|
|
||||||
raise Exception("Index out of range")
|
|
||||||
elif self.count() == 0:
|
|
||||||
return None
|
|
||||||
|
|
||||||
return self._values[0]
|
|
||||||
|
|
||||||
def skip(self, _index: int) -> QueryableABC:
|
|
||||||
r"""Skips all elements from index
|
|
||||||
|
|
||||||
Parameter
|
|
||||||
---------
|
|
||||||
_index: :class:`int`
|
|
||||||
index
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
:class: `cpl.query.base.queryable_abc.QueryableABC`
|
|
||||||
"""
|
|
||||||
if _index is None:
|
|
||||||
raise ArgumentNoneException(ExceptionArgument.index)
|
|
||||||
|
|
||||||
return type(self)(self.type, self._values[_index:])
|
|
||||||
|
|
||||||
def skip_last(self, _index: int) -> QueryableABC:
|
|
||||||
r"""Skips all elements after index
|
|
||||||
|
|
||||||
Parameter
|
|
||||||
---------
|
|
||||||
_index: :class:`int`
|
|
||||||
index
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
:class: `cpl.query.base.queryable_abc.QueryableABC`
|
|
||||||
"""
|
|
||||||
if _index is None:
|
|
||||||
raise ArgumentNoneException(ExceptionArgument.index)
|
|
||||||
|
|
||||||
index = self.count() - _index
|
|
||||||
return type(self)(self._type, self._values[:index])
|
|
||||||
|
|
||||||
def sum(self, _func: Callable = None) -> Union[int, float, complex]:
|
|
||||||
r"""Sum of all values
|
|
||||||
|
|
||||||
Parameter
|
|
||||||
---------
|
|
||||||
func: :class:`Callable`
|
|
||||||
selected value
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
Union[int, float, complex]
|
|
||||||
"""
|
|
||||||
if _func is None and not is_number(self.type):
|
|
||||||
raise InvalidTypeException()
|
|
||||||
|
|
||||||
if _func is None:
|
|
||||||
_func = default_lambda
|
|
||||||
|
|
||||||
result = 0
|
|
||||||
for x in self:
|
|
||||||
result += _func(x)
|
|
||||||
|
|
||||||
return result
|
|
||||||
|
|
||||||
def split(self, _func: Callable) -> QueryableABC:
|
|
||||||
r"""Splits the list by given function
|
|
||||||
|
|
||||||
|
|
||||||
Parameter
|
|
||||||
---------
|
|
||||||
func: :class:`Callable`
|
|
||||||
seperator
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
:class: `cpl.query.base.queryable_abc.QueryableABC`
|
|
||||||
"""
|
|
||||||
groups = []
|
|
||||||
group = []
|
|
||||||
for x in self:
|
|
||||||
v = _func(x)
|
|
||||||
if x == v:
|
|
||||||
groups.append(group)
|
|
||||||
group = []
|
|
||||||
|
|
||||||
group.append(x)
|
|
||||||
|
|
||||||
groups.append(group)
|
|
||||||
|
|
||||||
query_groups = []
|
|
||||||
for g in groups:
|
|
||||||
if len(g) == 0:
|
|
||||||
continue
|
|
||||||
query_groups.append(type(self)(self._type, g))
|
|
||||||
|
|
||||||
return type(self)(self._type, query_groups)
|
|
||||||
|
|
||||||
def take(self, _index: int) -> QueryableABC:
|
|
||||||
r"""Takes all elements from index
|
|
||||||
|
|
||||||
Parameter
|
|
||||||
---------
|
|
||||||
_index: :class:`int`
|
|
||||||
index
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
:class: `cpl.query.base.queryable_abc.QueryableABC`
|
|
||||||
"""
|
|
||||||
if _index is None:
|
|
||||||
raise ArgumentNoneException(ExceptionArgument.index)
|
|
||||||
|
|
||||||
return type(self)(self._type, self._values[:_index])
|
|
||||||
|
|
||||||
def take_last(self, _index: int) -> QueryableABC:
|
|
||||||
r"""Takes all elements after index
|
|
||||||
|
|
||||||
Parameter
|
|
||||||
---------
|
|
||||||
_index: :class:`int`
|
|
||||||
index
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
:class: `cpl.query.base.queryable_abc.QueryableABC`
|
|
||||||
"""
|
|
||||||
index = self.count() - _index
|
|
||||||
|
|
||||||
if index >= self.count() or index < 0:
|
|
||||||
raise IndexOutOfRangeException()
|
|
||||||
|
|
||||||
return type(self)(self._type, self._values[index:])
|
|
||||||
|
|
||||||
def where(self, _func: Callable = None) -> QueryableABC:
|
|
||||||
r"""Select element by function
|
|
||||||
|
|
||||||
Parameter
|
|
||||||
---------
|
|
||||||
func: :class:`Callable`
|
|
||||||
selected value
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
:class: `cpl.query.base.queryable_abc.QueryableABC`
|
|
||||||
"""
|
|
||||||
if _func is None:
|
|
||||||
raise ArgumentNoneException(ExceptionArgument.func)
|
|
||||||
|
|
||||||
if _func is None:
|
|
||||||
_func = default_lambda
|
|
||||||
|
|
||||||
return type(self)(self.type, filter(_func, self))
|
|
||||||
@@ -1,96 +0,0 @@
|
|||||||
from abc import abstractmethod, ABC
|
|
||||||
from typing import Iterable
|
|
||||||
|
|
||||||
|
|
||||||
class Sequence(ABC):
|
|
||||||
@abstractmethod
|
|
||||||
def __init__(self, t: type, values: Iterable = None):
|
|
||||||
assert t is not None
|
|
||||||
assert isinstance(t, type) or t == any
|
|
||||||
assert values is None or isinstance(values, Iterable)
|
|
||||||
|
|
||||||
if values is None:
|
|
||||||
values = []
|
|
||||||
|
|
||||||
self._values = list(values)
|
|
||||||
|
|
||||||
if t is None:
|
|
||||||
t = object
|
|
||||||
|
|
||||||
self._type = t
|
|
||||||
|
|
||||||
def __iter__(self):
|
|
||||||
return iter(self._values)
|
|
||||||
|
|
||||||
def __next__(self):
|
|
||||||
return next(iter(self._values))
|
|
||||||
|
|
||||||
def __len__(self):
|
|
||||||
return self.to_list().__len__()
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def __class_getitem__(cls, _t: type) -> type:
|
|
||||||
return _t
|
|
||||||
|
|
||||||
def __repr__(self):
|
|
||||||
return f"<{type(self).__name__} {self.to_list().__repr__()}>"
|
|
||||||
|
|
||||||
@property
|
|
||||||
def type(self) -> type:
|
|
||||||
return self._type
|
|
||||||
|
|
||||||
def _check_type(self, __object: any):
|
|
||||||
if self._type == any:
|
|
||||||
return
|
|
||||||
|
|
||||||
if (
|
|
||||||
self._type is not None
|
|
||||||
and type(__object) != self._type
|
|
||||||
and not isinstance(type(__object), self._type)
|
|
||||||
and not issubclass(type(__object), self._type)
|
|
||||||
):
|
|
||||||
raise Exception(f"Unexpected type: {type(__object)}\nExpected type: {self._type}")
|
|
||||||
|
|
||||||
def to_list(self) -> list:
|
|
||||||
r"""Converts :class: `cpl.query.base.sequence_abc.SequenceABC` to :class: `list`
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
:class: `list`
|
|
||||||
"""
|
|
||||||
return [x for x in self._values]
|
|
||||||
|
|
||||||
def copy(self) -> "Sequence":
|
|
||||||
r"""Creates a copy of sequence
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Sequence
|
|
||||||
"""
|
|
||||||
return type(self)(self._type, self.to_list())
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def empty(cls) -> "Sequence":
|
|
||||||
r"""Returns an empty sequence
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Sequence object that contains no elements
|
|
||||||
"""
|
|
||||||
return cls(object, [])
|
|
||||||
|
|
||||||
def index_of(self, _object: object) -> int:
|
|
||||||
r"""Returns the index of given element
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Index of object
|
|
||||||
|
|
||||||
Raises:
|
|
||||||
IndexError if object not in sequence
|
|
||||||
"""
|
|
||||||
for i, o in enumerate(self):
|
|
||||||
if o == _object:
|
|
||||||
return i
|
|
||||||
|
|
||||||
raise IndexError
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def range(cls, start: int, length: int) -> "Sequence":
|
|
||||||
return cls(int, range(start, length))
|
|
||||||
213
src/cpl-query/cpl/query/enumerable.py
Normal file
213
src/cpl-query/cpl/query/enumerable.py
Normal file
@@ -0,0 +1,213 @@
|
|||||||
|
from itertools import islice, groupby, chain
|
||||||
|
from typing import Generic, Callable, Iterable, Iterator, Dict, Tuple, Optional
|
||||||
|
|
||||||
|
from cpl.core.typing import T, R
|
||||||
|
from cpl.query.typing import Predicate, K, Selector
|
||||||
|
|
||||||
|
|
||||||
|
class Enumerable(Generic[T]):
|
||||||
|
def __init__(self, source: Iterable[T]):
|
||||||
|
self._source = source
|
||||||
|
|
||||||
|
def __iter__(self) -> Iterator[T]:
|
||||||
|
return iter(self._source)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def length(self) -> int:
|
||||||
|
return len(list(self._source))
|
||||||
|
|
||||||
|
def where(self, f: Predicate) -> "Enumerable[T]":
|
||||||
|
return Enumerable(x for x in self._source if f(x))
|
||||||
|
|
||||||
|
def select(self, f: Selector) -> "Enumerable[R]":
|
||||||
|
return Enumerable(f(x) for x in self._source)
|
||||||
|
|
||||||
|
def select_many(self, f: Callable[[T], Iterable[R]]) -> "Enumerable[R]":
|
||||||
|
return Enumerable(y for x in self._source for y in f(x))
|
||||||
|
|
||||||
|
def take(self, count: int) -> "Enumerable[T]":
|
||||||
|
return Enumerable(islice(self._source, count))
|
||||||
|
|
||||||
|
def skip(self, count: int) -> "Enumerable[T]":
|
||||||
|
return Enumerable(islice(self._source, count, None))
|
||||||
|
|
||||||
|
def take_while(self, f: Predicate) -> "Enumerable[T]":
|
||||||
|
def generator():
|
||||||
|
for x in self._source:
|
||||||
|
if f(x):
|
||||||
|
yield x
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
|
||||||
|
return Enumerable(generator())
|
||||||
|
|
||||||
|
def skip_while(self, f: Predicate) -> "Enumerable[T]":
|
||||||
|
def generator():
|
||||||
|
it = iter(self._source)
|
||||||
|
for x in it:
|
||||||
|
if not f(x):
|
||||||
|
yield x
|
||||||
|
break
|
||||||
|
yield from it
|
||||||
|
|
||||||
|
return Enumerable(generator())
|
||||||
|
|
||||||
|
def distinct(self) -> "Enumerable[T]":
|
||||||
|
def generator():
|
||||||
|
seen = set()
|
||||||
|
for x in self._source:
|
||||||
|
if x not in seen:
|
||||||
|
seen.add(x)
|
||||||
|
yield x
|
||||||
|
|
||||||
|
return Enumerable(generator())
|
||||||
|
|
||||||
|
def union(self, other: Iterable[T]) -> "Enumerable[T]":
|
||||||
|
return Enumerable(chain(self.distinct(), Enumerable(other).distinct())).distinct()
|
||||||
|
|
||||||
|
def intersect(self, other: Iterable[T]) -> "Enumerable[T]":
|
||||||
|
other_set = set(other)
|
||||||
|
return Enumerable(x for x in self._source if x in other_set)
|
||||||
|
|
||||||
|
def except_(self, other: Iterable[T]) -> "Enumerable[T]":
|
||||||
|
other_set = set(other)
|
||||||
|
return Enumerable(x for x in self._source if x not in other_set)
|
||||||
|
|
||||||
|
def concat(self, other: Iterable[T]) -> "Enumerable[T]":
|
||||||
|
return Enumerable(chain(self._source, other))
|
||||||
|
|
||||||
|
# --- Aggregation ---
|
||||||
|
def count(self) -> int:
|
||||||
|
return sum(1 for _ in self._source)
|
||||||
|
|
||||||
|
def sum(self, f: Optional[Selector] = None) -> R:
|
||||||
|
if f:
|
||||||
|
return sum(f(x) for x in self._source)
|
||||||
|
return sum(self._source) # type: ignore
|
||||||
|
|
||||||
|
def min(self, f: Optional[Selector] = None) -> R:
|
||||||
|
if f:
|
||||||
|
return min(f(x) for x in self._source)
|
||||||
|
return min(self._source) # type: ignore
|
||||||
|
|
||||||
|
def max(self, f: Optional[Selector] = None) -> R:
|
||||||
|
if f:
|
||||||
|
return max(f(x) for x in self._source)
|
||||||
|
return max(self._source) # type: ignore
|
||||||
|
|
||||||
|
def average(self, f: Optional[Callable[[T], float]] = None) -> float:
|
||||||
|
values = list(self.select(f).to_list()) if f else list(self._source)
|
||||||
|
return sum(values) / len(values) if values else 0.0
|
||||||
|
|
||||||
|
def aggregate(self, func: Callable[[R, T], R], seed: Optional[R] = None) -> R:
|
||||||
|
it = iter(self._source)
|
||||||
|
if seed is None:
|
||||||
|
acc = next(it) # type: ignore
|
||||||
|
else:
|
||||||
|
acc = seed
|
||||||
|
for x in it:
|
||||||
|
acc = func(acc, x)
|
||||||
|
return acc
|
||||||
|
|
||||||
|
def any(self, f: Optional[Predicate] = None) -> bool:
|
||||||
|
return any(f(x) if f else x for x in self._source)
|
||||||
|
|
||||||
|
def all(self, f: Predicate) -> bool:
|
||||||
|
return all(f(x) for x in self._source)
|
||||||
|
|
||||||
|
def contains(self, value: T) -> bool:
|
||||||
|
return any(x == value for x in self._source)
|
||||||
|
|
||||||
|
def sequence_equal(self, other: Iterable[T]) -> bool:
|
||||||
|
return list(self._source) == list(other)
|
||||||
|
|
||||||
|
def group_by(self, key_f: Callable[[T], K]) -> "Enumerable[Tuple[K, List[T]]]":
|
||||||
|
def generator():
|
||||||
|
sorted_data = sorted(self._source, key=key_f)
|
||||||
|
for key, group in groupby(sorted_data, key=key_f):
|
||||||
|
yield (key, list(group))
|
||||||
|
|
||||||
|
return Enumerable(generator())
|
||||||
|
|
||||||
|
def join(
|
||||||
|
self, inner: Iterable[R], outer_key: Callable[[T], K], inner_key: Callable[[R], K], result: Callable[[T, R], R]
|
||||||
|
) -> "Enumerable[R]":
|
||||||
|
def generator():
|
||||||
|
lookup: Dict[K, List[R]] = {}
|
||||||
|
for i in inner:
|
||||||
|
k = inner_key(i)
|
||||||
|
lookup.setdefault(k, []).append(i)
|
||||||
|
for o in self._source:
|
||||||
|
k = outer_key(o)
|
||||||
|
if k in lookup:
|
||||||
|
for i in lookup[k]:
|
||||||
|
yield result(o, i)
|
||||||
|
|
||||||
|
return Enumerable(generator())
|
||||||
|
|
||||||
|
def first(self, f: Optional[Predicate] = None) -> T:
|
||||||
|
if f:
|
||||||
|
for x in self._source:
|
||||||
|
if f(x):
|
||||||
|
return x
|
||||||
|
raise ValueError("No matching element")
|
||||||
|
return next(iter(self._source))
|
||||||
|
|
||||||
|
def first_or_default(self, default: Optional[T] = None) -> Optional[T]:
|
||||||
|
return next(iter(self._source), default)
|
||||||
|
|
||||||
|
def last(self) -> T:
|
||||||
|
return list(self._source)[-1]
|
||||||
|
|
||||||
|
def single(self, f: Optional[Predicate] = None) -> T:
|
||||||
|
items = [x for x in self._source if f(x)] if f else list(self._source)
|
||||||
|
if len(items) != 1:
|
||||||
|
raise ValueError("Sequence does not contain exactly one element")
|
||||||
|
return items[0]
|
||||||
|
|
||||||
|
def to_list(self) -> "List[T]":
|
||||||
|
from cpl.query.list import List
|
||||||
|
|
||||||
|
return List(self)
|
||||||
|
|
||||||
|
def to_set(self) -> "Set[T]":
|
||||||
|
from cpl.query.set import Set
|
||||||
|
|
||||||
|
return Set(self)
|
||||||
|
|
||||||
|
def to_dict(self, key_f: Callable[[T], K], value_f: Selector) -> Dict[K, R]:
|
||||||
|
return {key_f(x): value_f(x) for x in self._source}
|
||||||
|
|
||||||
|
def cast(self, t: Selector) -> "Enumerable[R]":
|
||||||
|
return Enumerable(t(x) for x in self._source)
|
||||||
|
|
||||||
|
def of_type(self, t: type) -> "Enumerable[T]":
|
||||||
|
return Enumerable(x for x in self._source if isinstance(x, t))
|
||||||
|
|
||||||
|
def reverse(self) -> "Enumerable[T]":
|
||||||
|
return Enumerable(reversed(list(self._source)))
|
||||||
|
|
||||||
|
def zip(self, other: Iterable[R]) -> "Enumerable[Tuple[T, R]]":
|
||||||
|
return Enumerable(zip(self._source, other))
|
||||||
|
|
||||||
|
def order_by(self, key_selector: Callable[[T], K]) -> "OrderedEnumerable[T]":
|
||||||
|
from cpl.query.ordered_enumerable import OrderedEnumerable
|
||||||
|
|
||||||
|
return OrderedEnumerable(self._source, [(key_selector, False)])
|
||||||
|
|
||||||
|
def order_by_descending(self, key_selector: Callable[[T], K]) -> "OrderedEnumerable[T]":
|
||||||
|
from cpl.query.ordered_enumerable import OrderedEnumerable
|
||||||
|
|
||||||
|
return OrderedEnumerable(self._source, [(key_selector, True)])
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def range(start: int, count: int) -> "Enumerable[int]":
|
||||||
|
return Enumerable(range(start, start + count))
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def repeat(value: T, count: int) -> "Enumerable[T]":
|
||||||
|
return Enumerable(value for _ in range(count))
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def empty() -> "Enumerable[T]":
|
||||||
|
return Enumerable([])
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
from .enumerable import Enumerable
|
|
||||||
from .enumerable_abc import EnumerableABC
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
from cpl.query.enumerable.enumerable_abc import EnumerableABC
|
|
||||||
|
|
||||||
|
|
||||||
def _default_lambda(x: object):
|
|
||||||
return x
|
|
||||||
|
|
||||||
|
|
||||||
class Enumerable(EnumerableABC):
|
|
||||||
r"""Implementation of :class: `cpl.query.enumerable.enumerable_abc.EnumerableABC`"""
|
|
||||||
|
|
||||||
def __init__(self, t: type = None, values: list = None):
|
|
||||||
EnumerableABC.__init__(self, t, values)
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
from abc import abstractmethod
|
|
||||||
|
|
||||||
from cpl.query.base.queryable_abc import QueryableABC
|
|
||||||
|
|
||||||
|
|
||||||
class EnumerableABC(QueryableABC):
|
|
||||||
r"""ABC to define functions on list"""
|
|
||||||
|
|
||||||
@abstractmethod
|
|
||||||
def __init__(self, t: type = None, values: list = None):
|
|
||||||
QueryableABC.__init__(self, t, values)
|
|
||||||
|
|
||||||
def to_iterable(self) -> "IterableABC":
|
|
||||||
r"""Converts :class: `cpl.query.enumerable.enumerable_abc.EnumerableABC` to :class: `cpl.query.iterable.iterable_abc.IterableABC`
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
:class: `cpl.query.iterable.iterable_abc.IterableABC`
|
|
||||||
"""
|
|
||||||
from cpl.query.iterable.iterable import Iterable
|
|
||||||
|
|
||||||
return Iterable(self._type, self.to_list())
|
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
from enum import Enum
|
|
||||||
|
|
||||||
|
|
||||||
# models
|
|
||||||
class ExceptionArgument(Enum):
|
|
||||||
list = "list"
|
|
||||||
func = "func"
|
|
||||||
type = "type"
|
|
||||||
value = "value"
|
|
||||||
index = "index"
|
|
||||||
|
|
||||||
|
|
||||||
# exceptions
|
|
||||||
class ArgumentNoneException(Exception):
|
|
||||||
r"""Exception when argument is None"""
|
|
||||||
|
|
||||||
def __init__(self, arg: ExceptionArgument):
|
|
||||||
Exception.__init__(self, f"argument {arg} is None")
|
|
||||||
|
|
||||||
|
|
||||||
class IndexOutOfRangeException(Exception):
|
|
||||||
r"""Exception when index is out of range"""
|
|
||||||
|
|
||||||
def __init__(self, err: str = None):
|
|
||||||
Exception.__init__(self, f"List index out of range" if err is None else err)
|
|
||||||
|
|
||||||
|
|
||||||
class InvalidTypeException(Exception):
|
|
||||||
r"""Exception when type is invalid"""
|
|
||||||
|
|
||||||
|
|
||||||
class WrongTypeException(Exception):
|
|
||||||
r"""Exception when type is unexpected"""
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
from .list import List
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
from cpl.query.enumerable.enumerable_abc import EnumerableABC
|
|
||||||
from cpl.query.iterable.iterable import Iterable
|
|
||||||
|
|
||||||
|
|
||||||
class List(Iterable):
|
|
||||||
r"""Implementation of :class: `cpl.query.extension.iterable.Iterable`"""
|
|
||||||
|
|
||||||
def __init__(self, t: type = None, values: Iterable = None):
|
|
||||||
Iterable.__init__(self, t, values)
|
|
||||||
|
|
||||||
def __getitem__(self, *args):
|
|
||||||
return self._values.__getitem__(*args)
|
|
||||||
|
|
||||||
def __setitem__(self, *args):
|
|
||||||
self._values.__setitem__(*args)
|
|
||||||
|
|
||||||
def __delitem__(self, *args):
|
|
||||||
self._values.__delitem__(*args)
|
|
||||||
|
|
||||||
def to_enumerable(self) -> EnumerableABC:
|
|
||||||
r"""Converts :class: `cpl.query.iterable.iterable_abc.IterableABC` to :class: `cpl.query.enumerable.enumerable_abc.EnumerableABC`
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
:class: `cpl.query.enumerable.enumerable_abc.EnumerableABC`
|
|
||||||
"""
|
|
||||||
from cpl.query.enumerable.enumerable import Enumerable
|
|
||||||
|
|
||||||
return Enumerable(self._type, self.to_list())
|
|
||||||
|
|
||||||
def to_iterable(self) -> Iterable:
|
|
||||||
r"""Converts :class: `cpl.query.enumerable.enumerable_abc.EnumerableABC` to :class: `cpl.query.iterable.iterable_abc.IterableABC`
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
:class: `cpl.query.iterable.iterable_abc.IterableABC`
|
|
||||||
"""
|
|
||||||
return Iterable(self._type, self.to_list())
|
|
||||||
65
src/cpl-query/cpl/query/immutable_list.py
Normal file
65
src/cpl-query/cpl/query/immutable_list.py
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
from typing import Generic, Iterable, Iterator, Optional
|
||||||
|
|
||||||
|
from cpl.core.typing import T
|
||||||
|
from cpl.query.enumerable import Enumerable
|
||||||
|
|
||||||
|
|
||||||
|
class ImmutableList(Generic[T], Enumerable[T]):
|
||||||
|
def __init__(self, source: Optional[Iterable[T]] = None):
|
||||||
|
Enumerable.__init__(self, [])
|
||||||
|
if source is None:
|
||||||
|
source = []
|
||||||
|
elif not isinstance(source, list):
|
||||||
|
source = list(source)
|
||||||
|
|
||||||
|
self.__source = source
|
||||||
|
|
||||||
|
@property
|
||||||
|
def _source(self) -> list[T]:
|
||||||
|
return self.__source
|
||||||
|
|
||||||
|
@_source.setter
|
||||||
|
def _source(self, value: list[T]) -> None:
|
||||||
|
self.__source = value
|
||||||
|
|
||||||
|
def __iter__(self) -> Iterator[T]:
|
||||||
|
return iter(self._source)
|
||||||
|
|
||||||
|
def __len__(self) -> int:
|
||||||
|
return len(self._source)
|
||||||
|
|
||||||
|
def __getitem__(self, index: int) -> T:
|
||||||
|
return self._source[index]
|
||||||
|
|
||||||
|
def __contains__(self, item: T) -> bool:
|
||||||
|
return item in self._source
|
||||||
|
|
||||||
|
def __repr__(self) -> str:
|
||||||
|
return f"List({self._source!r})"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def length(self) -> int:
|
||||||
|
return len(self._source)
|
||||||
|
|
||||||
|
def add(self, item: T) -> None:
|
||||||
|
self._source.append(item)
|
||||||
|
|
||||||
|
def extend(self, items: Iterable[T]) -> None:
|
||||||
|
self._source.extend(items)
|
||||||
|
|
||||||
|
def insert(self, index: int, item: T) -> None:
|
||||||
|
self._source.insert(index, item)
|
||||||
|
|
||||||
|
def remove(self, item: T) -> None:
|
||||||
|
self._source.remove(item)
|
||||||
|
|
||||||
|
def pop(self, index: int = -1) -> T:
|
||||||
|
return self._source.pop(index)
|
||||||
|
|
||||||
|
def clear(self) -> None:
|
||||||
|
self._source.clear()
|
||||||
|
|
||||||
|
def to_enumerable(self) -> "Enumerable[T]":
|
||||||
|
from cpl.query.enumerable import Enumerable
|
||||||
|
|
||||||
|
return Enumerable(self._source)
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user