monkey_wrench.query._list module
- class monkey_wrench.query._list.List(items: list, datetime_parser: SeviriIDParser | FilePathParser, log_context: str = 'List')[source]
Bases:
QueryA class to provide generic functionalities to query lists.
Note
This class is meant to behave as an immutable list.
Note
This class utilizes
numpy.ndarrayobjects under the hood.- __init__(items: list, datetime_parser: SeviriIDParser | FilePathParser, log_context: str = 'List') None[source]
Make an instance of the class.
- Parameters:
items – The complete list of items to query.
datetime_parser – A class of type
DateTimeParserto enable parsing items into datetime objects.log_context – A string that will be used in log messages to determine the context. Defaults to an empty string.
- property parsed_items: ndarray
- query(datetime_period: DateTimePeriod) Self[source]
Query items from the
Listobject, given a start datetime and an end datetime.- Parameters:
datetime_period – The datetime period to query the items from.
- Returns:
A new
Listobject including items that match the given query.- Raises:
ValueError – Refer to
assert_start_time_is_before_end_time().
- query_indices(datetime_period: DateTimePeriod) list[int][source]
Similar to
query(), but returns the indices of items as a Python built-in list.
- __get_indices(datetime_period: DateTimePeriod) array
Similar to
query_indices(), but returns the numpy indices instead.
- normalize_index(index: int) int[source]
Convert a negative index into its positive equivalent, or return the original index if it is non-negative.
- Raises:
IndexError – If the positive index or its positive equivalent exceeds the size of the
Listobject.
- generate_k_sized_batches_by_index(k: Annotated[int, Gt(gt=0)], index_start: int = 0, index_end: int = -1, batches_as_python_lists: bool = True) Generator[source]
Generate batches (sub-lists) of size
kand move forward by1index each time.A batch consists of the item at the current index, as well as
k-1preceding items. In other words, a batch includeskadjacent items, with the item at the current index being the last item of the batch. Next batch is retrieved by incrementing the current index by+1. As a result, two consecutive batches havek-2common objects.Note
Both
index_startandindex_endare considered as inclusive. They can be negative as well.Note
The indices are zero-based. If
index_startis less than or equal tok-1, the first batch includes items from index0to indexk-1(inclusive). The next batch includes indices[1, k].- Parameters:
k – The size of the batches. Each batch includes the current item as well as
k-1preceding items.index_start – The zero-based index of the first item to start generating the batches from. Defaults to
0and can be negative as well.index_end – The zero-based index of the last item (inclusive) up to which the batches are generated. Defaults to
-1meaning the last item of the list makes the last item of the final batch.batches_as_python_lists – A boolean determining whether to return each batch as a Python built-in list or as
Listobjects. Defaults toTrue.
- Yields:
A generator that yields batches of size
k. Adjacent batches overlap byk-2items.- Raises:
ValueError – If
index_startis greater thanindex_end.ValueError – If
kexceeds the size of the list.IndexError – If normalized indices exceed the size of the List object. Refer to
normalize_index().
- partition_in_k_sized_batches_by_index(k: Annotated[int, Gt(gt=0)], index_start: int = 0, index_end: int = -1, batches_as_python_lists: bool = True) Generator[source]
Partition the list, where the batches are of size
kor less.Note
The partition is given for all items that are in
[index_start, index_end](both inclusive).Note
This is similar to
generate_k_sized_batches_by_index(), but there are differences. First, this method generates partitions, i.e. sub-lists do not have any common items. Second, there could be one sub-list whose size is less thank. This happens when the length of available items to partition is less thank.