pelutils 3.8.2__tar.gz → 3.8.4__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.
- {pelutils-3.8.2 → pelutils-3.8.4}/PKG-INFO +11 -10
- {pelutils-3.8.2 → pelutils-3.8.4}/README.md +10 -9
- {pelutils-3.8.2 → pelutils-3.8.4}/pelutils/__version__.py +1 -1
- {pelutils-3.8.2 → pelutils-3.8.4}/pelutils/datastorage2/__init__.py +18 -6
- {pelutils-3.8.2 → pelutils-3.8.4}/pelutils.egg-info/PKG-INFO +11 -10
- {pelutils-3.8.2 → pelutils-3.8.4}/MANIFEST.in +0 -0
- {pelutils-3.8.2 → pelutils-3.8.4}/pelutils/__init__.py +0 -0
- {pelutils-3.8.2 → pelutils-3.8.4}/pelutils/_c/__init__.py +0 -0
- {pelutils-3.8.2 → pelutils-3.8.4}/pelutils/_c/ds.c +0 -0
- {pelutils-3.8.2 → pelutils-3.8.4}/pelutils/_c/hashmap.c/LICENSE +0 -0
- {pelutils-3.8.2 → pelutils-3.8.4}/pelutils/_c/hashmap.c/hashmap.c +0 -0
- {pelutils-3.8.2 → pelutils-3.8.4}/pelutils/_c/hashmap.c/hashmap.h +0 -0
- {pelutils-3.8.2 → pelutils-3.8.4}/pelutils/_entry_points/__init__.py +0 -0
- {pelutils-3.8.2 → pelutils-3.8.4}/pelutils/_entry_points/linecounter.py +0 -0
- {pelutils-3.8.2 → pelutils-3.8.4}/pelutils/datastorage.py +0 -0
- {pelutils-3.8.2 → pelutils-3.8.4}/pelutils/datastorage2/_pretty_json.py +0 -0
- {pelutils-3.8.2 → pelutils-3.8.4}/pelutils/ds/__init__.py +0 -0
- {pelutils-3.8.2 → pelutils-3.8.4}/pelutils/ds/distributions.py +0 -0
- {pelutils-3.8.2 → pelutils-3.8.4}/pelutils/ds/plots.py +0 -0
- {pelutils-3.8.2 → pelutils-3.8.4}/pelutils/ds/stats.py +0 -0
- {pelutils-3.8.2 → pelutils-3.8.4}/pelutils/format.py +0 -0
- {pelutils-3.8.2 → pelutils-3.8.4}/pelutils/jsonl.py +0 -0
- {pelutils-3.8.2 → pelutils-3.8.4}/pelutils/logging/__init__.py +0 -0
- {pelutils-3.8.2 → pelutils-3.8.4}/pelutils/logging/_rotate.py +0 -0
- {pelutils-3.8.2 → pelutils-3.8.4}/pelutils/logging/_utils.py +0 -0
- {pelutils-3.8.2 → pelutils-3.8.4}/pelutils/parser.py +0 -0
- {pelutils-3.8.2 → pelutils-3.8.4}/pelutils/pretty_json.py +0 -0
- {pelutils-3.8.2 → pelutils-3.8.4}/pelutils/py.typed +0 -0
- {pelutils-3.8.2 → pelutils-3.8.4}/pelutils/tests.py +0 -0
- {pelutils-3.8.2 → pelutils-3.8.4}/pelutils/ticktock.py +0 -0
- {pelutils-3.8.2 → pelutils-3.8.4}/pelutils/types.py +0 -0
- {pelutils-3.8.2 → pelutils-3.8.4}/pelutils.egg-info/SOURCES.txt +0 -0
- {pelutils-3.8.2 → pelutils-3.8.4}/pelutils.egg-info/dependency_links.txt +0 -0
- {pelutils-3.8.2 → pelutils-3.8.4}/pelutils.egg-info/entry_points.txt +0 -0
- {pelutils-3.8.2 → pelutils-3.8.4}/pelutils.egg-info/requires.txt +0 -0
- {pelutils-3.8.2 → pelutils-3.8.4}/pelutils.egg-info/top_level.txt +0 -0
- {pelutils-3.8.2 → pelutils-3.8.4}/setup.cfg +0 -0
- {pelutils-3.8.2 → pelutils-3.8.4}/setup.py +0 -0
- {pelutils-3.8.2 → pelutils-3.8.4}/tests/test_datastorage.py +0 -0
- {pelutils-3.8.2 → pelutils-3.8.4}/tests/test_format.py +0 -0
- {pelutils-3.8.2 → pelutils-3.8.4}/tests/test_init.py +0 -0
- {pelutils-3.8.2 → pelutils-3.8.4}/tests/test_jsonl.py +0 -0
- {pelutils-3.8.2 → pelutils-3.8.4}/tests/test_parser.py +0 -0
- {pelutils-3.8.2 → pelutils-3.8.4}/tests/test_pretty_json.py +0 -0
- {pelutils-3.8.2 → pelutils-3.8.4}/tests/test_tests.py +0 -0
- {pelutils-3.8.2 → pelutils-3.8.4}/tests/test_ticktock.py +0 -0
- {pelutils-3.8.2 → pelutils-3.8.4}/tests/test_types.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: pelutils
|
|
3
|
-
Version: 3.8.
|
|
3
|
+
Version: 3.8.4
|
|
4
4
|
Summary: The Swiss army knife of Python projects.
|
|
5
5
|
Home-page: https://github.com/peleiden/pelutils
|
|
6
6
|
Download-URL: https://pypi.org/project/pelutils/
|
|
@@ -90,15 +90,17 @@ while True:
|
|
|
90
90
|
|
|
91
91
|
## Data Serialisation
|
|
92
92
|
|
|
93
|
-
The `DataStorage2` class is an extension of `pydantic.BaseModel`
|
|
94
|
-
It
|
|
95
|
-
|
|
96
|
-
Any nested `BaseModel` is automatically converted to a dictionary. If a data type is reached which is not
|
|
97
|
-
JSON serialisable (e.g. normal classes, pandas DataFramas, numpy arrays), they are encoded with `pickle`
|
|
98
|
-
and `base64`. Inside the JSON file, a record is kept of the original type.
|
|
93
|
+
The `DataStorage2` class is an extension of `pydantic.BaseModel` which adds convenient `safe` and `load` methods for trivial saving to and loading from JSON files.
|
|
94
|
+
It can serialise *any* attribute on the model to JSON by pickling types which `pydantic` cannot natively serialise.
|
|
95
|
+
To get use it, simply inherit from `DataStorage2`, like you would inherit from `BaseModel`.
|
|
99
96
|
|
|
100
|
-
|
|
101
|
-
|
|
97
|
+
The produced JSON files take advantage of `pelutils.pretty_json(...)` to ensure good and human-readable formatting, almost no matter the data types.
|
|
98
|
+
Very long lists utilise the full allowed line length before splitting.
|
|
99
|
+
This prevents the common JSON issue of having either very long lines (no indents) or excessively many lines with a single, small element on each.
|
|
100
|
+
|
|
101
|
+
It is possible to get only the serialised JSON objects as dicts instead of saving them directly to a file with the `DataStorage2.model_safe_dump()` method.
|
|
102
|
+
This is practical if you want to nest the objects into another dict or list.
|
|
103
|
+
`DataStorage2.model_safe_load(...)` does the inverse operation by creating a class instance from a JSON-serialised dict.
|
|
102
104
|
|
|
103
105
|
Because the class inherits from `pydantic.BaseModel`, type checking is built directly into it.
|
|
104
106
|
|
|
@@ -154,7 +156,6 @@ This will produce `directory/to/save/in/BasedClass.json` with the following cont
|
|
|
154
156
|
"baseder": {"based_string": "Hello there"}
|
|
155
157
|
}
|
|
156
158
|
```
|
|
157
|
-
The class is built on top of the also provided `pretty_json` which does the exact same as the built-in `json.dumps` but with prettier formatting of the JSON string.
|
|
158
159
|
|
|
159
160
|
## Config and Command-line Argument Parsing
|
|
160
161
|
|
|
@@ -65,15 +65,17 @@ while True:
|
|
|
65
65
|
|
|
66
66
|
## Data Serialisation
|
|
67
67
|
|
|
68
|
-
The `DataStorage2` class is an extension of `pydantic.BaseModel`
|
|
69
|
-
It
|
|
70
|
-
|
|
71
|
-
Any nested `BaseModel` is automatically converted to a dictionary. If a data type is reached which is not
|
|
72
|
-
JSON serialisable (e.g. normal classes, pandas DataFramas, numpy arrays), they are encoded with `pickle`
|
|
73
|
-
and `base64`. Inside the JSON file, a record is kept of the original type.
|
|
68
|
+
The `DataStorage2` class is an extension of `pydantic.BaseModel` which adds convenient `safe` and `load` methods for trivial saving to and loading from JSON files.
|
|
69
|
+
It can serialise *any* attribute on the model to JSON by pickling types which `pydantic` cannot natively serialise.
|
|
70
|
+
To get use it, simply inherit from `DataStorage2`, like you would inherit from `BaseModel`.
|
|
74
71
|
|
|
75
|
-
|
|
76
|
-
|
|
72
|
+
The produced JSON files take advantage of `pelutils.pretty_json(...)` to ensure good and human-readable formatting, almost no matter the data types.
|
|
73
|
+
Very long lists utilise the full allowed line length before splitting.
|
|
74
|
+
This prevents the common JSON issue of having either very long lines (no indents) or excessively many lines with a single, small element on each.
|
|
75
|
+
|
|
76
|
+
It is possible to get only the serialised JSON objects as dicts instead of saving them directly to a file with the `DataStorage2.model_safe_dump()` method.
|
|
77
|
+
This is practical if you want to nest the objects into another dict or list.
|
|
78
|
+
`DataStorage2.model_safe_load(...)` does the inverse operation by creating a class instance from a JSON-serialised dict.
|
|
77
79
|
|
|
78
80
|
Because the class inherits from `pydantic.BaseModel`, type checking is built directly into it.
|
|
79
81
|
|
|
@@ -129,7 +131,6 @@ This will produce `directory/to/save/in/BasedClass.json` with the following cont
|
|
|
129
131
|
"baseder": {"based_string": "Hello there"}
|
|
130
132
|
}
|
|
131
133
|
```
|
|
132
|
-
The class is built on top of the also provided `pretty_json` which does the exact same as the built-in `json.dumps` but with prettier formatting of the JSON string.
|
|
133
134
|
|
|
134
135
|
## Config and Command-line Argument Parsing
|
|
135
136
|
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
# Do not put anything else in this file
|
|
2
|
-
__version__ = "3.8.
|
|
2
|
+
__version__ = "3.8.4"
|
|
@@ -2,7 +2,7 @@ from __future__ import annotations
|
|
|
2
2
|
|
|
3
3
|
import json
|
|
4
4
|
from pathlib import Path
|
|
5
|
-
from typing import TypeVar
|
|
5
|
+
from typing import Any, TypeVar
|
|
6
6
|
|
|
7
7
|
from pydantic import BaseModel, ConfigDict
|
|
8
8
|
|
|
@@ -24,6 +24,19 @@ class DataStorage2(BaseModel):
|
|
|
24
24
|
|
|
25
25
|
model_config = ConfigDict(arbitrary_types_allowed=True)
|
|
26
26
|
|
|
27
|
+
def model_safe_dump(self) -> dict[str, Any]: # pyright: ignore[reportExplicitAny]
|
|
28
|
+
"""Dump the model as a dictionary where pickled objects have been converted to b64 strings.
|
|
29
|
+
|
|
30
|
+
This is very similar to `self.model_dump(mode="json")` but it handles non-JSON-serialisable types.
|
|
31
|
+
"""
|
|
32
|
+
return self.model_dump(mode="json", fallback=_pickle_encode)
|
|
33
|
+
|
|
34
|
+
@classmethod
|
|
35
|
+
def model_safe_load(cls: type[_T], safe_json: dict[str, Any]) -> _T: # pyright: ignore[reportExplicitAny]
|
|
36
|
+
"""Build an instance from a safe JSON. This method is the inverse of `model_safe_dump`."""
|
|
37
|
+
self_dict = _make_json_unsafe(safe_json)
|
|
38
|
+
return cls.model_validate(self_dict)
|
|
39
|
+
|
|
27
40
|
@classmethod
|
|
28
41
|
def _resolve_save_file(cls, directory: str | Path, filename: str | None = None) -> Path:
|
|
29
42
|
if filename is None:
|
|
@@ -35,13 +48,13 @@ class DataStorage2(BaseModel):
|
|
|
35
48
|
) -> Path:
|
|
36
49
|
"""Save the instance to a json file in the directory. The path to the file is returned.
|
|
37
50
|
|
|
38
|
-
If save_name is None, the path is <directory>/<class name>.json, otherwise it is <directory>/<
|
|
51
|
+
If save_name is None, the path is <directory>/<class name>.json, otherwise it is <directory>/<filename>.json.
|
|
39
52
|
"""
|
|
40
53
|
savepath = self._resolve_save_file(directory, filename)
|
|
41
|
-
|
|
54
|
+
savepath.parent.mkdir(parents=True, exist_ok=True)
|
|
42
55
|
savepath.write_text(
|
|
43
56
|
_pretty_json(
|
|
44
|
-
|
|
57
|
+
self.model_safe_dump(),
|
|
45
58
|
max_line_length=max_line_length,
|
|
46
59
|
indent=indent,
|
|
47
60
|
# Safe should not be needed here, as the fallback function in model_dump should ensure no issues
|
|
@@ -58,5 +71,4 @@ class DataStorage2(BaseModel):
|
|
|
58
71
|
savepath = cls._resolve_save_file(directory, filename)
|
|
59
72
|
with savepath.open(encoding=encoding) as f:
|
|
60
73
|
self_dict = json.load(f)
|
|
61
|
-
|
|
62
|
-
return cls.model_validate(self_dict)
|
|
74
|
+
return cls.model_safe_load(self_dict)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: pelutils
|
|
3
|
-
Version: 3.8.
|
|
3
|
+
Version: 3.8.4
|
|
4
4
|
Summary: The Swiss army knife of Python projects.
|
|
5
5
|
Home-page: https://github.com/peleiden/pelutils
|
|
6
6
|
Download-URL: https://pypi.org/project/pelutils/
|
|
@@ -90,15 +90,17 @@ while True:
|
|
|
90
90
|
|
|
91
91
|
## Data Serialisation
|
|
92
92
|
|
|
93
|
-
The `DataStorage2` class is an extension of `pydantic.BaseModel`
|
|
94
|
-
It
|
|
95
|
-
|
|
96
|
-
Any nested `BaseModel` is automatically converted to a dictionary. If a data type is reached which is not
|
|
97
|
-
JSON serialisable (e.g. normal classes, pandas DataFramas, numpy arrays), they are encoded with `pickle`
|
|
98
|
-
and `base64`. Inside the JSON file, a record is kept of the original type.
|
|
93
|
+
The `DataStorage2` class is an extension of `pydantic.BaseModel` which adds convenient `safe` and `load` methods for trivial saving to and loading from JSON files.
|
|
94
|
+
It can serialise *any* attribute on the model to JSON by pickling types which `pydantic` cannot natively serialise.
|
|
95
|
+
To get use it, simply inherit from `DataStorage2`, like you would inherit from `BaseModel`.
|
|
99
96
|
|
|
100
|
-
|
|
101
|
-
|
|
97
|
+
The produced JSON files take advantage of `pelutils.pretty_json(...)` to ensure good and human-readable formatting, almost no matter the data types.
|
|
98
|
+
Very long lists utilise the full allowed line length before splitting.
|
|
99
|
+
This prevents the common JSON issue of having either very long lines (no indents) or excessively many lines with a single, small element on each.
|
|
100
|
+
|
|
101
|
+
It is possible to get only the serialised JSON objects as dicts instead of saving them directly to a file with the `DataStorage2.model_safe_dump()` method.
|
|
102
|
+
This is practical if you want to nest the objects into another dict or list.
|
|
103
|
+
`DataStorage2.model_safe_load(...)` does the inverse operation by creating a class instance from a JSON-serialised dict.
|
|
102
104
|
|
|
103
105
|
Because the class inherits from `pydantic.BaseModel`, type checking is built directly into it.
|
|
104
106
|
|
|
@@ -154,7 +156,6 @@ This will produce `directory/to/save/in/BasedClass.json` with the following cont
|
|
|
154
156
|
"baseder": {"based_string": "Hello there"}
|
|
155
157
|
}
|
|
156
158
|
```
|
|
157
|
-
The class is built on top of the also provided `pretty_json` which does the exact same as the built-in `json.dumps` but with prettier formatting of the JSON string.
|
|
158
159
|
|
|
159
160
|
## Config and Command-line Argument Parsing
|
|
160
161
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|