-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
90 lines (65 loc) · 2.45 KB
/
Copy pathutils.py
File metadata and controls
90 lines (65 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""Utility helpers for the Streamlit preprocessing app.
This module intentionally has no Streamlit dependency, so it can be imported
from anywhere (including tests/scripts) without UI side-effects.
"""
from __future__ import annotations
import io
from datetime import datetime, timezone
from typing import Any, Dict, Optional, Tuple
import pandas as pd
def human_readable_bytes(num_bytes: int) -> str:
"""Convert a byte count into a human-friendly string."""
if num_bytes < 0:
return "0 B"
units = ["B", "KB", "MB", "GB", "TB"]
size = float(num_bytes)
unit = 0
while size >= 1024 and unit < len(units) - 1:
size /= 1024
unit += 1
if unit == 0:
return f"{int(size)} {units[unit]}"
return f"{size:.2f} {units[unit]}"
def utc_now_iso() -> str:
"""Return current UTC time in ISO 8601 format."""
return datetime.now(timezone.utc).replace(microsecond=0).isoformat()
def read_csv_with_fallbacks(
uploaded_file: Any,
**read_csv_kwargs: Any,
) -> Tuple[pd.DataFrame, Dict[str, Any]]:
"""Read CSV from a Streamlit UploadedFile (or any file-like) robustly.
The Streamlit UploadedFile is a file-like wrapper with a `.getvalue()` method.
This helper reads the underlying bytes once, then retries parsing with
common encodings.
Returns:
(df, meta)
meta includes:
- file_name
- bytes
- encoding
"""
if uploaded_file is None:
raise ValueError("No file provided")
file_name = getattr(uploaded_file, "name", "uploaded.csv")
raw_bytes: Optional[bytes] = None
if hasattr(uploaded_file, "getvalue"):
raw_bytes = uploaded_file.getvalue()
if raw_bytes is None:
# Fall back to reading from the file-like object directly.
raw_bytes = uploaded_file.read()
encodings_to_try = ["utf-8", "utf-8-sig", "latin1"]
last_error: Optional[Exception] = None
for enc in encodings_to_try:
try:
buf = io.BytesIO(raw_bytes)
df = pd.read_csv(buf, encoding=enc, low_memory=False, **read_csv_kwargs)
return df, {"file_name": file_name, "bytes": len(raw_bytes), "encoding": enc}
except UnicodeDecodeError as e:
last_error = e
continue
except Exception as e: # noqa: BLE001 - surface original error
last_error = e
break
if last_error is not None:
raise last_error
raise RuntimeError("Failed to read CSV")