tensorneko-util 0.3.12__tar.gz → 0.3.13__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/PKG-INFO +1 -1
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/io/json/json_reader.py +56 -14
- tensorneko_util-0.3.13/src/tensorneko_util/io/json/json_writer.py +70 -0
- tensorneko_util-0.3.13/src/tensorneko_util/version.txt +1 -0
- tensorneko_util-0.3.12/src/tensorneko_util/visualization/watcher/web/dist/assets/index.6a923aa2.js → tensorneko_util-0.3.13/src/tensorneko_util/visualization/watcher/web/dist/assets/index.493c52c5.js +54 -54
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/visualization/watcher/web/dist/index.html +1 -1
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util.egg-info/PKG-INFO +1 -1
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util.egg-info/SOURCES.txt +1 -1
- tensorneko_util-0.3.12/src/tensorneko_util/io/json/json_writer.py +0 -41
- tensorneko_util-0.3.12/src/tensorneko_util/version.txt +0 -1
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/LICENSE +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/README.md +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/setup.cfg +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/setup.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/setup_util.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/__init__.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/backend/__init__.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/backend/audio_lib.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/backend/blocking.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/backend/parallel.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/backend/tqdm.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/backend/visual_lib.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/debug/__init__.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/debug/logger.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/debug/parser.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/io/__init__.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/io/_default_backends.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/io/audio/__init__.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/io/audio/audio_data.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/io/audio/audio_reader.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/io/audio/audio_writer.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/io/hdf5/__init__.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/io/hdf5/hdf5_reader.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/io/hdf5/hdf5_writer.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/io/image/__init__.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/io/image/image_reader.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/io/image/image_writer.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/io/json/__init__.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/io/json/json_data.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/io/matlab/__init__.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/io/matlab/mat_reader.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/io/matlab/mat_writer.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/io/npy/__init__.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/io/npy/npy_reader.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/io/npy/npy_writer.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/io/pickle/__init__.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/io/pickle/pickle_reader.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/io/pickle/pickle_writer.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/io/reader.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/io/text/__init__.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/io/text/text_reader.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/io/text/text_writer.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/io/toml/__init__.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/io/toml/toml_reader.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/io/toml/toml_writer.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/io/video/__init__.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/io/video/video_data.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/io/video/video_reader.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/io/video/video_writer.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/io/writer.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/io/yaml/__init__.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/io/yaml/yaml_reader.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/io/yaml/yaml_writer.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/notebook/__init__.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/notebook/display.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/preprocess/__init__.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/preprocess/crop.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/preprocess/face_detector/__init__.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/preprocess/face_detector/_utils.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/preprocess/face_detector/abstract_face_detector.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/preprocess/face_detector/anime_face_detector.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/preprocess/face_detector/facexzoo_face_detector.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/preprocess/face_detector/opencv_face_detector.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/preprocess/ffmpeg.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/preprocess/image.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/preprocess/video.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/util/__init__.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/util/average_meter.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/util/bimap.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/util/dispatched_misc.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/util/dispatcher.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/util/downloader.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/util/eventbus/__init__.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/util/eventbus/bus.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/util/eventbus/decorator.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/util/eventbus/event.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/util/fp/__init__.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/util/fp/args.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/util/fp/array/__init__.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/util/fp/array/abstract_seq.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/util/fp/array/seq.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/util/fp/array/stream.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/util/fp/func.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/util/fp/monad/__init__.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/util/fp/monad/eval.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/util/fp/monad/monad.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/util/fp/monad/option.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/util/fp/underscore.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/util/misc.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/util/ref.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/util/registry.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/util/server.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/util/singleton.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/util/timer.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/util/type.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/util/window_merger.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/visualization/__init__.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/visualization/color.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/visualization/image_browser/__init__.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/visualization/image_browser/server.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/visualization/image_browser/web/index.html +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/visualization/matplotlib.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/visualization/multi_plots.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/visualization/seaborn.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/visualization/tensorboard.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/visualization/watcher/__init__.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/visualization/watcher/component.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/visualization/watcher/server.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/visualization/watcher/view.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/visualization/watcher/web/dist/assets/index.cf95019d.css +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util.egg-info/dependency_links.txt +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util.egg-info/requires.txt +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util.egg-info/top_level.txt +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/test/test_library_info.py +0 -0
- {tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/test/test_version.py +0 -0
{tensorneko_util-0.3.12 → tensorneko_util-0.3.13}/src/tensorneko_util/io/json/json_reader.py
RENAMED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import json
|
|
2
|
+
import warnings
|
|
2
3
|
from typing import Union, Optional, List
|
|
3
4
|
|
|
4
5
|
from ...util.type import T
|
|
@@ -6,8 +7,19 @@ from ...util.type import T
|
|
|
6
7
|
|
|
7
8
|
class JsonReader:
|
|
8
9
|
|
|
10
|
+
@staticmethod
|
|
11
|
+
def _read_json(path: str, encoding: str = "UTF-8", fast: bool = False) -> Union[dict, list]:
|
|
12
|
+
if not fast:
|
|
13
|
+
with open(path, "r", encoding=encoding) as file:
|
|
14
|
+
return json.load(file)
|
|
15
|
+
else:
|
|
16
|
+
import orjson
|
|
17
|
+
with open(path, "rb") as file:
|
|
18
|
+
return orjson.loads(file.read())
|
|
19
|
+
|
|
9
20
|
@classmethod
|
|
10
|
-
def of(cls, path: str, clazz: Optional[T] = None, encoding: str = "UTF-8"
|
|
21
|
+
def of(cls, path: str, clazz: Optional[T] = None, encoding: str = "UTF-8", fast: bool = True
|
|
22
|
+
) -> Union[T, dict, list]:
|
|
11
23
|
"""
|
|
12
24
|
Read json files to ``list`` or ``dict``.
|
|
13
25
|
|
|
@@ -16,14 +28,23 @@ class JsonReader:
|
|
|
16
28
|
clazz: (``T``, optional): The object of the json read for. The type should be decorated by
|
|
17
29
|
:func:`json_data`. This should be ``T`` or ``List[T]`` or ``List[List[T]]``
|
|
18
30
|
encoding (``str``, optional): The encoding for python ``open`` function. Default: "UTF-8"
|
|
31
|
+
fast (``bool``, optional): Whether to use faster `orjson`. If `orjson` is not installed, use
|
|
32
|
+
`json` library. Default: True
|
|
19
33
|
|
|
20
34
|
Returns:
|
|
21
35
|
``dict`` | ``list`` | ``object``: The object of given json.
|
|
22
36
|
"""
|
|
37
|
+
if fast:
|
|
38
|
+
try:
|
|
39
|
+
import orjson
|
|
40
|
+
except ImportError:
|
|
41
|
+
fast = False
|
|
42
|
+
warnings.warn("orjson is not installed, will use json lib instead.")
|
|
43
|
+
assert encoding == "UTF-8", "orjson only supports UTF-8 encoding."
|
|
44
|
+
|
|
23
45
|
if clazz is not None:
|
|
24
46
|
if "typing.List[" in str(clazz):
|
|
25
|
-
|
|
26
|
-
obj: list = json.loads(file.read())
|
|
47
|
+
obj = cls._read_json(path, encoding, fast)
|
|
27
48
|
|
|
28
49
|
inner_type = clazz.__args__[0]
|
|
29
50
|
if "typing.List[" in str(inner_type):
|
|
@@ -44,17 +65,16 @@ class JsonReader:
|
|
|
44
65
|
if is_json_data:
|
|
45
66
|
obj = list(map(inner_type, obj))
|
|
46
67
|
else:
|
|
47
|
-
|
|
48
|
-
obj = clazz(json.loads(file.read()))
|
|
68
|
+
obj = clazz(cls._read_json(path, encoding, fast))
|
|
49
69
|
else:
|
|
50
|
-
|
|
51
|
-
obj = json.loads(file.read())
|
|
70
|
+
obj = cls._read_json(path, encoding, fast)
|
|
52
71
|
|
|
53
72
|
return obj
|
|
54
73
|
|
|
55
74
|
|
|
56
75
|
@classmethod
|
|
57
|
-
def of_jsonl(cls, path: str, clazz: Optional[T] = None, encoding: str = "UTF-8"
|
|
76
|
+
def of_jsonl(cls, path: str, clazz: Optional[T] = None, encoding: str = "UTF-8", fast: bool = True
|
|
77
|
+
) -> List[Union[T, dict, list]]:
|
|
58
78
|
"""
|
|
59
79
|
Read jsonl files to ``list`` or ``dict``.
|
|
60
80
|
|
|
@@ -63,14 +83,27 @@ class JsonReader:
|
|
|
63
83
|
clazz: (``T``, optional): The object of the jsonl read for. The type should be decorated by
|
|
64
84
|
:func:`json_data`. This should be ``T`` or ``List[T]`` or ``List[List[T]]``
|
|
65
85
|
encoding (``str``, optional): The encoding for python ``open`` function. Default: "UTF-8"
|
|
86
|
+
fast (``bool``, optional): Whether to use faster `orjson`. If `orjson` is not installed, use
|
|
87
|
+
`json` library. Default: True
|
|
66
88
|
|
|
67
89
|
Returns:
|
|
68
90
|
``List[dict]`` | ``List[list]`` | ``List[T]``: The object of given jsonl.
|
|
69
91
|
"""
|
|
92
|
+
if fast:
|
|
93
|
+
try:
|
|
94
|
+
import orjson
|
|
95
|
+
except ImportError:
|
|
96
|
+
fast = False
|
|
97
|
+
warnings.warn("orjson is not installed, will use json lib instead.")
|
|
98
|
+
assert encoding == "UTF-8", "orjson only supports UTF-8 encoding."
|
|
99
|
+
|
|
70
100
|
if clazz is not None:
|
|
71
101
|
if "typing.List[" in str(clazz):
|
|
72
102
|
with open(path, "r", encoding=encoding) as file:
|
|
73
|
-
|
|
103
|
+
if fast:
|
|
104
|
+
obj: list = [orjson.loads(line) for line in file]
|
|
105
|
+
else:
|
|
106
|
+
obj: list = [json.loads(line) for line in file]
|
|
74
107
|
|
|
75
108
|
inner_type = clazz.__args__[0]
|
|
76
109
|
if "typing.List[" in str(inner_type):
|
|
@@ -92,14 +125,21 @@ class JsonReader:
|
|
|
92
125
|
obj = list(map(inner_type, obj))
|
|
93
126
|
else:
|
|
94
127
|
with open(path, "r", encoding=encoding) as file:
|
|
95
|
-
|
|
128
|
+
if fast:
|
|
129
|
+
obj: list = [orjson.loads(line) for line in file]
|
|
130
|
+
else:
|
|
131
|
+
obj: list = [json.loads(line) for line in file]
|
|
132
|
+
obj = clazz(obj)
|
|
96
133
|
else:
|
|
97
134
|
with open(path, "r", encoding=encoding) as file:
|
|
98
|
-
|
|
135
|
+
if fast:
|
|
136
|
+
obj: list = [orjson.loads(line) for line in file]
|
|
137
|
+
else:
|
|
138
|
+
obj: list = [json.loads(line) for line in file]
|
|
99
139
|
|
|
100
140
|
return obj
|
|
101
141
|
|
|
102
|
-
def __new__(cls, path: str, clazz: T = None, encoding: str = "UTF-8") -> Union[T, dict, list]:
|
|
142
|
+
def __new__(cls, path: str, clazz: T = None, encoding: str = "UTF-8", fast: bool = True) -> Union[T, dict, list]:
|
|
103
143
|
"""
|
|
104
144
|
Read json or jsonl file smartly.
|
|
105
145
|
|
|
@@ -108,11 +148,13 @@ class JsonReader:
|
|
|
108
148
|
clazz: (``T``, optional): The object of the json read for. The type should be decorated by
|
|
109
149
|
:func:`json_data`. This should be ``T`` or ``List[T]`` or ``List[List[T]]``
|
|
110
150
|
encoding (``str``, optional): The encoding for python ``open`` function. Default: "UTF-8"
|
|
151
|
+
fast (``bool``, optional): Whether to use faster `orjson`. If `orjson` is not installed, use
|
|
152
|
+
`json` library. Default: True
|
|
111
153
|
|
|
112
154
|
Returns:
|
|
113
155
|
``dict`` | ``list`` | ``object``: The object of given json or jsonl.
|
|
114
156
|
"""
|
|
115
157
|
if path.endswith(".jsonl"):
|
|
116
|
-
return cls.of_jsonl(path, clazz, encoding)
|
|
158
|
+
return cls.of_jsonl(path, clazz, encoding, fast)
|
|
117
159
|
else:
|
|
118
|
-
return cls.of(path, clazz, encoding)
|
|
160
|
+
return cls.of(path, clazz, encoding, fast)
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import warnings
|
|
3
|
+
from typing import Union
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class JsonWriter:
|
|
7
|
+
|
|
8
|
+
@staticmethod
|
|
9
|
+
def _write_json(path: str, obj: Union[dict, list], encoding: str = "UTF-8", indent: int = 4,
|
|
10
|
+
ensure_ascii: bool = False, fast: bool = True
|
|
11
|
+
) -> None:
|
|
12
|
+
if not fast:
|
|
13
|
+
with open(path, "w", encoding=encoding) as file:
|
|
14
|
+
json.dump(obj, file, indent=indent, ensure_ascii=ensure_ascii)
|
|
15
|
+
else:
|
|
16
|
+
import orjson
|
|
17
|
+
with open(path, "w", encoding=encoding) as file:
|
|
18
|
+
if indent == 4:
|
|
19
|
+
option = orjson.OPT_INDENT_4
|
|
20
|
+
elif indent == 2:
|
|
21
|
+
option = orjson.OPT_INDENT_2
|
|
22
|
+
else:
|
|
23
|
+
option = None
|
|
24
|
+
file.write(orjson.dumps(obj, option=option))
|
|
25
|
+
|
|
26
|
+
@classmethod
|
|
27
|
+
def to(cls, path: str, obj: Union[dict, list, object], encoding: str = "UTF-8", indent: int = 4,
|
|
28
|
+
ensure_ascii: bool = False, fast: bool = True
|
|
29
|
+
) -> None:
|
|
30
|
+
"""
|
|
31
|
+
Save as Json file from a dictionary, list or json_dict.
|
|
32
|
+
|
|
33
|
+
Args:
|
|
34
|
+
path (``str``): The path of output file.
|
|
35
|
+
obj (``dict`` | ``list`` | ``object``): The json data which need to be used for output. The type should be
|
|
36
|
+
``dict``, ``list`` or the ``object`` decorated by :func:`~tensorneko.io.text.text_reader.json_data`.
|
|
37
|
+
encoding (``str``, optional): Python file IO encoding parameter. Default: "UTF-8".
|
|
38
|
+
indent (``int``, optional): Python json dump indent parameter. Default: 4.
|
|
39
|
+
ensure_ascii (``bool``, optional): Python json dump ensure_ascii parameter. Default: False.
|
|
40
|
+
fast (``bool``, optional): Whether to use faster `orjson`. If `orjson` is not installed, use
|
|
41
|
+
`json` library. Default: True
|
|
42
|
+
"""
|
|
43
|
+
if fast:
|
|
44
|
+
try:
|
|
45
|
+
import orjson
|
|
46
|
+
except ImportError:
|
|
47
|
+
fast = False
|
|
48
|
+
warnings.warn("orjson is not installed, will use json lib instead.")
|
|
49
|
+
assert encoding == "UTF-8", "orjson only supports UTF-8 encoding."
|
|
50
|
+
assert indent in (0, 2, 4, None), "Only support indent 2, 4 or 0/None."
|
|
51
|
+
assert ensure_ascii is False, "orjson does not support ensure_ascii."
|
|
52
|
+
|
|
53
|
+
if type(obj) is dict:
|
|
54
|
+
cls._write_json(path, obj, encoding, indent, ensure_ascii, fast)
|
|
55
|
+
elif "is_json_data" in type(obj).__dict__ and type(obj).is_json_data:
|
|
56
|
+
cls._write_json(path, obj.to_dict(), encoding, indent, ensure_ascii, fast)
|
|
57
|
+
elif type(obj) is list:
|
|
58
|
+
if len(obj) == 0 or type(obj[0]) in (dict, list):
|
|
59
|
+
cls._write_json(path, obj, encoding, indent, ensure_ascii, fast)
|
|
60
|
+
elif "is_json_data" in type(obj[0]).__dict__ and type(obj[0]).is_json_data:
|
|
61
|
+
obj = [each.to_dict() for each in obj]
|
|
62
|
+
cls._write_json(path, obj, encoding, indent, ensure_ascii, fast)
|
|
63
|
+
else:
|
|
64
|
+
raise TypeError("Not implemented type. Only support dict, list, json_data.")
|
|
65
|
+
|
|
66
|
+
def __new__(cls, path: str, obj: Union[dict, list, object], encoding: str = "UTF-8", indent: int = 4,
|
|
67
|
+
ensure_ascii: bool = False, fast: bool = True
|
|
68
|
+
) -> None:
|
|
69
|
+
"""Alias of :meth:`~tensorneko.io.json.json_writer.JsonWriter.to`."""
|
|
70
|
+
cls.to(path, obj, encoding, indent, ensure_ascii, fast)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
0.3.13
|