-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added read/save_object_file to match the R packages.
This is also a bit more convenient for method developers.
- Loading branch information
Showing
8 changed files
with
68 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from typing import Dict, Any | ||
import os | ||
import json | ||
|
||
|
||
def read_object_file(path: str) -> Dict[str, Any]: | ||
""" | ||
Read the ``OBJECT`` file in each directory, which provides some high-level | ||
metadata of the object represented by that directory. It is guaranteed to | ||
have a ‘type’ property that specifies the object type; individual objects | ||
may add their own information to this file. | ||
Args: | ||
path: | ||
Path to a directory containing the object. | ||
Returns: | ||
Dictionary containing the object metadata. | ||
""" | ||
with open(os.path.join(path, "OBJECT"), "rb") as handle: | ||
metadata = json.load(handle) | ||
return metadata |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from typing import Dict, Any | ||
import os | ||
import json | ||
|
||
|
||
def save_object_file(path: str, object_type: str, extra: Dict[str, Any] = {}): | ||
""" | ||
Saves object-specific metadata into the ``OBJECT`` file inside each | ||
directory, to be used by, e.g., :py:func:`~.read_object_file`. | ||
Args: | ||
path: | ||
Path to the directory representing an object. An ``OBJECT`` file | ||
will be created inside this directory. | ||
object_type: | ||
Type of the object. | ||
extra: | ||
Extra metadata to be written to the ``OBJECT`` file in ``path``. | ||
Any entry named ``type`` will be overwritten by ``object_type``. | ||
""" | ||
to_save = { **extra } | ||
to_save["type"] = object_type | ||
with open(os.path.join(path, "OBJECT"), 'w', encoding="utf-8") as handle: | ||
json.dump(to_save, handle) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters