2024-11-19 21:47:43 +01:00
|
|
|
import json
|
2025-01-12 05:19:37 +01:00
|
|
|
from typing import Any, Optional
|
2024-10-03 11:05:44 +02:00
|
|
|
|
2024-11-19 21:47:43 +01:00
|
|
|
import numpy as np
|
|
|
|
|
2024-12-15 14:40:03 +01:00
|
|
|
from akkudoktoreos.config.configabc import SettingsBaseModel
|
2025-01-05 14:41:07 +01:00
|
|
|
from akkudoktoreos.core.logging import get_logger
|
2024-03-31 13:00:01 +02:00
|
|
|
|
2024-12-15 14:40:03 +01:00
|
|
|
logger = get_logger(__name__)
|
2024-03-31 13:00:01 +02:00
|
|
|
|
|
|
|
|
2025-01-12 05:19:37 +01:00
|
|
|
class classproperty(property):
|
|
|
|
def __get__(self, _: Any, owner_cls: Optional[type[Any]] = None) -> Any:
|
|
|
|
if owner_cls is None:
|
|
|
|
return self
|
|
|
|
assert self.fget is not None
|
|
|
|
return self.fget(owner_cls)
|
|
|
|
|
|
|
|
|
2024-12-15 14:40:03 +01:00
|
|
|
class UtilsCommonSettings(SettingsBaseModel):
|
2025-01-15 00:54:45 +01:00
|
|
|
"""Utils Configuration."""
|
|
|
|
|
2024-12-15 14:40:03 +01:00
|
|
|
pass
|
2024-03-31 13:00:01 +02:00
|
|
|
|
2024-10-03 11:05:44 +02:00
|
|
|
|
2024-11-19 21:47:43 +01:00
|
|
|
class NumpyEncoder(json.JSONEncoder):
|
2024-11-26 22:28:05 +01:00
|
|
|
@classmethod
|
|
|
|
def convert_numpy(cls, obj: Any) -> tuple[Any, bool]:
|
2024-11-19 21:47:43 +01:00
|
|
|
if isinstance(obj, np.ndarray):
|
2024-11-26 22:28:05 +01:00
|
|
|
# Convert NumPy arrays to lists
|
|
|
|
return [
|
|
|
|
None if isinstance(x, (int, float)) and np.isnan(x) else x for x in obj.tolist()
|
|
|
|
], True
|
2024-11-19 21:47:43 +01:00
|
|
|
if isinstance(obj, np.generic):
|
2024-11-26 22:28:05 +01:00
|
|
|
return obj.item(), True # Convert NumPy scalars to native Python types
|
|
|
|
return obj, False
|
|
|
|
|
|
|
|
def default(self, obj: Any) -> Any:
|
|
|
|
obj, converted = NumpyEncoder.convert_numpy(obj)
|
|
|
|
if converted:
|
|
|
|
return obj
|
2024-11-19 21:47:43 +01:00
|
|
|
return super(NumpyEncoder, self).default(obj)
|
|
|
|
|
|
|
|
@staticmethod
|
2024-11-26 22:28:05 +01:00
|
|
|
def dumps(data: Any) -> str:
|
2024-11-19 21:47:43 +01:00
|
|
|
"""Static method to serialize a Python object into a JSON string using NumpyEncoder.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
data: The Python object to serialize.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str: A JSON string representation of the object.
|
|
|
|
"""
|
|
|
|
return json.dumps(data, cls=NumpyEncoder)
|
|
|
|
|
|
|
|
|
2024-09-20 13:04:52 +02:00
|
|
|
# # Example usage
|
|
|
|
# start_date = datetime.datetime(2024, 3, 31) # Date of the DST change
|
|
|
|
# if ist_dst_wechsel(start_date):
|
2025-01-18 14:26:34 +01:00
|
|
|
# hours = 23 # Adjust to 23 hours for DST change days
|
2024-04-01 13:16:24 +02:00
|
|
|
# else:
|
2025-01-18 14:26:34 +01:00
|
|
|
# hours = 24 # Default value for days without DST change
|