Skip to content

Commit

Permalink
add support for pathlib to load and dump (open-mmlab#91)
Browse files Browse the repository at this point in the history
* add support for pathlib to load and dump

* minor formatting
  • Loading branch information
karlind authored and hellock committed Jul 30, 2019
1 parent 5a5c94b commit 30985b2
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions mmcv/fileio/io.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from pathlib import Path

from ..utils import is_list_of, is_str
from .handlers import BaseFileHandler, JsonHandler, PickleHandler, YamlHandler

Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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]
Expand Down

0 comments on commit 30985b2

Please sign in to comment.