26 lines
657 B
Python
26 lines
657 B
Python
|
|
"""Enrichment 共享:去重键与 object_json 规范化(sync/async 共用)。"""
|
|||
|
|
|
|||
|
|
from __future__ import annotations
|
|||
|
|
|
|||
|
|
import json
|
|||
|
|
from typing import Any
|
|||
|
|
|
|||
|
|
|
|||
|
|
def dedupe_key(f: dict) -> tuple:
|
|||
|
|
s = f.get("subject") or ""
|
|||
|
|
p = f.get("predicate") or ""
|
|||
|
|
o = f.get("object_json")
|
|||
|
|
try:
|
|||
|
|
oj = json.dumps(o, sort_keys=True, ensure_ascii=False) if o is not None else ""
|
|||
|
|
except (TypeError, ValueError):
|
|||
|
|
oj = str(o)
|
|||
|
|
return (str(s), str(p), oj)
|
|||
|
|
|
|||
|
|
|
|||
|
|
def normalize_object_json(obj: Any) -> dict | list | None:
|
|||
|
|
if obj is None:
|
|||
|
|
return None
|
|||
|
|
if isinstance(obj, (dict, list)):
|
|||
|
|
return obj
|
|||
|
|
return {"value": obj}
|