feat(add-data): Add general config and utilities for data prep

This commit is contained in:
Tobias Quadfasel
2024-08-31 14:15:53 +02:00
parent 49ff1bbfec
commit 596893edf3
2 changed files with 41 additions and 0 deletions

View File

@@ -0,0 +1,7 @@
"""Global configuration for data preprocessing."""
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent.parent
DATA_DIR = BASE_DIR / "data"
APP_DIR = BASE_DIR / "app"

View File

@@ -0,0 +1,34 @@
import json
from datetime import datetime
from typing import Any
class DateTimeEncoder(json.JSONEncoder):
"""A custom JSON encoder that handles datetime objects.
This encoder extends the json.JSONEncoder class to provide serialization
support for datetime objects, converting them to ISO format strings.
Methods
-------
default(obj: Any) -> str
Encode datetime objects as ISO format strings.
"""
def default(self, obj: Any) -> str:
"""Encode the given object as a JSON-serializable type.
Parameters
----------
obj : Any
The object to encode.
Returns
-------
str
The ISO format string if the object is a datetime, otherwise
delegates to the superclass encoder.
"""
if isinstance(obj, datetime):
return obj.isoformat()
return super().default(obj)