stouputils 1.3.24__tar.gz → 1.4.0__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 (107) hide show
  1. {stouputils-1.3.24 → stouputils-1.4.0}/.gitignore +1 -0
  2. {stouputils-1.3.24 → stouputils-1.4.0}/PKG-INFO +2 -1
  3. {stouputils-1.3.24 → stouputils-1.4.0}/pyproject.toml +2 -2
  4. stouputils-1.4.0/stouputils/collections.py +157 -0
  5. stouputils-1.3.24/stouputils/collections.py +0 -62
  6. {stouputils-1.3.24 → stouputils-1.4.0}/LICENSE +0 -0
  7. {stouputils-1.3.24 → stouputils-1.4.0}/README.md +0 -0
  8. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/__init__.py +0 -0
  9. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/all_doctests.py +0 -0
  10. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/applications/__init__.py +0 -0
  11. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/applications/automatic_docs.py +0 -0
  12. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/applications/upscaler/__init__.py +0 -0
  13. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/applications/upscaler/config.py +0 -0
  14. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/applications/upscaler/image.py +0 -0
  15. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/applications/upscaler/video.py +0 -0
  16. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/archive.py +0 -0
  17. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/backup.py +0 -0
  18. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/continuous_delivery/__init__.py +0 -0
  19. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/continuous_delivery/cd_utils.py +0 -0
  20. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/continuous_delivery/github.py +0 -0
  21. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/continuous_delivery/pypi.py +0 -0
  22. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/continuous_delivery/pyproject.py +0 -0
  23. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/ctx.py +0 -0
  24. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/config/get.py +0 -0
  25. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/config/set.py +0 -0
  26. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/data_processing/image/__init__.py +0 -0
  27. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/data_processing/image/auto_contrast.py +0 -0
  28. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/data_processing/image/axis_flip.py +0 -0
  29. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/data_processing/image/bias_field_correction.py +0 -0
  30. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/data_processing/image/binary_threshold.py +0 -0
  31. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/data_processing/image/blur.py +0 -0
  32. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/data_processing/image/brightness.py +0 -0
  33. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/data_processing/image/canny.py +0 -0
  34. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/data_processing/image/clahe.py +0 -0
  35. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/data_processing/image/common.py +0 -0
  36. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/data_processing/image/contrast.py +0 -0
  37. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/data_processing/image/curvature_flow_filter.py +0 -0
  38. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/data_processing/image/denoise.py +0 -0
  39. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/data_processing/image/histogram_equalization.py +0 -0
  40. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/data_processing/image/invert.py +0 -0
  41. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/data_processing/image/laplacian.py +0 -0
  42. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/data_processing/image/median_blur.py +0 -0
  43. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/data_processing/image/noise.py +0 -0
  44. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/data_processing/image/normalize.py +0 -0
  45. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/data_processing/image/random_erase.py +0 -0
  46. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/data_processing/image/resize.py +0 -0
  47. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/data_processing/image/rotation.py +0 -0
  48. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/data_processing/image/salt_pepper.py +0 -0
  49. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/data_processing/image/sharpening.py +0 -0
  50. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/data_processing/image/shearing.py +0 -0
  51. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/data_processing/image/threshold.py +0 -0
  52. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/data_processing/image/translation.py +0 -0
  53. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/data_processing/image/zoom.py +0 -0
  54. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/data_processing/image_augmentation.py +0 -0
  55. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/data_processing/image_preprocess.py +0 -0
  56. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/data_processing/prosthesis_detection.py +0 -0
  57. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/data_processing/technique.py +0 -0
  58. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/dataset/__init__.py +0 -0
  59. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/dataset/dataset.py +0 -0
  60. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/dataset/dataset_loader.py +0 -0
  61. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/dataset/grouping_strategy.py +0 -0
  62. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/dataset/image_loader.py +0 -0
  63. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/dataset/xy_tuple.py +0 -0
  64. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/metric_dictionnary.py +0 -0
  65. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/metric_utils.py +0 -0
  66. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/mlflow_utils.py +0 -0
  67. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/models/abstract_model.py +0 -0
  68. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/models/all.py +0 -0
  69. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/models/base_keras.py +0 -0
  70. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/models/keras/all.py +0 -0
  71. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/models/keras/convnext.py +0 -0
  72. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/models/keras/densenet.py +0 -0
  73. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/models/keras/efficientnet.py +0 -0
  74. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/models/keras/mobilenet.py +0 -0
  75. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/models/keras/resnet.py +0 -0
  76. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/models/keras/squeezenet.py +0 -0
  77. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/models/keras/vgg.py +0 -0
  78. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/models/keras/xception.py +0 -0
  79. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/models/keras_utils/callbacks/__init__.py +0 -0
  80. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/models/keras_utils/callbacks/colored_progress_bar.py +0 -0
  81. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/models/keras_utils/callbacks/learning_rate_finder.py +0 -0
  82. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/models/keras_utils/callbacks/model_checkpoint_v2.py +0 -0
  83. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/models/keras_utils/callbacks/progressive_unfreezing.py +0 -0
  84. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/models/keras_utils/callbacks/warmup_scheduler.py +0 -0
  85. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/models/keras_utils/losses/__init__.py +0 -0
  86. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/models/keras_utils/losses/next_generation_loss.py +0 -0
  87. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/models/keras_utils/visualizations.py +0 -0
  88. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/models/model_interface.py +0 -0
  89. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/models/sandbox.py +0 -0
  90. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/range_tuple.py +0 -0
  91. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/scripts/augment_dataset.py +0 -0
  92. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/scripts/exhaustive_process.py +0 -0
  93. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/scripts/preprocess_dataset.py +0 -0
  94. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/scripts/routine.py +0 -0
  95. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/data_science/utils.py +0 -0
  96. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/decorators.py +0 -0
  97. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/image.py +0 -0
  98. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/installer/__init__.py +0 -0
  99. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/installer/common.py +0 -0
  100. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/installer/downloader.py +0 -0
  101. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/installer/linux.py +0 -0
  102. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/installer/main.py +0 -0
  103. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/installer/windows.py +0 -0
  104. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/io.py +0 -0
  105. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/parallel.py +0 -0
  106. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/print.py +0 -0
  107. {stouputils-1.3.24 → stouputils-1.4.0}/stouputils/py.typed +0 -0
@@ -1,6 +1,7 @@
1
1
 
2
2
  # Python
3
3
  *__pycache__/
4
+ uv.lock
4
5
 
5
6
  # Miscellaneous
6
7
  __temporary__/
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: stouputils
3
- Version: 1.3.24
3
+ Version: 1.4.0
4
4
  Summary: Stouputils is a collection of utility modules designed to simplify and enhance the development process. It includes a range of tools for tasks such as execution of doctests, display utilities, decorators, as well as context managers, and many more.
5
5
  Project-URL: Homepage, https://github.com/Stoupy51/stouputils
6
6
  Project-URL: Issues, https://github.com/Stoupy51/stouputils/issues
@@ -20,6 +20,7 @@ Requires-Dist: pyyaml>=6.0.0
20
20
  Requires-Dist: requests>=2.20.0
21
21
  Requires-Dist: toml>=0.10.0
22
22
  Requires-Dist: tqdm>=4.0.0
23
+ Requires-Dist: zarr>=2.18.3
23
24
  Description-Content-Type: text/markdown
24
25
 
25
26
  # 🛠️ Project Badges
@@ -5,7 +5,7 @@ build-backend = "hatchling.build"
5
5
 
6
6
  [project]
7
7
  name = "stouputils"
8
- version = "1.3.24"
8
+ version = "1.4.0"
9
9
  description = "Stouputils is a collection of utility modules designed to simplify and enhance the development process. It includes a range of tools for tasks such as execution of doctests, display utilities, decorators, as well as context managers, and many more."
10
10
  readme = "README.md"
11
11
  requires-python = ">=3.10"
@@ -14,7 +14,7 @@ classifiers = [
14
14
  "License :: OSI Approved :: MIT License",
15
15
  "Operating System :: OS Independent",
16
16
  ]
17
- dependencies = [ "tqdm>=4.0.0", "requests>=2.20.0", "pyyaml>=6.0.0", "toml>=0.10.0", "pillow>=10.0.0", "numpy", "opencv-python>=4.0.0", "orjson>=3.0.0", "pyfastcopy>=1.0.0", "python-box[all]>=7.0.0",]
17
+ dependencies = [ "tqdm>=4.0.0", "requests>=2.20.0", "pyyaml>=6.0.0", "toml>=0.10.0", "pillow>=10.0.0", "numpy", "opencv-python>=4.0.0", "orjson>=3.0.0", "pyfastcopy>=1.0.0", "python-box[all]>=7.0.0", "zarr>=2.18.3",]
18
18
  [[project.authors]]
19
19
  name = "Stoupy51"
20
20
  email = "stoupy51@gmail.com"
@@ -0,0 +1,157 @@
1
+ """
2
+ This module provides utilities for collection manipulation:
3
+
4
+ - unique_list: Remove duplicates from a list while preserving order using object id, hash or str
5
+ - array_to_disk: Easily handle large numpy arrays on disk using zarr for efficient storage and access.
6
+
7
+ .. image:: https://raw.githubusercontent.com/Stoupy51/stouputils/refs/heads/main/assets/collections_module.gif
8
+ :alt: stouputils collections examples
9
+ """
10
+
11
+ # pyright: reportUnknownMemberType=false
12
+ # Imports
13
+ import atexit
14
+ import os
15
+ import shutil
16
+ import tempfile
17
+ from typing import Any, Literal
18
+
19
+ import numpy as np
20
+ import zarr # pyright: ignore[reportMissingTypeStubs]
21
+ from numpy.typing import NDArray
22
+
23
+
24
+ # Functions
25
+ def unique_list(list_to_clean: list[Any], method: Literal["id", "hash", "str"] = "str") -> list[Any]:
26
+ """ Remove duplicates from the list while keeping the order using ids (default) or hash or str
27
+
28
+ Args:
29
+ list_to_clean (list[Any]): The list to clean
30
+ method (Literal["id", "hash", "str"]): The method to use to identify duplicates
31
+ Returns:
32
+ list[Any]: The cleaned list
33
+
34
+ Examples:
35
+ >>> unique_list([1, 2, 3, 2, 1], method="id")
36
+ [1, 2, 3]
37
+
38
+ >>> s1 = {1, 2, 3}
39
+ >>> s2 = {2, 3, 4}
40
+ >>> s3 = {1, 2, 3}
41
+ >>> unique_list([s1, s2, s1, s1, s3, s2, s3], method="id")
42
+ [{1, 2, 3}, {2, 3, 4}, {1, 2, 3}]
43
+
44
+ >>> s1 = {1, 2, 3}
45
+ >>> s2 = {2, 3, 4}
46
+ >>> s3 = {1, 2, 3}
47
+ >>> unique_list([s1, s2, s1, s1, s3, s2, s3], method="str")
48
+ [{1, 2, 3}, {2, 3, 4}]
49
+ """
50
+ # Initialize the seen ids set and the result list
51
+ seen: set[Any] = set()
52
+ result: list[Any] = []
53
+
54
+ # Iterate over each item in the list
55
+ for item in list_to_clean:
56
+ if method == "id":
57
+ item_identifier = id(item)
58
+ elif method == "hash":
59
+ item_identifier = hash(item)
60
+ elif method == "str":
61
+ item_identifier = str(item)
62
+ else:
63
+ raise ValueError(f"Invalid method: {method}")
64
+
65
+ # If the item id is not in the seen ids set, add it to the seen ids set and append the item to the result list
66
+ if item_identifier not in seen:
67
+ seen.add(item_identifier)
68
+ result.append(item)
69
+
70
+ # Return the cleaned list
71
+ return result
72
+
73
+ def array_to_disk(
74
+ data: NDArray[Any] | zarr.Array,
75
+ delete_input: bool = True,
76
+ more_data: NDArray[Any] | zarr.Array | None = None
77
+ ) -> tuple[zarr.Array, str, int]:
78
+ """ Easily handle large numpy arrays on disk using zarr for efficient storage and access.
79
+
80
+ Zarr provides a simpler and more efficient alternative to np.memmap with better compression
81
+ and chunking capabilities.
82
+
83
+ Args:
84
+ data (NDArray | zarr.Array): The data to save/load as a zarr array
85
+ delete_input (bool): Whether to delete the input data after creating the zarr array
86
+ more_data (NDArray | zarr.Array | None): Additional data to append to the zarr array
87
+ Returns:
88
+ tuple[zarr.Array, str, int]: The zarr array, the directory path, and the total size in bytes
89
+
90
+ Examples:
91
+ >>> data = np.random.rand(1000, 1000)
92
+ >>> zarr_array = array_to_disk(data)[0]
93
+ >>> zarr_array.shape
94
+ (1000, 1000)
95
+
96
+ >>> more_data = np.random.rand(500, 1000)
97
+ >>> longer_array, dir_path, total_size = array_to_disk(zarr_array, more_data=more_data)
98
+ """
99
+ def dir_size(directory: str) -> int:
100
+ return sum(
101
+ os.path.getsize(os.path.join(dirpath, filename))
102
+ for dirpath, _, filenames in os.walk(directory)
103
+ for filename in filenames
104
+ )
105
+
106
+ # If data is already a zarr.Array and more_data is present, just append and return
107
+ if isinstance(data, zarr.Array) and more_data is not None:
108
+ original_size: int = data.shape[0]
109
+ new_shape: tuple[int, ...] = (original_size + more_data.shape[0], *data.shape[1:])
110
+ data.resize(new_shape)
111
+ data[original_size:] = more_data[:]
112
+
113
+ # Delete more_data if specified, calculate size, and return
114
+ if delete_input:
115
+ del more_data
116
+ store_path: str = data.store.path if hasattr(data.store, 'path') else str(data.store)
117
+ return data, store_path, dir_size(store_path)
118
+
119
+ # Create a temporary directory to store the zarr array (with compression (auto-chunking for optimal performance))
120
+ temp_dir: str = tempfile.mkdtemp()
121
+ zarr_array: zarr.Array = zarr.open_array(temp_dir, mode="w", shape=data.shape, dtype=data.dtype, chunks=True)
122
+ zarr_array[:] = data[:]
123
+
124
+ # If additional data is provided, resize and append
125
+ if more_data is not None:
126
+ original_size = data.shape[0]
127
+ new_shape = (original_size + more_data.shape[0], *data.shape[1:])
128
+ zarr_array.resize(new_shape)
129
+ zarr_array[original_size:] = more_data[:]
130
+
131
+ # Delete the original data from memory if specified
132
+ if delete_input:
133
+ del data
134
+ if more_data is not None:
135
+ del more_data
136
+
137
+ # Register a cleanup function to delete the zarr directory at exit
138
+ atexit.register(lambda: shutil.rmtree(temp_dir, ignore_errors=True))
139
+
140
+ # Return all
141
+ return zarr_array, temp_dir, dir_size(temp_dir)
142
+
143
+ if __name__ == "__main__":
144
+
145
+ # Example usage of array_to_disk (now using zarr)
146
+ print("\nZarr Example:")
147
+ data = np.random.rand(1000, 1000)
148
+ zarr_array, dir_path, total_size = array_to_disk(data, delete_input=True)
149
+ print(f"Zarr array shape: {zarr_array.shape}, directory: {dir_path}, size: {total_size:,} bytes")
150
+ print(f"Compression ratio: {(data.nbytes / total_size):.2f}x")
151
+
152
+ # Make it longer (1000x1000 -> 1500x1000)
153
+ data2 = np.random.rand(500, 1000)
154
+ longer_array, dir_path, total_size = array_to_disk(zarr_array, more_data=data2)
155
+ print(f"\nLonger zarr array shape: {longer_array.shape}, directory: {dir_path}, size: {total_size:,} bytes")
156
+ print(f"Compression ratio: {(1500 * 1000 * 8 / total_size):.2f}x")
157
+
@@ -1,62 +0,0 @@
1
- """
2
- This module provides utilities for collection manipulation:
3
-
4
- - unique_list: Remove duplicates from a list while preserving order using object id, hash or str
5
-
6
- .. image:: https://raw.githubusercontent.com/Stoupy51/stouputils/refs/heads/main/assets/collections_module.gif
7
- :alt: stouputils collections examples
8
- """
9
-
10
- # Imports
11
- from typing import Any, Literal
12
-
13
-
14
- # Functions
15
- def unique_list(list_to_clean: list[Any], method: Literal["id", "hash", "str"] = "str") -> list[Any]:
16
- """ Remove duplicates from the list while keeping the order using ids (default) or hash or str
17
-
18
- Args:
19
- list_to_clean (list[Any]): The list to clean
20
- method (Literal["id", "hash", "str"]): The method to use to identify duplicates
21
- Returns:
22
- list[Any]: The cleaned list
23
-
24
- Examples:
25
- >>> unique_list([1, 2, 3, 2, 1], method="id")
26
- [1, 2, 3]
27
-
28
- >>> s1 = {1, 2, 3}
29
- >>> s2 = {2, 3, 4}
30
- >>> s3 = {1, 2, 3}
31
- >>> unique_list([s1, s2, s1, s1, s3, s2, s3], method="id")
32
- [{1, 2, 3}, {2, 3, 4}, {1, 2, 3}]
33
-
34
- >>> s1 = {1, 2, 3}
35
- >>> s2 = {2, 3, 4}
36
- >>> s3 = {1, 2, 3}
37
- >>> unique_list([s1, s2, s1, s1, s3, s2, s3], method="str")
38
- [{1, 2, 3}, {2, 3, 4}]
39
- """
40
- # Initialize the seen ids set and the result list
41
- seen: set[Any] = set()
42
- result: list[Any] = []
43
-
44
- # Iterate over each item in the list
45
- for item in list_to_clean:
46
- if method == "id":
47
- item_identifier = id(item)
48
- elif method == "hash":
49
- item_identifier = hash(item)
50
- elif method == "str":
51
- item_identifier = str(item)
52
- else:
53
- raise ValueError(f"Invalid method: {method}")
54
-
55
- # If the item id is not in the seen ids set, add it to the seen ids set and append the item to the result list
56
- if item_identifier not in seen:
57
- seen.add(item_identifier)
58
- result.append(item)
59
-
60
- # Return the cleaned list
61
- return result
62
-
File without changes
File without changes
File without changes