This repository has been archived by the owner on Sep 19, 2023. It is now read-only.
-
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.
Refactored
json
exporting implementations #23
Signed-off-by: RaenonX <[email protected]>
- Loading branch information
Showing
5 changed files
with
61 additions
and
10 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
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,3 +1,4 @@ | ||
"""Various utility functions.""" | ||
from .execution import concurrent_run, concurrent_run_no_return, time_exec | ||
from .export import export_json | ||
from .image import crop_image, merge_y_cb_cr_a |
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,52 @@ | ||
"""Utility function for exporting.""" | ||
import json | ||
from typing import Any, Optional | ||
|
||
__all__ = ("export_json",) | ||
|
||
|
||
def round_floats(obj: Any) -> Any: | ||
"""Round the ``float`` in ``obj``.""" | ||
if isinstance(obj, float): | ||
return float(format(obj, ".9g")) | ||
|
||
if isinstance(obj, dict): | ||
return {k: round_floats(v) for k, v in obj.items()} | ||
|
||
if isinstance(obj, (list, tuple)): | ||
return [round_floats(x) for x in obj] | ||
|
||
return obj | ||
|
||
|
||
def export_json(export_path: str, obj: Any, /, separators: Optional[tuple[str, str]] = None) -> None: | ||
""" | ||
Export the object ``obj`` to ``export_path``. | ||
It was tested that the solutions below are slower: | ||
>>> with open(export_path, "w+", encoding="utf-8") as f: | ||
>>> f.write(json.dumps( | ||
>>> json.loads( | ||
>>> json.dumps(obj), | ||
>>> parse_float=lambda x: f"{float(x):.9g}" | ||
>>> ) | ||
>>> )) | ||
The solution above was about on par. | ||
>>> with open(export_path, "w+", encoding="utf-8") as f: | ||
>>> json.dump(round_floats(obj), f) | ||
The solution above is about 3x slower. | ||
>>> with open(export_path, "w+", encoding="utf-8") as f: | ||
>>> json.dump( | ||
>>> json.loads(json.dumps(obj), parse_float=lambda x: f"{float(x):.9g}"), | ||
>>> f, | ||
>>> ) | ||
The solution above is about 3x slower. | ||
""" | ||
with open(export_path, "w+", encoding="utf-8") as f: | ||
f.write(json.dumps(round_floats(obj), ensure_ascii=False, indent=2, separators=separators)) |