Root class#

A root is a remote repository that can be subscribed to.

class caterva2.Root(client, name)#
Attributes:
cookie
file_list

Retrieves a list of files in this root.

urlbase

Methods

upload(localpath[, remotepath])

Uploads a local file to this root.

Special Methods:

__init__(client, name)

Represents a remote repository that can be subscribed to.

__getitem__(path)

Retrieves a file or dataset from the root.

__contains__(path)

Checks if a path exists in the root.

__iter__()

Iterates over the files and datasets in the root.

__len__()

Returns the number of files in the root.

Constructor#

__init__(client, name)#

Represents a remote repository that can be subscribed to.

Parameters:
  • client (Client) – The client used to interact with the remote repository.

  • name (str) – Name of the root to subscribe to.

Examples

>>> import caterva2 as cat2
>>> client = cat2.Client("https://demo.caterva2.net")
>>> root = client.get("example")
>>> root.file_list[-3:]
['ds-sc-attr.b2nd', 'lung-jpeg2000_10x.b2nd', 'tomo-guess-test.b2nd']

Utility Methods#

__getitem__(path)#

Retrieves a file or dataset from the root.

Parameters:

path (str or Path) – Path of the file or dataset to retrieve.

Returns:

An instance of File or Dataset.

Return type:

File, Dataset

Examples

>>> import caterva2 as cat2
>>> client = cat2.Client('https://demo.caterva2.net')
>>> root = client.get('example')
>>> root['ds-1d.b2nd']
<Dataset: example/ds-1d.b2nd>
__contains__(path)#

Checks if a path exists in the root.

Parameters:

path (str or Path) – The path of the file or dataset to check.

Returns:

True if the path exists in the root, otherwise False.

Return type:

bool

Examples

>>> import caterva2 as cat2
>>> client = cat2.Client('https://demo.caterva2.net')
>>> root = client.get('example')
>>> 'ds-1d.b2nd' in root
True
__iter__()#

Iterates over the files and datasets in the root.

Returns:

An iterator for the files and datasets in the root.

Return type:

iter

Examples

>>> import caterva2 as cat2
>>> client = cat2.Client('https://demo.caterva2.net')
>>> root = client.get('example')
>>> for file in root:
...     print(file)
README.md
dir1/ds-2d.b2nd
dir1/ds-3d.b2nd
dir2/ds-4d.b2nd
ds-1d-b.b2nd
ds-1d-fields.b2nd
ds-1d.b2nd
ds-2d-fields.b2nd
ds-hello.b2frame
ds-sc-attr.b2nd
lung-jpeg2000_10x.b2nd
tomo-guess-test.b2nd
__len__()#

Returns the number of files in the root.

Examples

>>> import caterva2 as cat2
>>> client = cat2.Client('https://demo.caterva2.net')
>>> root = client.get('example')
>>> len(root)
12
upload(localpath, remotepath=None)#

Uploads a local file to this root.

Parameters:
  • localpath (Path) – Path of the local file to upload.

  • remotepath (Path, optional) – Remote path where the file will be uploaded. If not provided, the file will be uploaded to the top level of this root.

Returns:

A instance of File or Dataset.

Return type:

File

Examples

>>> import caterva2 as cat2
>>> import numpy as np
>>> # To upload a file you must be registered as a user.
>>> client = cat2.Client("https://cat2.cloud/demo", ("joedoe@example.com", "foobar"))
>>> root = client.get('@personal')
>>> path = f'@personal/dir{np.random.randint(0, 100)}/ds-4d.b2nd'
>>> root.upload('root-example/dir2/ds-4d.b2nd')
<Dataset: @personal/root-example/dir2/ds-4d.b2nd>
>>> 'root-example/dir2/ds-4d.b2nd' in root
True
property file_list#

Retrieves a list of files in this root.