Skip to content

Commit

Permalink
Use correctly formatted uuid when generating index from primary keys
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkleiven committed Oct 27, 2022
1 parent 4054b02 commit 0ad7806
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions psseio/psse.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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]:
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0ad7806

Please sign in to comment.