PetArray#

class petdb.PetArray(data: List[T] = None)#
find(query: dict | Callable[[T], bool]) T | None#

Returns the first element that satisfies the provided query. If no values satisfy, None is returned.

Parameters:

query – A query object that to match against

Returns:

The first matched element or None

findall(query: dict | Callable[[T], bool]) List[T]#

Returns all elements that satisfy the provided query. If no values satisfy, the empty list is returned.

Parameters:

query – A query object that selects which documents to include in the result set

Returns:

List of matched documents

filter(query: dict | Callable[[T], bool]) PetArray[T]#

Perform filter mutation. Accepts only query object.

contains(query: T | dict | Callable[[T], bool]) bool#

Check whether the collection contains a document matching a query.

Parameters:

query – the query object

exists(query: T | dict | Callable[[T], bool]) bool#

Alias for PetArray.contains()

sort(query: i_sort = None, reverse: bool = False) Self#

Perform sort mutation. Accepts a path, list of paths and sorting function.

map(func: Callable[[T], TP]) PetArray[TP]#

Perform map mutation. Accepts only callable object.

pick(*queries: str | int | Callable[[T], TP]) PetArray[TP]#

Makes a list of copies of objects consisting of the picked properties.

Parameters:

queries – Queries to pick keys from objects.

omit(*queries: str) PetArray#

Makes a list of copies of objects without the omitted properties.

Parameters:

queries – Queries to omit keys from objects.

foreach(func: Callable[[T], None]) None#

Go through all elements and call the given function with each.

Parameters:

func – function that takes each element in the array

size(query: T | dict | Callable[[T], bool] = None) int#

Returns the amount of all documents in the collection

length(query: T | dict | Callable[[T], bool] = None) int#

Returns the amount of all documents in the collection

insert(element: T) T#

Insert a new element into the array.

Parameters:

element – the element to insert

Returns:

inserted element

insert_many(elements: Iterable[T]) list[T]#

Insert multiple elements into the array.

Parameters:

elements – an Iterable of elements to insert

Returns:

a list containing the inserted elements

clear() List[T]#

Removes all elements from the array.

Returns:

Removed elements

remove(query: dict) List[T]#

Removes matched documents. Accepts id, query object, list of ids and list of documents. Performs clearing if the query is None. Returns removed documents.

Returns:

removed documents

delete(query: dict) Self#

Calls the remove method and returns self

update(update: dict, query: dict | Callable[[T], bool] = None) Self#

Applies update query to all elements that match the given query

update_one(update: dict, query: dict | Callable[[T], bool] = None) Self#

Applies update query to a single element matching the given query

unique(query: str | int | Callable[[T], Any] = None) PetArray[T]#

Removes all repeated elements

groupby(key: str | int | Iterable[str | int] | Callable[[T], Any] = None) PetArray[dict]#

Groups elements by field accessed by a key.

Parameters:

key – The key or keys to access value or values to group elements by.

Returns:

A PetArray of dicts with key and items fields.

Raises:

QueryException – If the key type is not a string, integer or iterable of strings or integers.

join(delimiter: str, adapter: Callable[[T], str] = None) str#

Compose all elements of a PetArray into the single string with the given delimiter.

Parameters:
  • delimiter – Separator used to join elements.

  • adapter – Function to convert each element to string.

Returns:

The concatenated string

reduce(reducer: Callable[[TP, T], TP], init: TP = None) TP#

Executes the given “reducer” callback function on each element of the array, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.

Parameters:
  • reducer – A function to execute for each element in the array with the next signature: reducer(accumulator: TP, item: T) -> TP. Its return value becomes the value of the accumulator parameter on the next invocation of reducer. For the last invocation, the return value becomes the return value of reduce(). The function is called with the following arguments:

  • init – A value to which accumulator is initialized the first time the callback is called. If init is specified, reducer starts executing with the first value in the array as item. If init is not specified, accumulator is initialized to the first value in the array, and reducer starts executing with the second value in the array as currentValue. In this case, if the array is empty (so that there’s no first value to return as accumulator), a None is returned.

Returns:

The value that results from running the reducer callback to completion over the entire array.

list() List[T]#

Returns all documents stored in the collection as a list

Returns:

a list with all documents.