Add hex converter utility
- To move to separate utility file in the future
This commit is contained in:
+13
-15
@@ -1,11 +1,20 @@
|
|||||||
# File: extractor.py
|
|
||||||
# Copyright (C) 2026 Erick Ahmed
|
|
||||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
||||||
|
|
||||||
import json
|
import json
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
|
|
||||||
|
def to_int(x):
|
||||||
|
"""Convert a hex string or integer to int, returning NaN on failure."""
|
||||||
|
if isinstance(x, (int, np.integer)):
|
||||||
|
return int(x)
|
||||||
|
if isinstance(x, str):
|
||||||
|
try:
|
||||||
|
return int(x, 16)
|
||||||
|
except ValueError:
|
||||||
|
return np.nan
|
||||||
|
return np.nan
|
||||||
|
|
||||||
def extract_id(row: pd.Series) -> str:
|
def extract_id(row: pd.Series) -> str:
|
||||||
"""Extracts PGN from metadata or falls back to CAN ID."""
|
"""Extracts PGN from metadata or falls back to CAN ID."""
|
||||||
meta = row.get('j1939_metadata')
|
meta = row.get('j1939_metadata')
|
||||||
@@ -25,14 +34,3 @@ def load_data(file_path: Path) -> pd.DataFrame:
|
|||||||
df = pd.read_parquet(file_path)
|
df = pd.read_parquet(file_path)
|
||||||
df['Identifier'] = df.apply(extract_id, axis=1)
|
df['Identifier'] = df.apply(extract_id, axis=1)
|
||||||
return df
|
return df
|
||||||
|
|
||||||
def to_int(x):
|
|
||||||
"""Convert a hex string or integer to int, returning NaN on failure."""
|
|
||||||
if isinstance(x, (int, np.integer)):
|
|
||||||
return int(x)
|
|
||||||
if isinstance(x, str):
|
|
||||||
try:
|
|
||||||
return int(x, 16)
|
|
||||||
except ValueError:
|
|
||||||
return np.nan
|
|
||||||
return np.nan
|
|
||||||
|
|||||||
Reference in New Issue
Block a user