-
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add argument
compressed
to save/load functions
- Loading branch information
1 parent
bcf7a39
commit 27d26b6
Showing
7 changed files
with
145 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,47 @@ | ||
"""JSON input interface.""" | ||
import gzip | ||
import json | ||
from pathlib import Path | ||
from typing import TextIO, Union | ||
from typing import Optional, TextIO, Union | ||
|
||
from ..music import Music | ||
|
||
|
||
def load_json(path: Union[str, Path, TextIO]) -> Music: | ||
def load_json( | ||
path: Union[str, Path, TextIO], compressed: Optional[bool] = None | ||
) -> Music: | ||
"""Load a JSON file into a Music object. | ||
Parameters | ||
---------- | ||
path : str, Path or TextIO | ||
Path to the file or the file to load. | ||
compressed : bool, optional | ||
Whether the file is a compressed JSON file (`.json.gz`). Has no | ||
effect when `path` is a file object. Defaults to infer from the | ||
extension (`.gz`). | ||
Returns | ||
------- | ||
:class:`muspy.Music` | ||
Loaded Music object. | ||
Notes | ||
----- | ||
When a path is given, assume UTF-8 encoding and gzip compression if | ||
`compressed=True`. | ||
""" | ||
if isinstance(path, (str, Path)): | ||
with open(str(path), encoding="utf-8") as f: | ||
if compressed is None: | ||
if str(path).lower().endswith(".gz"): | ||
compressed = True | ||
else: | ||
compressed = False | ||
if compressed: | ||
with gzip.open(path, "rt", encoding="utf-8") as f: | ||
return Music.from_dict(json.load(f)) | ||
with open(path, encoding="utf-8") as f: | ||
return Music.from_dict(json.load(f)) | ||
|
||
return Music.from_dict(json.load(path)) |
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 |
---|---|---|
@@ -1,28 +1,48 @@ | ||
"""YAML input interface.""" | ||
import gzip | ||
from pathlib import Path | ||
from typing import TextIO, Union | ||
from typing import Optional, TextIO, Union | ||
|
||
import yaml | ||
|
||
from ..music import Music | ||
|
||
|
||
def load_yaml(path: Union[str, Path, TextIO]) -> Music: | ||
def load_yaml( | ||
path: Union[str, Path, TextIO], compressed: Optional[bool] = None | ||
) -> Music: | ||
"""Load a YAML file into a Music object. | ||
Parameters | ||
---------- | ||
path : str, Path or TextIO | ||
Path to the file or the file to load. | ||
compressed : bool, optional | ||
Whether the file is a compressed YAML file (`.yaml.gz`). Has no | ||
effect when `path` is a file object. Defaults to infer from the | ||
extension (`.gz`). | ||
Returns | ||
------- | ||
:class:`muspy.Music` | ||
Loaded Music object. | ||
Notes | ||
----- | ||
When a path is given, assume UTF-8 encoding and gzip compression if | ||
`compressed=True`. | ||
""" | ||
if isinstance(path, (str, Path)): | ||
with open(str(path), encoding="utf-8") as f: | ||
if compressed is None: | ||
if str(path).lower().endswith(".gz"): | ||
compressed = True | ||
else: | ||
compressed = False | ||
if compressed: | ||
with gzip.open(path, "rt", encoding="utf-8") as f: | ||
return Music.from_dict(yaml.safe_load(f)) | ||
with open(path, encoding="utf-8") as f: | ||
return Music.from_dict(yaml.safe_load(f)) | ||
|
||
return Music.from_dict(yaml.safe_load(path)) |
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
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