-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
61 lines (47 loc) · 1.4 KB
/
utils.py
File metadata and controls
61 lines (47 loc) · 1.4 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
import uuid
import logging
from datetime import datetime
# ----------------------------
# Logging Setup
# ----------------------------
def get_logger(name: str):
"""
Creates a formatted logger for any microservice.
"""
logger = logging.getLogger(name)
logger.setLevel(logging.INFO)
if not logger.handlers:
handler = logging.StreamHandler()
formatter = logging.Formatter(
"[%(asctime)s] [%(name)s] [%(levelname)s] %(message)s",
datefmt="%Y-%m-%d %H:%M:%S"
)
handler.setFormatter(formatter)
logger.addHandler(handler)
return logger
# ----------------------------
# UUID Helper
# ----------------------------
def generate_id(prefix: str = "") -> str:
"""
Generates unique IDs for trips, events, etc.
"""
value = f"{prefix}{uuid.uuid4().hex[:12]}"
return value
# ----------------------------
# Timestamp Helper
# ----------------------------
def now_timestamp() -> datetime:
"""Returns a timezone-aware timestamp."""
return datetime.utcnow()
# ----------------------------
# Zone Utility Helper
# ----------------------------
def zone_from_coordinates(lat: float, lon: float) -> str:
"""
Converts coordinates into artificial zones.
In a real system, this uses geohashing.
"""
lat_bucket = int(lat * 10) % 5
lon_bucket = int(lon * 10) % 5
return f"Z{lat_bucket}{lon_bucket}"