init commit
This commit is contained in:
13
utils_id.py
Normal file
13
utils_id.py
Normal file
@@ -0,0 +1,13 @@
|
||||
# -*- 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)
|
||||
Reference in New Issue
Block a user