Commit 30985b2f authored by Karlind's avatar Karlind Committed by Kai Chen
Browse files

add support for pathlib to load and dump (#91)

* add support for pathlib to load and dump

* minor formatting
parent 5a5c94b6
from pathlib import Path
from ..utils import is_list_of, is_str
from .handlers import BaseFileHandler, JsonHandler, PickleHandler, YamlHandler
......@@ -16,7 +18,8 @@ def load(file, file_format=None, **kwargs):
This method provides a unified api for loading data from serialized files.
Args:
file (str or file-like object): Filename or a file-like object.
file (str or :obj:`Path` or file-like object): Filename or a file-like
object.
file_format (str, optional): If not specified, the file format will be
inferred from the file extension, otherwise use the specified one.
Currently supported formats include "json", "yaml/yml" and
......@@ -25,6 +28,8 @@ def load(file, file_format=None, **kwargs):
Returns:
The content from the file.
"""
if isinstance(file, Path):
file = str(file)
if file_format is None and is_str(file):
file_format = file.split('.')[-1]
if file_format not in file_handlers:
......@@ -48,14 +53,16 @@ def dump(obj, file=None, file_format=None, **kwargs):
Args:
obj (any): The python object to be dumped.
file (str or file-like object, optional): If not specified, then the
object is dump to a str, otherwise to a file specified by the
filename or file-like object.
file (str or :obj:`Path` or file-like object, optional): If not
specified, then the object is dump to a str, otherwise to a file
specified by the filename or file-like object.
file_format (str, optional): Same as :func:`load`.
Returns:
bool: True for success, False otherwise.
"""
if isinstance(file, Path):
file = str(file)
if file_format is None:
if is_str(file):
file_format = file.split('.')[-1]
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment