qcodespp.data.io

Attributes

Classes

Module Contents

qcodespp.data.io.ALLOWED_OPEN_MODES = ('r', 'w', 'a')
class qcodespp.data.io.DiskIO(base_location)

Simple IO object to wrap disk operations with a custom base location.

Also accepts both forward and backward slashes at any point, and normalizes both to the OS we are currently on.

Args:
base_location (str): a path to the root data folder.

Converted to an absolute path immediately, so even if you supply a relative path, later changes to the OS working directory will not affect data paths.

open(filename, mode, encoding=None)

Mimic the interface of the built in open context manager.

Args:

filename (str): path relative to base_location.

mode (str): ‘r’ (read), ‘w’ (write), or ‘a’ (append).

Other open modes are not supported because we don’t want to force all IO managers to support others.

Returns:

context manager yielding the open file

to_path(location)

Convert a location string into a path on the local file system.

For DiskIO this just fixes slashes and prepends the base location, doing nothing active with the file. But for other io managers that refer to remote storage, this method may actually fetch the file and put it at a temporary local path.

Args:
location (str): A location string for a complete dataset or

a file within it.

Returns:

path (str): The path on disk to which this location maps.

to_location(path)

Convert a local filesystem path into a location string.

Args:

path (str): a path on the local file system.

Returns:

location (str): the location string corresponding to this path.

__repr__()

Show the base location in the repr.

join(*args)

Context-dependent os.path.join for this io manager.

isfile(location)

Check whether this location matches a file.

list(location, maxdepth=1, include_dirs=False)

Return all files that match location.

This is either files whose names match up to an arbitrary extension, or any files within an exactly matching directory name.

Args:
location (str): the location to match.

May contain the usual path wildcards * and ?

maxdepth (int, optional): maximum levels of directory nesting to

recurse into looking for files. Default 1.

include_dirs (bool, optional): whether to allow directories in

the results or just files. Default False.

Returns:

A list of matching files and/or directories, as locations relative to our base_location.

remove(filename)

Delete a file or folder and prune the directory tree.

remove_all(location)

Delete all files/directories in the dataset at this location.

Afterward prunes the directory tree.