PetMutable#
- class petdb.PetMutable(col: PetCollection, data: list[dict])#
Bases:
PetArray[dict]- get(id: str) dict | None#
Search for a document with the given id
- Parameters:
id – document’s id
- Returns:
a single document or
Noneif no matching document is found
- filter(query: dict | Callable[[dict], bool]) PetMutable#
Perform filter mutation. Accepts only query object.
- insert(doc: dict) dict#
Insert a new document into the parent collection.
- Parameters:
doc – the document to insert
- Returns:
inserted document
- insert_many(docs: Iterable[dict]) list[dict]#
Insert multiple documents into the collection.
- Parameters:
docs – an Iterable of documents to insert
- Returns:
a list containing the inserted documents
- update_one(update: dict, query: dict = None) None#
Applies update query to a single element matching the given query
- update(update: dict, query: dict = None) None#
Applies update query to all documents in mutated list that matches the given query, affects the original collection
- clear() List[dict]#
Removes all documents from the current mutable array and removes all of them from the original collection.
- Returns:
Removed documents
- remove(query: dict) List[dict]#
Removes matched documents, affects the original collection. Accepts id, query object, list of ids and list of documents. Performs clearing if the query is None. Returns removed documents.
- Returns:
removed documents
- contains(query: T | dict | Callable[[T], bool]) bool#
Check whether the collection contains a document matching a query.
- Parameters:
query – the query object
- delete(query: dict) Self#
Calls the remove method and returns self
- exists(query: T | dict | Callable[[T], bool]) bool#
Alias for
PetArray.contains()
- find(query: dict | Callable[[T], bool]) T | None#
Returns the first element that satisfies the provided
query. If no values satisfy,Noneis 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
- 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
- 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
keyanditemsfields.- 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
- length(query: T | dict | Callable[[T], bool] = None) int#
Returns the amount of all documents in the collection
- list() List[T]#
Returns all documents stored in the collection as a list
- Returns:
a list with all documents.
- omit(*queries: str) PetArray#
Makes a list of copies of objects without the omitted properties.
- Parameters:
queries – Queries to omit keys from objects.
- 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.
- 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 theaccumulatorparameter 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
accumulatoris initialized the first time the callback is called. Ifinitis specified,reducerstarts executing with the first value in the array asitem. Ifinitis not specified,accumulatoris initialized to the first value in the array, andreducerstarts 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 asaccumulator), aNoneis returned.
- Returns:
The value that results from running the
reducercallback to completion over the entire array.
- size(query: T | dict | Callable[[T], bool] = None) int#
Returns the amount of all documents in the collection
- sort(query: i_sort = None, reverse: bool = False) Self#
Perform sort mutation. Accepts a path, list of paths and sorting function.