Bucket
amltk.store.bucket
#
Module containing the base definition of a bucket.
A bucket is a collection of resources that can be accessed by a key of a given type. This lets you easily store and retrieve objects of varying types in a single location.
Concrete examples
Bucket
#
Bases: ABC
, MutableMapping[KeyT, Drop[LinkT]]
, Generic[KeyT, LinkT]
Definition of a bucket of resources, accessed by a Key.
Indexing into a bucket returns a Drop
that
can be used to access the resource.
The definition mostly follow that of MutableMapping, but with
the change of .keys()
and .values()
to return iterators
and .items()
to return an iterator of tuples.
The other change is that the .values()
do not return the
resources themselves, by rather a Drop
which wraps the resource.
__contains__
#
Check if a key is in the bucket.
PARAMETER | DESCRIPTION |
---|---|
key |
The key to check for.
TYPE:
|
__delitem__
abstractmethod
#
Remove a resource from the bucket.
PARAMETER | DESCRIPTION |
---|---|
key |
The key to the resource.
TYPE:
|
__getitem__
abstractmethod
#
__getitem__(key: KeyT) -> Drop[LinkT]
Get a drop for a resource in the bucket.
PARAMETER | DESCRIPTION |
---|---|
key |
The key to the resource.
TYPE:
|
__setitem__
abstractmethod
#
__setitem__(key: KeyT, value: Any) -> None
Store a value in the bucket.
PARAMETER | DESCRIPTION |
---|---|
key |
The key to the resource.
TYPE:
|
value |
The value to store in the bucket.
TYPE:
|
fetch
#
Fetch a resource from the bucket.
PARAMETER | DESCRIPTION |
---|---|
keys |
The keys to the resources.
TYPE:
|
default |
The default value to return if the key is not in the bucket. If a dict is passed, the default for each key will be the value in the dict for that key, using None if not present. |
RETURNS | DESCRIPTION |
---|---|
dict[KeyT, Any]
|
The resources stored in the bucket at the given keys. |
Source code in src/amltk/store/bucket.py
find
#
find(
pattern: str, *, multi_key: bool = False
) -> (
dict[str, Drop[LinkT]]
| dict[tuple[str, ...], Drop[LinkT]]
| None
)
Find resources in the bucket.
found = bucket.find(r"trial_(.+)_val_predictions.npy") # (1)!
if found is None:
raise KeyError("No predictions found")
for name, drop in found.items():
predictions = drop.get()
# Do something with the predictions
# ...
- The
(.+)
is a capture group which will attempt to match anything.
, when there is one or more occurences+
, and put it in a capure group()
. What is captured will be used as the key in the returned dict.
PARAMETER | DESCRIPTION |
---|---|
pattern |
The pattern to search for.
TYPE:
|
multi_key |
Whether you have multiple capture groups in the pattern. Multiple capture groups with If using multiple capture groups, the returned dict will have tuples as keys. If there is only one capture group, the tuple will be expanded to a single value.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
dict[str, Drop[LinkT]] | dict[tuple[str, ...], Drop[LinkT]] | None
|
A mapping of links to drops for the resources found. |
Source code in src/amltk/store/bucket.py
remove
#
Remove resources from the bucket.
PARAMETER | DESCRIPTION |
---|---|
keys |
The keys to the resources.
TYPE:
|
how |
A function that removes the resource. |
RETURNS | DESCRIPTION |
---|---|
dict[KeyT, bool]
|
A mapping of keys to whether they were removed. |
Source code in src/amltk/store/bucket.py
store
#
Store items into the bucket with the given mapping.
PARAMETER | DESCRIPTION |
---|---|
other |
The mapping of items to store in the bucket. |
sub
abstractmethod
#
Create a subbucket of this bucket.
PARAMETER | DESCRIPTION |
---|---|
key |
The name of the sub bucket.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Self
|
A new bucket with the same loaders as the current bucket. |
update
#
Update the bucket with the given mapping.
PARAMETER | DESCRIPTION |
---|---|
items |
The mapping of items to store in the bucket. |