Shortcuts

PetrelBackend

class mmeval.fileio.PetrelBackend(path_mapping: Optional[dict] = None, enable_mc: bool = True)[source]

Petrel storage backend (for internal usage).

PetrelBackend supports reading and writing data to multiple clusters. If the file path contains the cluster name, PetrelBackend will read data from specified cluster or write data to it. Otherwise, PetrelBackend will access the default cluster.

Parameters
  • path_mapping (dict, optional) – Path mapping dict from local path to Petrel path. When path_mapping={'src': 'dst'}, src in filepath will be replaced by dst. Defaults to None.

  • enable_mc (bool, optional) – Whether to enable memcached support. Defaults to True.

Examples

>>> backend = PetrelBackend()
>>> filepath1 = 'petrel://path/of/file'
>>> filepath2 = 'cluster-name:petrel://path/of/file'
>>> backend.get(filepath1)  # get data from default cluster
>>> client.get(filepath2)  # get data from 'cluster-name' cluster
exists(filepath: Union[str, pathlib.Path])bool[source]

Check whether a file path exists.

Parameters

filepath (str or Path) – Path to be checked whether exists.

Returns

Return True if filepath exists, False otherwise.

Return type

bool

Examples

>>> backend = PetrelBackend()
>>> filepath = 'petrel://path/of/file'
>>> backend.exists(filepath)
True
get(filepath: Union[str, pathlib.Path])bytes[source]

Read bytes from a given filepath with ‘rb’ mode.

Parameters

filepath (str or Path) – Path to read data.

Returns

Return bytes read from filepath.

Return type

bytes

Examples

>>> backend = PetrelBackend()
>>> filepath = 'petrel://path/of/file'
>>> backend.get(filepath)
b'hello world'
get_local_path(filepath: Union[str, pathlib.Path])Generator[Union[str, pathlib.Path], None, None][source]

Download a file from filepath to a local temporary directory, and return the temporary path.

get_local_path is decorated by contxtlib.contextmanager(). It can be called with with statement, and when exists from the with statement, the temporary path will be released.

Parameters

filepath (str or Path) – Download a file from filepath.

Yields

Iterable[str] – Only yield one temporary path.

Examples

>>> backend = PetrelBackend()
>>> # After existing from the ``with`` clause,
>>> # the path will be removed
>>> filepath = 'petrel://path/of/file'
>>> with backend.get_local_path(filepath) as path:
...     # do something here
get_text(filepath: Union[str, pathlib.Path], encoding: str = 'utf-8')str[source]

Read text from a given filepath with ‘r’ mode.

Parameters
  • filepath (str or Path) – Path to read data.

  • encoding (str) – The encoding format used to open the filepath. Defaults to ‘utf-8’.

Returns

Expected text reading from filepath.

Return type

str

Examples

>>> backend = PetrelBackend()
>>> filepath = 'petrel://path/of/file'
>>> backend.get_text(filepath)
'hello world'
isdir(filepath: Union[str, pathlib.Path])bool[source]

Check whether a file path is a directory.

Parameters

filepath (str or Path) – Path to be checked whether it is a directory.

Returns

Return True if filepath points to a directory, False otherwise.

Return type

bool

Examples

>>> backend = PetrelBackend()
>>> filepath = 'petrel://path/of/dir'
>>> backend.isdir(filepath)
True
isfile(filepath: Union[str, pathlib.Path])bool[source]

Check whether a file path is a file.

Parameters

filepath (str or Path) – Path to be checked whether it is a file.

Returns

Return True if filepath points to a file, False otherwise.

Return type

bool

Examples

>>> backend = PetrelBackend()
>>> filepath = 'petrel://path/of/file'
>>> backend.isfile(filepath)
True
join_path(filepath: Union[str, pathlib.Path], *filepaths: Union[str, pathlib.Path])str[source]

Concatenate all file paths.

Join one or more filepath components intelligently. The return value is the concatenation of filepath and any members of *filepaths.

Parameters

filepath (str or Path) – Path to be concatenated.

Returns

The result after concatenation.

Return type

str

Examples

>>> backend = PetrelBackend()
>>> filepath = 'petrel://path/of/file'
>>> backend.join_path(filepath, 'another/path')
'petrel://path/of/file/another/path'
>>> backend.join_path(filepath, '/another/path')
'petrel://path/of/file/another/path'
list_dir_or_file(dir_path: Union[str, pathlib.Path], list_dir: bool = True, list_file: bool = True, suffix: Optional[Union[str, Tuple[str]]] = None, recursive: bool = False)Iterator[str][source]

Scan a directory to find the interested directories or files in arbitrary order.

Note

Petrel has no concept of directories but it simulates the directory hierarchy in the filesystem through public prefixes. In addition, if the returned path ends with ‘/’, it means the path is a public prefix which is a logical directory.

Note

list_dir_or_file() returns the path relative to dir_path. In addition, the returned path of directory will not contains the suffix ‘/’ which is consistent with other backends.

Parameters
  • dir_path (str | Path) – Path of the directory.

  • list_dir (bool) – List the directories. Defaults to True.

  • list_file (bool) – List the path of files. Defaults to True.

  • suffix (str or tuple[str], optional) – File suffix that we are interested in. Defaults to None.

  • recursive (bool) – If set to True, recursively scan the directory. Defaults to False.

Yields

Iterable[str] – A relative path to dir_path.

Examples

>>> backend = PetrelBackend()
>>> dir_path = 'petrel://path/of/dir'
>>> # list those files and directories in current directory
>>> for file_path in backend.list_dir_or_file(dir_path):
...     print(file_path)
>>> # only list files
>>> for file_path in backend.list_dir_or_file(dir_path, list_dir=False):
...     print(file_path)
>>> # only list directories
>>> for file_path in backend.list_dir_or_file(dir_path, list_file=False):
...     print(file_path)
>>> # only list files ending with specified suffixes
>>> for file_path in backend.list_dir_or_file(dir_path, suffix='.txt'):
...     print(file_path)
>>> # list all files and directory recursively
>>> for file_path in backend.list_dir_or_file(dir_path, recursive=True):
...     print(file_path)
Read the Docs v: latest
Versions
latest
stable
Downloads
pdf
html
epub
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.