From a9f8424ea2367782a9cc708768c33c71fa782f3f Mon Sep 17 00:00:00 2001 From: Jiajun Yao Date: Mon, 24 Jul 2023 20:55:24 -0700 Subject: [PATCH] [Doc] Make doc code snippet testable [10/n] (#37636) Remove # doctest: +SKIP Signed-off-by: Balaji Veeramani Signed-off-by: Jiajun Yao Co-authored-by: Balaji Veeramani Co-authored-by: Balaji Veeramani Signed-off-by: e428265 --- python/ray/_private/storage.py | 166 +++++++++++++++++++++++---------- 1 file changed, 119 insertions(+), 47 deletions(-) diff --git a/python/ray/_private/storage.py b/python/ray/_private/storage.py index 290f4dbc9820e..d594f4ba754b2 100644 --- a/python/ray/_private/storage.py +++ b/python/ray/_private/storage.py @@ -35,12 +35,22 @@ def get_filesystem() -> ("pyarrow.fs.FileSystem", str): storage filesystem. Examples: - # Assume ray.init(storage="s3:/bucket/cluster_1/storage") - >>> fs, path = storage.get_filesystem() # doctest: +SKIP - >>> print(fs) # doctest: +SKIP - - >>> print(path) # doctest: +SKIP - cluster_1/storage + .. testcode:: + + import ray + from ray._private import storage + + ray.shutdown() + + ray.init(storage="/tmp/storage/cluster_1/storage") + fs, path = storage.get_filesystem() + print(fs) + print(path) + + .. testoutput:: + + + /tmp/storage/cluster_1/storage Returns: Tuple of pyarrow filesystem instance and the path under which files should @@ -63,9 +73,16 @@ def get_client(prefix: str) -> "KVClient": data will be stored under. All writes will be scoped to this sub-dir. Examples: - # Assume ray.init(storage="s3:/bucket/cluster_1/storage") - >>> client = storage.get_client("foo") # doctest: +SKIP - >>> client.put("foo", b"bar") # doctest: +SKIP + .. testcode:: + + import ray + from ray._private import storage + + ray.shutdown() + + ray.init(storage="/tmp/storage/cluster_1/storage") + client = storage.get_client("foo") + client.put("foo", b"bar") Returns: KVClient. @@ -107,9 +124,16 @@ def put(self, path: str, value: bytes) -> None: """Save a blob in persistent storage at the given path, if possible. Examples: - # Writes "bar" to /my_app/path/foo.txt - >>> client = storage.get_client("my_app") # doctest: +SKIP - >>> client.put("path/foo.txt", b"bar") # doctest: +SKIP + .. testcode:: + + import ray + from ray._private import storage + + ray.shutdown() + + ray.init(storage="/tmp/storage/cluster_1/storage") + client = storage.get_client("my_app") + client.put("path/foo.txt", b"bar") Args: path: Relative directory of the blobs. @@ -130,12 +154,19 @@ def get(self, path: str) -> bytes: """Load a blob from persistent storage at the given path, if possible. Examples: - # Loads value from /my_app/path/foo.txt - >>> client = storage.get_client("my_app") # doctest: +SKIP - >>> client.get("path/foo.txt") # doctest: +SKIP - b"bar" - >>> client.get("invalid") # doctest: +SKIP - None + .. testcode:: + + import ray + from ray._private import storage + + ray.shutdown() + + ray.init(storage="/tmp/storage/cluster_1/storage") + + client = storage.get_client("my_app") + client.put("path/foo.txt", b"bar") + assert client.get("path/foo.txt") == b"bar" + assert client.get("invalid") is None Args: path: Relative directory of the blobs. @@ -158,10 +189,18 @@ def delete(self, path: str) -> bool: """Load the blob from persistent storage at the given path, if possible. Examples: - # Deletes blob at /my_app/path/foo.txt - >>> client = storage.get_client("my_app") # doctest: +SKIP - >>> client.delete("path/foo.txt") # doctest: +SKIP - True + .. testcode:: + + import ray + from ray._private import storage + + ray.shutdown() + + ray.init(storage="/tmp/storage/cluster_1/storage") + + client = storage.get_client("my_app") + client.put("path/foo.txt", b"bar") + assert client.delete("path/foo.txt") Args: path: Relative directory of the blob. @@ -184,10 +223,18 @@ def delete_dir(self, path: str) -> bool: """Delete a directory and its contents, recursively. Examples: - # Deletes dir at /my_app/path/ - >>> client = storage.get_client("my_app") # doctest: +SKIP - >>> client.delete_dir("path") # doctest: +SKIP - True + .. testcode:: + + import ray + from ray._private import storage + + ray.shutdown() + + ray.init(storage="/tmp/storage/cluster_1/storage") + + client = storage.get_client("my_app") + client.put("path/foo.txt", b"bar") + assert client.delete_dir("path") Args: path: Relative directory of the blob. @@ -210,15 +257,26 @@ def get_info(self, path: str) -> Optional["pyarrow.fs.FileInfo"]: """Get info about the persistent blob at the given path, if possible. Examples: - # Inspect blob at /my_app/path/foo.txt - >>> client = storage.get_client("my_app") # doctest: +SKIP - >>> client.get_info("path/foo.txt") # doctest: +SKIP - + .. testcode:: + + import ray + from ray._private import storage + + ray.shutdown() + + ray.init(storage="/tmp/storage/cluster_1/storage") + + client = storage.get_client("my_app") + client.put("path/foo.txt", b"bar") + + print(client.get_info("path/foo.txt")) - # Non-existent blob. - >>> client.get_info("path/does_not_exist.txt") # doctest: +SKIP - None - + print(client.get_info("path/does_not_exist.txt")) + + .. testoutput:: + + + None Args: path: Relative directory of the blob. @@ -241,19 +299,33 @@ def list( """List blobs and sub-dirs in the given path, if possible. Examples: - # List created blobs and dirs at /my_app/path - >>> client = storage.get_client("my_app") # doctest: +SKIP - >>> client.list("path") # doctest: +SKIP - [, - ] - - # Non-existent path. - >>> client.get_info("does_not_exist") # doctest: +SKIP - FileNotFoundError: ... - - # Not a directory. - >>> storage.get_info("path/foo.txt") # doctest: +SKIP - NotADirectoryError: ... + + >>> import ray + >>> from ray._private import storage + >>> ray.shutdown() + + Normal usage. + + >>> ray.init(storage="/tmp/storage/cluster_1/storage") + RayContext(...) + >>> client = storage.get_client("my_app") + >>> client.put("path/foo.txt", b"bar") + >>> client.list("path") + [] + + Non-existent path. + + >>> client.list("does_not_exist") + Traceback (most recent call last): + ... + FileNotFoundError: ... No such file or directory + + Not a directory. + + >>> client.list("path/foo.txt") + Traceback (most recent call last): + ... + NotADirectoryError: ... Not a directory Args: path: Relative directory to list from.