From 0ad7806df37bac9964a1e77a6718e8549e1c065b Mon Sep 17 00:00:00 2001 From: David Kleiven Date: Thu, 27 Oct 2022 12:30:37 +0000 Subject: [PATCH] Use correctly formatted uuid when generating index from primary keys --- psseio/psse.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/psseio/psse.py b/psseio/psse.py index 241f1ec..a466ef2 100644 --- a/psseio/psse.py +++ b/psseio/psse.py @@ -1,11 +1,13 @@ """Convert between base case and PSS/E rawx format. """ +import hashlib import json import logging import sys from pathlib import Path -from typing import Any, Dict, Hashable, List, Union +from typing import Any, Dict, List, Union +from uuid import UUID import pandas as pd @@ -26,8 +28,10 @@ def get_rawx_record_type(data: Union[List[Any], List[List[Any]]]) -> DataSetType return DataSetType.DATA_SET if isinstance(data[0], list) else DataSetType.PARAMETER_SET -def hex_uuid(value: Hashable) -> str: - return hex(hash(value) + HASH_SHIFT) +def uuid(value: Any) -> str: + h = hashlib.md5(usedforsecurity=False) + h.update(repr(value).encode("utf-8")) + return str(UUID(bytes=h.digest())) def read_rawx(fname: Path) -> Dict[str, pd.DataFrame]: @@ -64,7 +68,7 @@ def read_rawx(fname: Path) -> Dict[str, pd.DataFrame]: # The frame has primary keys. We produce a hash value to use for index based # on the primary keys pk_fields = list(set(get_pk_fields(key)).intersection(df.columns)) - index = [hex_uuid(t) for t in df[sorted(pk_fields)].itertuples()] + index = [uuid(t) for t in df[sorted(pk_fields)].itertuples()] df = df.set_index(pd.Index(index, name="uid")) result[key] = df return result