TFDS now supports the Croissant 🥐 format! Read the documentation to know more.

tfds.testing.MockFs

View source on GitHub

This util wraps mock for the tf.io.gfile / epath.Path API.

tfds.testing.MockFs()

This allow to test code which uses absolute paths / GCS path while keeping tests hermetic.

Usage:

with MockFs() as fs:
 # GCS example
 fs.add_file('gs://bucket/dir/file.txt')
 assert tf.io.gfile.glob('gs://bucket/*/file.txt') == [
 'gs://bucket/dir/file.txt',
 ]
 # This also works with absolute paths
 tf.io.gfile.makedirs('/path/to/')
 with tf.io.gfile.GFile('/path/to/file.txt', 'w') as f:
 f.write('Content of file.txt')

Internally, this is done by converting absolute path into local tmp paths:

  • /absolute/path -> /tmp/mocked_file_system/absolute/path
  • gs://path -> /tmp/mocked_file_system/gs/path

Methods

add_file

View source

add_file(
 path, content=None
) -> None

Add a file, creating all parent directories.

contextmanager

View source

@contextlib.contextmanager
contextmanager() -> Iterator['MockFs']

Activate the mock file system.

mock

View source

@contextlib.contextmanager
mock()

View source

print_tree() -> None

read_file

View source

read_file(
 path
) -> str

__enter__

View source

__enter__()

__exit__

View source

__exit__(
 exc_type, exc_value, traceback
)

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2024年04月26日 UTC.