utils.io
Utils - io¤
is_dir(path)
¤
Check if the given path is a directory
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
Union[str, Path]
|
path to be checked |
required |
Source code in kirsche/utils/io.py
27 28 29 30 31 32 33 34 35 36 37 38 |
|
load_batch_json(data_path)
¤
load data from json file(s)
If the given data_path
is a folder, all the json files in the folder are loaded. If the given data_path
is a single json file, everything inside the file will be loaded.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_path |
Union[str, Path]
|
json file path |
required |
Source code in kirsche/utils/io.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
|
load_json(data_file)
¤
load dict/list of dict data from json file
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_file |
Union[str, Path]
|
json file path |
required |
Source code in kirsche/utils/io.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
record_exists(id, existing_records, keys=UNIQUE_ID_PRECEDENCE, unique_id_prefix=UNIQUE_ID_PREFIX)
¤
Whether the record already exists in the data file
Parameters:
Name | Type | Description | Default |
---|---|---|---|
id |
json files folder path |
required |
Source code in kirsche/utils/io.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
|
save_batch_json(records, data_path, unique_key=None, mode=None)
¤
save data to json file.
There are two modes:
- single file mode, if the data_path
is a folder, and
- multi file mode, if the data_path
is a json file path.
In the single file mode, all the entries in the data are saved to the same file. In the multi file mode, each entry will be saved as a separate file.
Single file mode is good for long term presevation, and multi file mode is good for updates.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
records |
list
|
list of data to be saved |
required |
data_path |
Union[str, Path]
|
json file path or folder path |
required |
Source code in kirsche/utils/io.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
|
save_json(data, data_file)
¤
save data to json file
This Function Overwrites any Existing Content
Beware that all contents in the file will be overwritten if it exists.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
Union[dict, list]
|
dictionary data to be saved |
required |
data_file |
Union[str, Path]
|
json file path |
required |
Source code in kirsche/utils/io.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
|