forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathids.py
More file actions
45 lines (33 loc) · 1.4 KB
/
Copy pathids.py
File metadata and controls
45 lines (33 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
from __future__ import annotations
import hashlib
import hmac
import json
from typing import Any
def canonical_json(value: Any) -> str:
return json.dumps(value, sort_keys=True, separators=(",", ":"), default=str, ensure_ascii=True)
def stable_hash(*parts: Any, length: int = 32) -> str:
payload = "\x1f".join(canonical_json(part) for part in parts)
return hashlib.sha256(payload.encode("utf-8")).hexdigest()[:length]
def stable_hmac(key: str, value: str, length: int = 32) -> str:
return hmac.new(key.encode("utf-8"), value.encode("utf-8"), hashlib.sha256).hexdigest()[:length]
class StableIdFactory:
def __init__(self, namespace: str):
self.namespace = namespace
def new_id(self, prefix: str, *parts: Any) -> str:
return f"{prefix}_{stable_hash(self.namespace, prefix, *parts, length=24)}"
def edit_distance(a: str, b: str) -> int:
"""Levenshtein edit distance between two strings."""
if len(a) < len(b):
return edit_distance(b, a)
if len(b) == 0:
return len(a)
prev_row = list(range(len(b) + 1))
for i, ca in enumerate(a):
curr_row = [i + 1]
for j, cb in enumerate(b):
insertions = prev_row[j + 1] + 1
deletions = curr_row[j] + 1
substitutions = prev_row[j] + (ca != cb)
curr_row.append(min(insertions, deletions, substitutions))
prev_row = curr_row
return prev_row[-1]