sibi-dst 2025.8.6__py3-none-any.whl → 2025.8.7__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.
@@ -1,5 +1,6 @@
1
1
  from __future__ import annotations
2
2
 
3
+ import asyncio
3
4
  from typing import Any, Dict, Optional, TypeVar, Union
4
5
 
5
6
  import dask.dataframe as dd
@@ -104,7 +105,6 @@ class HttpBackend(BaseBackend):
104
105
  return self.total_records, result
105
106
 
106
107
 
107
- # ---- Main DfHelper ----
108
108
  class DfHelper(ManagedResource):
109
109
  _BACKEND_STRATEGIES = {
110
110
  "sqlalchemy": SqlAlchemyBackend,
@@ -198,6 +198,37 @@ class DfHelper(ManagedResource):
198
198
  df = df.persist() if persist else df
199
199
  return df.compute() if as_pandas else df
200
200
 
201
+ async def load_async(
202
+ self,
203
+ *,
204
+ persist: bool = False,
205
+ as_pandas: bool = False,
206
+ prefer_native: bool = False,
207
+ **options,
208
+ ):
209
+ """
210
+ Async load that prefers native async backends when available,
211
+ otherwise runs the sync `load()` in a worker thread via asyncio.to_thread.
212
+
213
+ Args:
214
+ persist: same as `load`
215
+ as_pandas: same as `load`
216
+ prefer_native: if True and the backend overrides `aload`, use it.
217
+ otherwise force thread offload of `load()`.
218
+ **options: forwarded to `load` / `aload`
219
+ """
220
+ # If the backend provided an override for `aload`, use it
221
+ if prefer_native and type(self.backend_strategy).aload is not BaseBackend.aload:
222
+ return await self.aload(persist=persist, as_pandas=as_pandas, **options)
223
+
224
+ # Fall back to offloading the sync path to a thread
225
+ return await asyncio.to_thread(
226
+ self.load,
227
+ persist=persist,
228
+ as_pandas=as_pandas,
229
+ **options,
230
+ )
231
+
201
232
  # ---------- dataframe post-processing ----------
202
233
  def _post_process_df(self, df: dd.DataFrame) -> dd.DataFrame:
203
234
  self.logger.debug("Post-processing DataFrame.")
@@ -240,9 +271,12 @@ class DfHelper(ManagedResource):
240
271
  return df
241
272
 
242
273
  # ---------- sinks ----------
243
- def save_to_parquet(self, df: dd.DataFrame, parquet_filename: str, **kwargs):
274
+ def save_to_parquet(self, df: dd.DataFrame, **kwargs):
244
275
  fs: AbstractFileSystem = kwargs.pop("fs", self.fs)
245
- path: str = kwargs.pop("parquet_storage_path")
276
+ path: str = kwargs.pop("parquet_storage_path", self.backend_parquet.parquet_storage_path if self.backend_parquet else None)
277
+ parquet_filename = kwargs.pop("parquet_filename" or self._backend_params.parquet_filename if self.backend_parquet else None)
278
+ if not parquet_filename:
279
+ raise ValueError("A 'parquet_filename' keyword argument must be provided.")
246
280
  if not fs:
247
281
  raise ValueError("A filesystem (fs) must be provided to save the parquet file.")
248
282
  if not path:
@@ -272,7 +306,7 @@ class DfHelper(ManagedResource):
272
306
  writer.save_to_clickhouse(df)
273
307
  self.logger.debug("Save to ClickHouse completed.")
274
308
 
275
- # ---------- convenience period loaders ----------
309
+ # ---------- period loaders ----------
276
310
  def load_period(self, dt_field: str, start: str, end: str, **kwargs):
277
311
  final_kwargs = self._prepare_period_filters(dt_field, start, end, **kwargs)
278
312
  return self.load(**final_kwargs)
@@ -0,0 +1,12 @@
1
+ import asyncio
2
+ import dask.dataframe as dd
3
+
4
+
5
+ def is_dask_dataframe(df):
6
+ """Check if the given object is a Dask DataFrame."""
7
+ return isinstance(df, dd.DataFrame)
8
+
9
+ async def to_thread(func, *args, **kwargs):
10
+ """Explicit helper to keep code clear where we hop off the event loop."""
11
+ return await asyncio.to_thread(func, *args, **kwargs)
12
+
@@ -0,0 +1,195 @@
1
+ from __future__ import annotations
2
+ import pandas as pd
3
+ import dask.dataframe as dd
4
+ from typing import Iterable, Optional, List, Tuple, Union
5
+ import fsspec
6
+
7
+ DNFFilter = List[List[Tuple[str, str, Union[str, int]]]]
8
+
9
+
10
+ class HiveDatePartitionedStore:
11
+ """
12
+ Dask-only Parquet store with Hive-style yyyy=…/mm=…/dd=… partitions.
13
+
14
+ - `write(...)` safely "overwrites" S3 prefixes via per-object deletes (no bulk DeleteObjects).
15
+ - `read_range(...)` builds DNF filters and auto-matches partition types (string vs int).
16
+ """
17
+
18
+ def __init__(
19
+ self,
20
+ path: str,
21
+ *,
22
+ filesystem=None, # fsspec filesystem or None to infer from path
23
+ date_col: str = "tracking_dt",
24
+ compression: str = "zstd",
25
+ partition_values_as_strings: bool = True, # keep mm=07, dd=01 folder names
26
+ logger=None,
27
+ ) -> None:
28
+ self.path = path
29
+ self.fs = filesystem or fsspec.open(path).fs
30
+ self.date_col = date_col
31
+ self.compression = compression
32
+ self.partition_values_as_strings = partition_values_as_strings
33
+ self.log = logger
34
+
35
+ # ----------------- public API -----------------
36
+
37
+ def write(
38
+ self,
39
+ df: dd.DataFrame,
40
+ *,
41
+ repartition: Optional[int] = None,
42
+ overwrite: bool = False,
43
+ ) -> None:
44
+ """Write Dask DataFrame to Hive-style yyyy/mm/dd partitions."""
45
+ self._require_col(df, self.date_col)
46
+ ser = dd.to_datetime(df[self.date_col], errors="coerce")
47
+
48
+ if self.partition_values_as_strings:
49
+ parts = {
50
+ "yyyy": ser.dt.strftime("%Y"),
51
+ "mm": ser.dt.strftime("%m"),
52
+ "dd": ser.dt.strftime("%d"),
53
+ }
54
+ else:
55
+ parts = {
56
+ "yyyy": ser.dt.year.astype("int32"),
57
+ "mm": ser.dt.month.astype("int8"),
58
+ "dd": ser.dt.day.astype("int8"),
59
+ }
60
+
61
+ df = df.assign(**{self.date_col: ser}, **parts)
62
+
63
+ if repartition:
64
+ df = df.repartition(npartitions=repartition)
65
+
66
+ if overwrite:
67
+ self._safe_rm_prefix(self.path)
68
+
69
+ if self.log:
70
+ self.log.info(f"Writing parquet to {self.path} (hive yyyy/mm/dd)…")
71
+
72
+ df.to_parquet(
73
+ self.path,
74
+ engine="pyarrow",
75
+ write_index=False,
76
+ filesystem=self.fs,
77
+ partition_on=["yyyy", "mm", "dd"],
78
+ compression=self.compression,
79
+ overwrite=False, # we pre-cleaned if overwrite=True
80
+ )
81
+
82
+ def read_range(
83
+ self,
84
+ start: Union[str, pd.Timestamp],
85
+ end: Union[str, pd.Timestamp],
86
+ *,
87
+ columns: Optional[Iterable[str]] = None,
88
+ ) -> dd.DataFrame:
89
+ """
90
+ Read a date window with partition pruning. Tries string filters first,
91
+ falls back to integer filters if Arrow infers partition types as ints.
92
+ """
93
+ str_filters = self._dnf_filters_for_range_str(start, end)
94
+ try:
95
+ return dd.read_parquet(
96
+ self.path,
97
+ engine="pyarrow",
98
+ filesystem=self.fs,
99
+ columns=list(columns) if columns else None,
100
+ filters=str_filters,
101
+ )
102
+ except Exception:
103
+ int_filters = self._dnf_filters_for_range_int(start, end)
104
+ return dd.read_parquet(
105
+ self.path,
106
+ engine="pyarrow",
107
+ filesystem=self.fs,
108
+ columns=list(columns) if columns else None,
109
+ filters=int_filters,
110
+ )
111
+
112
+ # Convenience: full month / single day
113
+ def read_month(self, year: int, month: int, *, columns=None) -> dd.DataFrame:
114
+ start = pd.Timestamp(year=year, month=month, day=1)
115
+ end = (start + pd.offsets.MonthEnd(0))
116
+ return self.read_range(start, end, columns=columns)
117
+
118
+ def read_day(self, year: int, month: int, day: int, *, columns=None) -> dd.DataFrame:
119
+ ts = pd.Timestamp(year=year, month=month, day=day)
120
+ return self.read_range(ts, ts, columns=columns)
121
+
122
+ # ----------------- internals -----------------
123
+
124
+ @staticmethod
125
+ def _pad2(n: int) -> str:
126
+ return f"{n:02d}"
127
+
128
+ def _safe_rm_prefix(self, path: str) -> None:
129
+ """Per-object delete to avoid S3 bulk DeleteObjects (and Content-MD5 issues)."""
130
+ if not self.fs.exists(path):
131
+ return
132
+ if self.log:
133
+ self.log.info(f"Cleaning prefix (safe delete): {path}")
134
+ for k in self.fs.find(path):
135
+ try:
136
+ (self.fs.rm_file(k) if hasattr(self.fs, "rm_file") else self.fs.rm(k, recursive=False))
137
+ except Exception as e:
138
+ if self.log:
139
+ self.log.warning(f"Could not delete {k}: {e}")
140
+
141
+ @staticmethod
142
+ def _require_col(df: dd.DataFrame, col: str) -> None:
143
+ if col not in df.columns:
144
+ raise KeyError(f"'{col}' not in DataFrame")
145
+
146
+ # ---- DNF builders (string vs int) ----
147
+ def _dnf_filters_for_range_str(self, start, end) -> DNFFilter:
148
+ s, e = pd.Timestamp(start), pd.Timestamp(end)
149
+ if s > e:
150
+ raise ValueError("start > end")
151
+ sY, sM, sD = s.year, s.month, s.day
152
+ eY, eM, eD = e.year, e.month, e.day
153
+ p2 = self._pad2
154
+ if sY == eY and sM == eM:
155
+ return [[("yyyy","==",str(sY)),("mm","==",p2(sM)),("dd",">=",p2(sD)),("dd","<=",p2(eD))]]
156
+ clauses: DNFFilter = [
157
+ [("yyyy","==",str(sY)),("mm","==",p2(sM)),("dd",">=",p2(sD))],
158
+ [("yyyy","==",str(eY)),("mm","==",p2(eM)),("dd","<=",p2(eD))]
159
+ ]
160
+ if sY == eY:
161
+ for m in range(sM+1, eM):
162
+ clauses.append([("yyyy","==",str(sY)),("mm","==",p2(m))])
163
+ return clauses
164
+ for m in range(sM+1, 13):
165
+ clauses.append([("yyyy","==",str(sY)),("mm","==",p2(m))])
166
+ for y in range(sY+1, eY):
167
+ clauses.append([("yyyy","==",str(y))])
168
+ for m in range(1, eM):
169
+ clauses.append([("yyyy","==",str(eY)),("mm","==",p2(m))])
170
+ return clauses
171
+
172
+ @staticmethod
173
+ def _dnf_filters_for_range_int(start, end) -> DNFFilter:
174
+ s, e = pd.Timestamp(start), pd.Timestamp(end)
175
+ if s > e:
176
+ raise ValueError("start > end")
177
+ sY, sM, sD = s.year, s.month, s.day
178
+ eY, eM, eD = e.year, e.month, e.day
179
+ if sY == eY and sM == eM:
180
+ return [[("yyyy","==",sY),("mm","==",sM),("dd",">=",sD),("dd","<=",eD)]]
181
+ clauses: DNFFilter = [
182
+ [("yyyy","==",sY),("mm","==",sM),("dd",">=",sD)],
183
+ [("yyyy","==",eY),("mm","==",eM),("dd","<=",eD)],
184
+ ]
185
+ if sY == eY:
186
+ for m in range(sM+1, eM):
187
+ clauses.append([("yyyy","==",sY),("mm","==",m)])
188
+ return clauses
189
+ for m in range(sM+1, 13):
190
+ clauses.append([("yyyy","==",sY),("mm","==",m)])
191
+ for y in range(sY+1, eY):
192
+ clauses.append([("yyyy","==",y)])
193
+ for m in range(1, eM):
194
+ clauses.append([("yyyy","==",eY),("mm","==",m)])
195
+ return clauses
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sibi-dst
3
- Version: 2025.8.6
3
+ Version: 2025.8.7
4
4
  Summary: Data Science Toolkit
5
5
  Author: Luis Valverde
6
6
  Author-email: lvalverdeb@gmail.com
@@ -2,7 +2,7 @@ sibi_dst/__init__.py,sha256=D01Z2Ds4zES8uz5Zp7qOWD0EcfCllWgew7AWt2X1SQg,445
2
2
  sibi_dst/df_helper/__init__.py,sha256=CyDXtFhRnMrycktxNO8jGGkP0938QiScl56kMZS1Sf8,578
3
3
  sibi_dst/df_helper/_artifact_updater_async.py,sha256=0lUwel-IkmKewRnmMv9GtuT-P6SivkIKtgOHvKchHlc,8462
4
4
  sibi_dst/df_helper/_artifact_updater_threaded.py,sha256=M5GNZismOqMmBrcyfolP1DPv87VILQf_P18is_epn50,7238
5
- sibi_dst/df_helper/_df_helper.py,sha256=TS8nQV6QExSz5rNh94zmawNOvQ6eBEzsAcJkiiKXAb0,12945
5
+ sibi_dst/df_helper/_df_helper.py,sha256=IqlfTPnbXyaLLkwn8iaulHLuJ6LlBB3hSR3e5O8ixQ0,14360
6
6
  sibi_dst/df_helper/_parquet_artifact.py,sha256=tqYOjwxHV1MsADmn-RNFuVI_RrEvvmCJHZieRcsVXuc,12334
7
7
  sibi_dst/df_helper/_parquet_reader.py,sha256=tFq0OQVczozbKZou93vscokp2R6O2DIJ1zHbZqVjagc,3069
8
8
  sibi_dst/df_helper/backends/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -37,6 +37,7 @@ sibi_dst/osmnx_helper/utils.py,sha256=HfxrmXVPq3akf68SiwncbAp7XI1ER-zp8YN_doh7Ya
37
37
  sibi_dst/tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
38
38
  sibi_dst/tests/test_data_wrapper_class.py,sha256=6uFmZR2DxnxQz49L5jT2ehlKvlLnpUHMLFB_PqqUq7k,3336
39
39
  sibi_dst/utils/__init__.py,sha256=vShNCOMPw8KKwlb4tq5XGrpjqakJ_OE8YDc_xDAWAxI,1302
40
+ sibi_dst/utils/async_utils.py,sha256=53aywfgq1Q6-0OVr9qR1Sf6g7Qv3I9qunAAR4fjFXBE,351
40
41
  sibi_dst/utils/base.py,sha256=IyObjZ7AaE-YjVU0RLIXNCnQKWwzi5NH2I6D1KfcIyk,8716
41
42
  sibi_dst/utils/business_days.py,sha256=dP0Xj4FhTBIvZZrZYLOHZl5zOpDAgWkD4p_1a7BOT7I,8461
42
43
  sibi_dst/utils/clickhouse_writer.py,sha256=NngJyJpx2PjUQWsX0YmwCuGdeViK77Wi3HmYqHz3jTc,9544
@@ -55,6 +56,7 @@ sibi_dst/utils/parquet_saver.py,sha256=aYBlijqPAn-yuJXhmaRIteAN_IAQZvPh8I8Os2TLG
55
56
  sibi_dst/utils/periods.py,sha256=8eTGi-bToa6_a8Vwyg4fkBPryyzft9Nzy-3ToxjqC8c,1434
56
57
  sibi_dst/utils/phone_formatter.py,sha256=oeM22nLjhObENrpItCNeVpkYS4pXRm5hSxdk0M4nvwU,4580
57
58
  sibi_dst/utils/storage_config.py,sha256=DLtP5jKVM0mdFdgRw6LQfRqyavMjJcCVU7GhsUCRH78,4427
59
+ sibi_dst/utils/storage_hive.py,sha256=FCF6zSTM_VWBEvSuTjn2bmb69oqsYjSS6nvnSZrJRFY,7123
58
60
  sibi_dst/utils/storage_manager.py,sha256=La1NY79bhRAmHWXp7QcXJZtbHoRboJMgoXOSXbIl1SA,6643
59
61
  sibi_dst/utils/update_planner.py,sha256=smlMHpr1p8guZnP5SyzCe6RsC-XkPOJWIsdeospUyb0,11471
60
62
  sibi_dst/utils/webdav_client.py,sha256=D9J5d1f1qQwHGm5FE5AMVpOPwcU5oD7K8JZoKGP8NpM,5811
@@ -78,6 +80,6 @@ sibi_dst/v2/df_helper/core/_params_config.py,sha256=DYx2drDz3uF-lSPzizPkchhy-kxR
78
80
  sibi_dst/v2/df_helper/core/_query_config.py,sha256=Y8LVSyaKuVkrPluRDkQoOwuXHQxner1pFWG3HPfnDHM,441
79
81
  sibi_dst/v2/utils/__init__.py,sha256=6H4cvhqTiFufnFPETBF0f8beVVMpfJfvUs6Ne0TQZNY,58
80
82
  sibi_dst/v2/utils/log_utils.py,sha256=rfk5VsLAt-FKpv6aPTC1FToIPiyrnHAFFBAkHme24po,4123
81
- sibi_dst-2025.8.6.dist-info/METADATA,sha256=fFvtxHyXl8FCryToGB4H91n_NZ3hzJNRos0O2FUNVBQ,2610
82
- sibi_dst-2025.8.6.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
83
- sibi_dst-2025.8.6.dist-info/RECORD,,
83
+ sibi_dst-2025.8.7.dist-info/METADATA,sha256=6sDcEFzHqZK8J1kSjtOCT_m-e5peFg4gFHpAGfeZWRw,2610
84
+ sibi_dst-2025.8.7.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
85
+ sibi_dst-2025.8.7.dist-info/RECORD,,