Files
syops/backend/app/services/shipping_notice/reservation_id.py
T

42 lines
1.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from __future__ import annotations
import re
from typing import Any
# 常见 Reservation ID:纯数字,如 1000140351(通常 8~12 位)
RESERVATION_ID_NUMERIC_RE = re.compile(r"^\d{8,12}$")
def normalize_reservation_id(value: Any) -> str:
"""Normalize reservation id; Excel often stores numeric ids as float."""
if value is None:
return ""
if isinstance(value, bool):
return ""
if isinstance(value, float):
if value != value: # NaN
return ""
if value.is_integer():
return str(int(value))
text = str(value).strip()
elif isinstance(value, int):
text = str(value)
else:
text = str(value).strip()
if not text:
return ""
text = text.replace(",", "").replace(",", "").strip()
if re.fullmatch(r"\d+\.0+", text):
text = text.split(".", 1)[0]
return text
def looks_like_reservation_id(value: Any) -> bool:
text = normalize_reservation_id(value)
return bool(text and RESERVATION_ID_NUMERIC_RE.fullmatch(text))