mirror of
https://github.com/Akkudoktor-EOS/EOS.git
synced 2026-08-31 20:56:41 +00:00
Mypy (#217)
* Mypy: Initial support * Add to pre-commit (currently installs own deps, could maybe changed to poetry venv in the future to reuse environment and don't need duplicated types deps). * Add type hints. * Mypy: Add missing annotations
This commit is contained in:
committed by
GitHub
parent
2a163569bc
commit
1163ddb4ac
@@ -1,12 +1,13 @@
|
||||
import datetime
|
||||
import json
|
||||
import zoneinfo
|
||||
from typing import Any
|
||||
|
||||
import numpy as np
|
||||
|
||||
|
||||
# currently unused
|
||||
def ist_dst_wechsel(tag: datetime.datetime, timezone="Europe/Berlin") -> bool:
|
||||
def ist_dst_wechsel(tag: datetime.datetime, timezone: str = "Europe/Berlin") -> bool:
|
||||
"""Checks if Daylight Saving Time (DST) starts or ends on a given day."""
|
||||
tz = zoneinfo.ZoneInfo(timezone)
|
||||
# Get the current day and the next day
|
||||
@@ -20,15 +21,25 @@ def ist_dst_wechsel(tag: datetime.datetime, timezone="Europe/Berlin") -> bool:
|
||||
|
||||
|
||||
class NumpyEncoder(json.JSONEncoder):
|
||||
def default(self, obj):
|
||||
@classmethod
|
||||
def convert_numpy(cls, obj: Any) -> tuple[Any, bool]:
|
||||
if isinstance(obj, np.ndarray):
|
||||
return obj.tolist() # Convert NumPy arrays to lists
|
||||
# Convert NumPy arrays to lists
|
||||
return [
|
||||
None if isinstance(x, (int, float)) and np.isnan(x) else x for x in obj.tolist()
|
||||
], True
|
||||
if isinstance(obj, np.generic):
|
||||
return obj.item() # Convert NumPy scalars to native Python types
|
||||
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
|
||||
return super(NumpyEncoder, self).default(obj)
|
||||
|
||||
@staticmethod
|
||||
def dumps(data):
|
||||
def dumps(data: Any) -> str:
|
||||
"""Static method to serialize a Python object into a JSON string using NumpyEncoder.
|
||||
|
||||
Args:
|
||||
|
||||
Reference in New Issue
Block a user