stouputils 1.18.0__py3-none-any.whl → 1.18.1__py3-none-any.whl

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.
stouputils/collections.py CHANGED
@@ -132,16 +132,45 @@ def sort_dict_keys[T](dictionary: dict[T, Any], order: list[T], reverse: bool =
132
132
  def upsert_in_dataframe(
133
133
  df: "pl.DataFrame",
134
134
  new_entry: dict[str, Any],
135
- primary_keys: dict[str, Any] | None = None
135
+ primary_keys: list[str] | dict[str, Any] | None = None
136
136
  ) -> "pl.DataFrame":
137
137
  """ Insert or update a row in the Polars DataFrame based on primary keys.
138
138
 
139
139
  Args:
140
140
  df (pl.DataFrame): The Polars DataFrame to update.
141
141
  new_entry (dict[str, Any]): The new entry to insert or update.
142
- primary_keys (dict[str, Any]): The primary keys to identify the row (default: empty).
142
+ primary_keys (list[str] | dict[str, Any] | None): The primary keys to identify the row (for updates).
143
143
  Returns:
144
144
  pl.DataFrame: The updated Polars DataFrame.
145
+ Examples:
146
+ >>> import polars as pl
147
+ >>> df = pl.DataFrame({"id": [1, 2], "value": ["a", "b"]})
148
+ >>> new_entry = {"id": 2, "value": "updated"}
149
+ >>> updated_df = upsert_in_dataframe(df, new_entry, primary_keys=["id"])
150
+ >>> print(updated_df)
151
+ shape: (2, 2)
152
+ ┌─────┬─────────┐
153
+ │ id ┆ value │
154
+ │ --- ┆ --- │
155
+ │ i64 ┆ str │
156
+ ╞═════╪═════════╡
157
+ │ 1 ┆ a │
158
+ │ 2 ┆ updated │
159
+ └─────┴─────────┘
160
+
161
+ >>> new_entry = {"id": 3, "value": "new"}
162
+ >>> updated_df = upsert_in_dataframe(updated_df, new_entry, primary_keys=["id"])
163
+ >>> print(updated_df)
164
+ shape: (3, 2)
165
+ ┌─────┬─────────┐
166
+ │ id ┆ value │
167
+ │ --- ┆ --- │
168
+ │ i64 ┆ str │
169
+ ╞═════╪═════════╡
170
+ │ 1 ┆ a │
171
+ │ 2 ┆ updated │
172
+ │ 3 ┆ new │
173
+ └─────┴─────────┘
145
174
  """
146
175
  # Imports
147
176
  import polars as pl
@@ -155,6 +184,10 @@ def upsert_in_dataframe(
155
184
  new_row_df = pl.DataFrame([new_entry])
156
185
  return pl.concat([df, new_row_df], how="diagonal_relaxed")
157
186
 
187
+ # If primary keys are provided as a list, convert to dict with values from new_entry
188
+ if isinstance(primary_keys, list):
189
+ primary_keys = {key: new_entry[key] for key in primary_keys if key in new_entry}
190
+
158
191
  # Build mask based on primary keys
159
192
  mask: pl.Expr = pl.lit(True)
160
193
  for key, value in primary_keys.items():
@@ -69,16 +69,45 @@ def sort_dict_keys[T](dictionary: dict[T, Any], order: list[T], reverse: bool =
69
69
  \t\t>>> sort_dict_keys({\'b\': 2, \'a\': 1, \'c\': 3, \'d\': 4}, order=["c", "b"])
70
70
  \t\t{\'c\': 3, \'b\': 2, \'a\': 1, \'d\': 4}
71
71
  \t'''
72
- def upsert_in_dataframe(df: pl.DataFrame, new_entry: dict[str, Any], primary_keys: dict[str, Any] | None = None) -> pl.DataFrame:
73
- """ Insert or update a row in the Polars DataFrame based on primary keys.
72
+ def upsert_in_dataframe(df: pl.DataFrame, new_entry: dict[str, Any], primary_keys: list[str] | dict[str, Any] | None = None) -> pl.DataFrame:
73
+ ''' Insert or update a row in the Polars DataFrame based on primary keys.
74
74
 
75
75
  \tArgs:
76
76
  \t\tdf\t\t\t\t(pl.DataFrame):\t\tThe Polars DataFrame to update.
77
77
  \t\tnew_entry\t\t(dict[str, Any]):\tThe new entry to insert or update.
78
- \t\tprimary_keys\t(dict[str, Any]):\tThe primary keys to identify the row (default: empty).
78
+ \t\tprimary_keys\t(list[str] | dict[str, Any] | None):\tThe primary keys to identify the row (for updates).
79
79
  \tReturns:
80
80
  \t\tpl.DataFrame: The updated Polars DataFrame.
81
- \t"""
81
+ \tExamples:
82
+ \t\t>>> import polars as pl
83
+ \t\t>>> df = pl.DataFrame({"id": [1, 2], "value": ["a", "b"]})
84
+ \t\t>>> new_entry = {"id": 2, "value": "updated"}
85
+ \t\t>>> updated_df = upsert_in_dataframe(df, new_entry, primary_keys=["id"])
86
+ \t\t>>> print(updated_df)
87
+ \t\tshape: (2, 2)
88
+ \t\t┌─────┬─────────┐
89
+ \t\t│ id ┆ value │
90
+ \t\t│ --- ┆ --- │
91
+ \t\t│ i64 ┆ str │
92
+ \t\t╞═════╪═════════╡
93
+ \t\t│ 1 ┆ a │
94
+ \t\t│ 2 ┆ updated │
95
+ \t\t└─────┴─────────┘
96
+
97
+ \t\t>>> new_entry = {"id": 3, "value": "new"}
98
+ \t\t>>> updated_df = upsert_in_dataframe(updated_df, new_entry, primary_keys=["id"])
99
+ \t\t>>> print(updated_df)
100
+ \t\tshape: (3, 2)
101
+ \t\t┌─────┬─────────┐
102
+ \t\t│ id ┆ value │
103
+ \t\t│ --- ┆ --- │
104
+ \t\t│ i64 ┆ str │
105
+ \t\t╞═════╪═════════╡
106
+ \t\t│ 1 ┆ a │
107
+ \t\t│ 2 ┆ updated │
108
+ \t\t│ 3 ┆ new │
109
+ \t\t└─────┴─────────┘
110
+ \t'''
82
111
  def array_to_disk(data: NDArray[Any] | zarr.Array, delete_input: bool = True, more_data: NDArray[Any] | zarr.Array | None = None) -> tuple['zarr.Array', str, int]:
83
112
  """ Easily handle large numpy arrays on disk using zarr for efficient storage and access.
84
113
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: stouputils
3
- Version: 1.18.0
3
+ Version: 1.18.1
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
  Keywords: utilities,tools,helpers,development,python
6
6
  Author: Stoupy51
@@ -21,8 +21,8 @@ stouputils/archive.py,sha256=uDrPFxbY_C8SwUZRH4FWnYSoJKkFWynCx751zP9AHaY,12144
21
21
  stouputils/archive.pyi,sha256=Z2BbQAiErRYntv53QC9uf_XPw3tx3Oy73wB0Bbil11c,3246
22
22
  stouputils/backup.py,sha256=AE5WKMLiyk0VkRUfhmNfO2EUeUbZY5GTFVIuI5z7axA,20947
23
23
  stouputils/backup.pyi,sha256=-SLVykkR5U8479T84zjNPVBNnV193s0zyWjathY2DDA,4923
24
- stouputils/collections.py,sha256=jUNJQaMhmgLetVyZrYm0FFiaU0RkBfuXeJEvJ7wyGzU,9815
25
- stouputils/collections.pyi,sha256=naOr5vsvu2SOW-IUFLaoyxYXVv0Vk5pC9zD_mmT0dg8,4297
24
+ stouputils/collections.py,sha256=73799uJ5ryQoNBo7N4Cz41Q992QP_kSsw_hy33ZpDyw,11121
25
+ stouputils/collections.pyi,sha256=jPzyaeesz1IqutuT69Bt4DFOguURjXbcYO2l2sifXRA,5448
26
26
  stouputils/continuous_delivery/__init__.py,sha256=JqPww29xZ-pp6OJDGhUj2dxyV9rgTTMUz0YDDVr9RaA,731
27
27
  stouputils/continuous_delivery/__init__.pyi,sha256=_Sz2D10n1CDEyY8qDFwXNKdr01HVxanY4qdq9aN19cc,117
28
28
  stouputils/continuous_delivery/cd_utils.py,sha256=fkaHk2V3j66uFAUsM2c_UddNhXW2KAQcrh7jVsH79pU,8594
@@ -156,7 +156,7 @@ stouputils/typing.py,sha256=TwvxrvxhBRkyHkoOpfyXebN13M3xJb8MAjKXiNIWjew,2205
156
156
  stouputils/typing.pyi,sha256=U2UmFZausMYpnsUQROQE2JOwHcjx2hKV0rJuOdR57Ew,1341
157
157
  stouputils/version_pkg.py,sha256=Jsp-s03L14DkiZ94vQgrlQmaxApfn9DC8M_nzT1SJLk,7014
158
158
  stouputils/version_pkg.pyi,sha256=QPvqp1U3QA-9C_CC1dT9Vahv1hXEhstbM7x5uzMZSsQ,755
159
- stouputils-1.18.0.dist-info/WHEEL,sha256=XV0cjMrO7zXhVAIyyc8aFf1VjZ33Fen4IiJk5zFlC3g,80
160
- stouputils-1.18.0.dist-info/entry_points.txt,sha256=tx0z9VOnE-sfkmbFbA93zaBMzV3XSsKEJa_BWIqUzxw,57
161
- stouputils-1.18.0.dist-info/METADATA,sha256=OrIKAYzFOAG8DIJjpNj2UQzc2QW7iRSH_PS3yc6EnMQ,14011
162
- stouputils-1.18.0.dist-info/RECORD,,
159
+ stouputils-1.18.1.dist-info/WHEEL,sha256=XV0cjMrO7zXhVAIyyc8aFf1VjZ33Fen4IiJk5zFlC3g,80
160
+ stouputils-1.18.1.dist-info/entry_points.txt,sha256=tx0z9VOnE-sfkmbFbA93zaBMzV3XSsKEJa_BWIqUzxw,57
161
+ stouputils-1.18.1.dist-info/METADATA,sha256=PWMcVUM25QN5JXf9cDGGl-xPQQc3evJlWk8QX6FliFQ,14011
162
+ stouputils-1.18.1.dist-info/RECORD,,