14 lines
545 B
Python
14 lines
545 B
Python
|
|
# -*- coding: utf-8 -*-
|
||
|
|
import hashlib
|
||
|
|
|
||
|
|
def stable_symbol_id(symbol: str, exchange: str = "US") -> int:
|
||
|
|
"""Generate a stable positive 64-bit int ID from symbol+exchange.
|
||
|
|
Collisions are extremely unlikely for our scale.
|
||
|
|
"""
|
||
|
|
base = f"{exchange}:{symbol}".upper().encode("utf-8")
|
||
|
|
h = hashlib.sha1(base).digest()
|
||
|
|
# take first 8 bytes as unsigned 64-bit integer
|
||
|
|
val = int.from_bytes(h[:8], byteorder="big", signed=False)
|
||
|
|
# constrain to 63-bit to avoid CSV tools issues with signedness
|
||
|
|
return val & ((1 << 63) - 1)
|