-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
60 lines (54 loc) · 1.92 KB
/
utils.py
File metadata and controls
60 lines (54 loc) · 1.92 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
# utils.py
import os
import re
import json
from datetime import datetime
from typing import List, Optional, Dict
# Read file bytes from various input formats
def read_file_bytes(f):
if hasattr(f, "read"):
try:
data = f.read()
name = os.path.basename(getattr(f, "name", "uploaded"))
return data, name
except Exception:
pass
path = getattr(f, "path", None)
if path and os.path.exists(path):
with open(path, "rb") as fp:
data = fp.read()
return data, os.path.basename(path)
if isinstance(f, dict) and "path" in f and os.path.exists(f["path"]):
with open(f["path"], "rb") as fp:
data = fp.read()
return data, os.path.basename(f["path"])
if isinstance(f, (str, os.PathLike)) and os.path.exists(str(f)):
p = str(f)
with open(p, "rb") as fp:
data = fp.read()
return data, os.path.basename(p)
raise TypeError(f"Unsupported upload type: {type(f)}; value={repr(f)}")
# Convert Swedish price string to float
def to_float(s: Optional[str]) -> Optional[float]:
if not s:
return None
s = s.replace(" ", "").replace("kr", "").replace("KR", "")
if s.count(",") == 1 and s.count(".") == 0:
s = s.replace(",", ".")
elif s.count(",") > 1 and s.count(".") == 0:
s = s.replace(",", "")
return float(re.sub(r"[^\d.-]", "", s)) if re.search(r"\d", s) else None
# Normalize date string to ISO format
def normalize_date(s: Optional[str]) -> Optional[str]:
if not s:
return None
s2 = s.replace(".", "-").replace("/", "-")
for fmt in ("%Y-%m-%d", "%d-%m-%Y", "%d-%m-%y"):
try:
return datetime.strptime(s2, fmt).date().isoformat()
except:
pass
m = DATE_RE.search(s2)
if m:
return normalize_date(m.group(1))
return None