graviti.manager.lazy
#
Related classes for the lazy evaluation.
Module Contents#
Classes#
In paging lazy evaluation system, a LazyItem instance represents an element in a pagination. |
|
ReturnGenerator is a generator wrap to get the return value easily. |
|
In paging lazy evaluation system, a LazyPage instance represents a page with elements. |
|
In paging lazy evaluation system, InitPage is the page to initialize |
|
LazyPagingList is a wrap of web paging request. |
Attributes#
- class graviti.manager.lazy.LazyItem(page, data)[source]#
Bases:
Generic
[_T
]In paging lazy evaluation system, a LazyItem instance represents an element in a pagination.
If user wants to access the elememt, LazyItem will trigger the paging request to pull a page of elements and return the required element. All the pulled elements will be stored in different LazyItem instances and will not be requested again.
- Parameters
page (LazyPage[_T]) – The page the item belongs to.
data (_T) –
- data#
The actual element stored in this item.
- classmethod from_data(cls, data)[source]#
Create a LazyItem instance from data.
- Parameters
data (_T) – The actual data needs to be stored in LazyItem.
- Returns
The LazyItem instance which stores the input data.
- Return type
LazyItem[_T]
- get(self)[source]#
Access the actual element represented by LazyItem.
If the element is already pulled from web, it will be return directly, otherwise this function will request for a page of elements to get the required elememt.
- Returns
The actual element this LazyItem instance represents.
- Return type
_T
- class graviti.manager.lazy.ReturnGenerator(generator)[source]#
Bases:
Generic
[_T
,_R
]ReturnGenerator is a generator wrap to get the return value easily.
- Parameters
generator (Generator[_T, Any, _R]) – The generator needs to be wrapped.
- class graviti.manager.lazy.LazyPage(offset, limit, func)[source]#
Bases:
Generic
[_T
]In paging lazy evaluation system, a LazyPage instance represents a page with elements.
LazyPage is used for sending paging request to pull a page of elements and storing them in different
LazyItem
instances.- Parameters
offset (int) – The offset of the page.
limit (int) – The limit of the page.
func (PagingGenerator[_T]) – A paging generator function, which takes offset<int> and limit<int> as inputs and returns a generator. The returned generator should yield the element user needs, and return the total count of the elements in the paging request.
- classmethod from_items(cls, offset, limit, func, item_contents)[source]#
Create a LazyPage instance with the given items and generator function.
- Parameters
offset (int) – The offset of the page.
limit (int) – The limit of the page.
func (PagingGenerator[_T]) – A paging generator function, which takes offset<int> and limit<int> as inputs and returns a generator. The returned generator should yield the element user needs, and return the total count of the elements in the paging request.
item_contents (Iterable[_T]) – The lazy item contents that need to be stored on this page.
- Returns
The LazyPage instance which stores the input items and function.
- Return type
LazyPage[_T]
- class graviti.manager.lazy.InitPage(offset, limit, func)[source]#
Bases:
LazyPage
[_T
]In paging lazy evaluation system, InitPage is the page to initialize
LazyPagingList
.InitPage will send a paging request to pull a page of elements and storing them in different
LazyItem
instances when construction. And the totalCount of the page will also be stored in the instance.- Parameters
offset (int) – The offset of the page.
limit (int) – The limit of the page.
func (PagingGenerator[_T]) – A paging generator function, which takes offset<int> and limit<int> as inputs and returns a generator. The returned generator should yield the element user needs, and return the total count of the elements in the paging request.
- total_count#
The totalCount of the paging request.
- class graviti.manager.lazy.LazyPagingList(func, limit)[source]#
Bases:
MutableSequence
[_T
],graviti.utility.ReprMixin
LazyPagingList is a wrap of web paging request.
It follows the python MutableSequence protocal, which means it can be used like a python builtin list. And it provides features like lazy evaluation and cache.
- Parameters
func (PagingGenerator[_T]) – A paging generator function, which takes offset<int> and limit<int> as inputs and returns a generator. The returned generator should yield the element user needs, and return the total count of the elements in the paging request.
limit (int) – The page size of each paging request.
- insert(self, index, value)[source]#
Insert object before index.
- Parameters
index (int) – Position of the LazyPagingList.
value (_T) – Element to be inserted into the LazyPagingList.
- Return type
None
- append(self, value)[source]#
Append object to the end of the LazyPagingList.
- Parameters
value (_T) – Element to be appended to the LazyPagingList.
- Return type
None
- pop(self, index=- 1)[source]#
Return the item at index (default last) and remove it from the LazyPagingList.
- Parameters
index (int) – Position of the LazyPagingList.
- Returns
Element to be removed from the LazyPagingList.
- Return type
_T
- index(self, value, start=0, stop=None)[source]#
Return the first index of the value.
- Parameters
value (Any) – The value to be found.
start (int) – The start index of the subsequence.
stop (Optional[int]) – The end index of the subsequence.
- Raises
ValueError – When the value is not in the LazyPagingList
- Returns
The first index of the value.
- Return type
int