tensorbay.client.lazy

Lazy evaluation related classes.

class tensorbay.client.lazy.LazyItem(page, data)[source]

Bases: Generic[tensorbay.client.lazy._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 – The page the item belongs to.

page

The parent LazyPage of this item.

data

The actual element stored in this item.

classmethod from_page(page)[source]

Create a LazyItem instance from page.

Parameters

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

Returns

The LazyItem instance which stores the input page.

Return type

tensorbay.client.lazy.LazyItem[tensorbay.client.lazy._T]

classmethod from_data(data)[source]

Create a LazyItem instance from data.

Parameters

data (tensorbay.client.lazy._T) – The actual data needs to be stored in LazyItem.

Returns

The LazyItem instance which stores the input data.

Return type

tensorbay.client.lazy.LazyItem[tensorbay.client.lazy._T]

get()[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

tensorbay.client.lazy._T

class tensorbay.client.lazy.ReturnGenerator(generator)[source]

Bases: Generic[tensorbay.client.lazy._T, tensorbay.client.lazy._R]

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

Parameters

generator – The generator needs to be wrapped.

value

The return value of the input generator.

Type

tensorbay.client.lazy._R

class tensorbay.client.lazy.LazyPage(offset, limit, func)[source]

Bases: Generic[tensorbay.client.lazy._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 – The offset of the page.

  • limit – The limit of the page.

  • func – 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(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 (Callable[[int, int], Generator[tensorbay.client.lazy._T, None, int]]) – 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[tensorbay.client.lazy._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

tensorbay.client.lazy.LazyPage[tensorbay.client.lazy._T]

pull()[source]

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

Return type

None

class tensorbay.client.lazy.InitPage(offset, limit, func)[source]

Bases: tensorbay.client.lazy.LazyPage[tensorbay.client.lazy._T]

In paging lazy evaluation system, InitPage is the page for initializing PagingList.

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 – The offset of the page.

  • limit – The limit of the page.

  • func – 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 tensorbay.client.lazy.PagingList(func, limit)[source]

Bases: MutableSequence[tensorbay.client.lazy._T], tensorbay.utility.repr.ReprMixin

PagingList 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 – 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 – The page size of each paging request.

insert(index, value)[source]

Insert object before index.

Parameters
  • index (int) – Position of the PagingList.

  • value (tensorbay.client.lazy._T) – Element to be inserted into the PagingList.

Return type

None

append(value)[source]

Append object to the end of the PagingList.

Parameters

value (tensorbay.client.lazy._T) – Element to be appended to the PagingList.

Return type

None

reverse()[source]

Reverse the items of the PagingList in place.

Return type

None

pop(index=- 1)[source]

Return the item at index (default last) and remove it from the PagingList.

Parameters

index (int) – Position of the PagingList.

Returns

Element to be removed from the PagingList.

Return type

tensorbay.client.lazy._T

index(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 PagingList

Returns

The first index of the value.

Return type

int

count(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(values)[source]

Extend PagingList by appending elements from the iterable.

Parameters

values (Iterable[tensorbay.client.lazy._T]) – Elements to be extended into the PagingList.

Return type

None