graviti.manager.lazy#

Related classes for the lazy evaluation.

Module Contents#

Classes#

LazyItem

In paging lazy evaluation system, a LazyItem instance represents an element in a pagination.

ReturnGenerator

ReturnGenerator is a generator wrap to get the return value easily.

LazyPage

In paging lazy evaluation system, a LazyPage instance represents a page with elements.

InitPage

In paging lazy evaluation system, InitPage is the page to initialize LazyPagingList.

LazyPagingList

LazyPagingList is a wrap of web paging request.

Attributes#

graviti.manager.lazy.PagingGenerator[source]#
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) –

page#

The parent LazyPage of this item.

data#

The actual element stored in this item.

classmethod from_page(cls, page)[source]#

Create a LazyItem instance from page.

Parameters

page (LazyPage[_T]) – The page of the element.

Returns

The LazyItem instance which stores the input page.

Return type

LazyItem[_T]

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.

value[source]#

The return value of the input generator.

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.

items#

The LazyItem list which represents a page of elements.

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]

pull(self)[source]#

Send paging request to pull a page of elements and store them in LazyItem.

Return type

None

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.

items#

The LazyItem list which represents a page of elements.

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

reverse(self)[source]#

Reverse the items of the LazyPagingList in place.

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

count(self, value)[source]#

Return the number of occurrences of value.

Parameters

value (Any) – The value needs to be counted.

Returns

The number of occurrences of value.

Return type

int

extend(self, values)[source]#

Extend LazyPagingList by appending elements from the iterable.

Parameters

values (Iterable[_T]) – Elements to be extended into the LazyPagingList.

Return type

None