PySerials 0.1.0__tar.gz → 0.1.2__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.
Files changed (24) hide show
  1. {pyserials-0.1.0 → pyserials-0.1.2}/PKG-INFO +2 -2
  2. {pyserials-0.1.0 → pyserials-0.1.2}/pyproject.toml +1 -1
  3. {pyserials-0.1.0 → pyserials-0.1.2}/src/PySerials.egg-info/PKG-INFO +2 -2
  4. {pyserials-0.1.0 → pyserials-0.1.2}/src/PySerials.egg-info/SOURCES.txt +1 -0
  5. {pyserials-0.1.0 → pyserials-0.1.2}/src/pyserials/__init__.py +1 -0
  6. pyserials-0.1.2/src/pyserials/flatten.py +51 -0
  7. {pyserials-0.1.0 → pyserials-0.1.2}/src/pyserials/write.py +24 -1
  8. {pyserials-0.1.0 → pyserials-0.1.2}/setup.cfg +0 -0
  9. {pyserials-0.1.0 → pyserials-0.1.2}/src/PySerials.egg-info/dependency_links.txt +0 -0
  10. {pyserials-0.1.0 → pyserials-0.1.2}/src/PySerials.egg-info/not-zip-safe +0 -0
  11. {pyserials-0.1.0 → pyserials-0.1.2}/src/PySerials.egg-info/requires.txt +0 -0
  12. {pyserials-0.1.0 → pyserials-0.1.2}/src/PySerials.egg-info/top_level.txt +0 -0
  13. {pyserials-0.1.0 → pyserials-0.1.2}/src/pyserials/compare.py +0 -0
  14. {pyserials-0.1.0 → pyserials-0.1.2}/src/pyserials/exception/__init__.py +0 -0
  15. {pyserials-0.1.0 → pyserials-0.1.2}/src/pyserials/exception/_base.py +0 -0
  16. {pyserials-0.1.0 → pyserials-0.1.2}/src/pyserials/exception/read.py +0 -0
  17. {pyserials-0.1.0 → pyserials-0.1.2}/src/pyserials/exception/update.py +0 -0
  18. {pyserials-0.1.0 → pyserials-0.1.2}/src/pyserials/exception/validate.py +0 -0
  19. {pyserials-0.1.0 → pyserials-0.1.2}/src/pyserials/format.py +0 -0
  20. {pyserials-0.1.0 → pyserials-0.1.2}/src/pyserials/nested_dict.py +0 -0
  21. {pyserials-0.1.0 → pyserials-0.1.2}/src/pyserials/property_dict.py +0 -0
  22. {pyserials-0.1.0 → pyserials-0.1.2}/src/pyserials/read.py +0 -0
  23. {pyserials-0.1.0 → pyserials-0.1.2}/src/pyserials/update.py +0 -0
  24. {pyserials-0.1.0 → pyserials-0.1.2}/src/pyserials/validate.py +0 -0
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.2
1
+ Metadata-Version: 2.4
2
2
  Name: PySerials
3
- Version: 0.1.0
3
+ Version: 0.1.2
4
4
  Requires-Python: >=3.10
5
5
  Requires-Dist: jsonschema<5,>=4.21.0
6
6
  Requires-Dist: referencing>=0.35.1
@@ -17,7 +17,7 @@ namespaces = true
17
17
  # ----------------------------------------- Project Metadata -------------------------------------
18
18
  #
19
19
  [project]
20
- version = "0.1.0"
20
+ version = "0.1.2"
21
21
  name = "PySerials"
22
22
  dependencies = [
23
23
  "jsonschema >= 4.21.0, < 5",
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.2
1
+ Metadata-Version: 2.4
2
2
  Name: PySerials
3
- Version: 0.1.0
3
+ Version: 0.1.2
4
4
  Requires-Python: >=3.10
5
5
  Requires-Dist: jsonschema<5,>=4.21.0
6
6
  Requires-Dist: referencing>=0.35.1
@@ -7,6 +7,7 @@ src/PySerials.egg-info/requires.txt
7
7
  src/PySerials.egg-info/top_level.txt
8
8
  src/pyserials/__init__.py
9
9
  src/pyserials/compare.py
10
+ src/pyserials/flatten.py
10
11
  src/pyserials/format.py
11
12
  src/pyserials/nested_dict.py
12
13
  src/pyserials/property_dict.py
@@ -3,3 +3,4 @@
3
3
  from pyserials import exception, update, validate, read, write, format, compare
4
4
  from pyserials.nested_dict import NestedDict
5
5
  from pyserials.property_dict import PropertyDict
6
+ from pyserials.flatten import flatten
@@ -0,0 +1,51 @@
1
+ def flatten(data: dict | list[dict]) -> dict | list[dict]:
2
+ """Flatten a nested dictionary into a single-level dictionary.
3
+
4
+ Parameters
5
+ ----------
6
+ data
7
+ A dictionary or a list of dictionaries to flatten.
8
+
9
+ Returns
10
+ -------
11
+ If `data` is a dictionary, returns a flattened dictionary.
12
+ If `data` is a list of dictionaries, returns a list of flattened dictionaries.
13
+ """
14
+ if isinstance(data, dict):
15
+ return _flatten(data)
16
+ if isinstance(data, list):
17
+ if not data:
18
+ return []
19
+ if not all(isinstance(item, dict) for item in data):
20
+ raise ValueError("All items in the list must be dictionaries.")
21
+ return [_flatten(item) for item in data]
22
+ raise ValueError("Input must be a dictionary or a list of dictionaries.")
23
+
24
+
25
+ def _flatten(data, output: dict | None = None, path: str | None = None):
26
+ output = output if output is not None else {}
27
+ if isinstance(data, str | int | float | bool | None):
28
+ output[path] = data
29
+ return output
30
+ if isinstance(data, dict):
31
+ for key, value in data.items():
32
+ new_path = _flatten_key(path, key)
33
+ _flatten(data=value, output=output, path=new_path)
34
+ return output
35
+ if isinstance(data, list):
36
+ if len(data) == 0:
37
+ output[path] = None
38
+ return output
39
+ if all(isinstance(val, str | int | float | bool | None) for val in data):
40
+ output[path] = data[0] if len(data) == 1 else data
41
+ return output
42
+ if len(data) != 1:
43
+ raise ValueError(f"Data at {path} is a list with more than one non-primitive element: {data}")
44
+ return _flatten(data=data[0], output=output, path=path)
45
+ raise ValueError(f"Unknown data type {type(data)} at {path}")
46
+
47
+
48
+ def _flatten_key(path: str | None, key: str):
49
+ if path:
50
+ return f"{path}.{key}"
51
+ return key
@@ -27,7 +27,7 @@ def to_string(
27
27
  return to_json_string(data, sort_keys=sort_keys, indent=indent, default=default, end_of_file_newline=end_of_file_newline)
28
28
  if data_type == "yaml":
29
29
  return to_yaml_string(
30
- data,
30
+ data,
31
31
  end_of_file_newline=end_of_file_newline,
32
32
  indent_mapping=indent_mapping,
33
33
  indent_sequence=indent_sequence,
@@ -80,6 +80,29 @@ def to_json_string(
80
80
  return f"{string.rstrip("\n")}\n" if end_of_file_newline else string
81
81
 
82
82
 
83
+ def to_json_file(
84
+ data: dict | list | str | int | float | bool | _yaml.CommentedMap | _yaml.CommentedSeq,
85
+ path: str | _Path,
86
+ sort_keys: bool = False,
87
+ indent: int | None = None,
88
+ default: Callable[[Any], Any] | None = None,
89
+ end_of_file_newline: bool = True,
90
+ make_dirs: bool = True,
91
+ ) -> None:
92
+ json_string = to_json_string(
93
+ data,
94
+ sort_keys=sort_keys,
95
+ indent=indent,
96
+ default=default,
97
+ end_of_file_newline=end_of_file_newline,
98
+ )
99
+ path = _Path(path).resolve()
100
+ if make_dirs:
101
+ path.parent.mkdir(parents=True, exist_ok=True)
102
+ path.write_text(json_string)
103
+ return
104
+
105
+
83
106
  def to_yaml_file(
84
107
  data: dict | list | str | int | float | bool | _yaml.CommentedMap | _yaml.CommentedSeq,
85
108
  path: str | _Path,
File without changes