Skip to content

capturegraph.data.typed_json.handlers.weather #

WeatherHandler #

Bases: TypeHandler

TypeHandler for CaptureGraph Weather objects.

Serializes Weather to/from iOS PWeather format (WeatherKit data).

Source code in capturegraph-lib/capturegraph/data/typed_json/handlers/weather.py
class WeatherHandler(TypeHandler):
    """TypeHandler for CaptureGraph Weather objects.

    Serializes Weather to/from iOS PWeather format (WeatherKit data).
    """

    # Required keys that must be present to identify a Weather dict
    REQUIRED_KEYS = {
        "temperature_celsius",
        "condition",
        "humidity_ratio",
        "wind_speed_mps",
        "date_timestamp",
    }

    def decodable_schema(self) -> Dict[str, Tuple[type, ...]]:
        """Return schema: expects weather-related keys."""
        return {
            "temperature_celsius": (int, float),
            "condition": str,
            "humidity_ratio": (int, float),
            "wind_speed_mps": (int, float),
            "date_timestamp": (int, float),
        }

    def decode(self, obj: Dict[str, Any]) -> Any | None:
        """Decode JSON dict to Weather.

        Args:
            obj: Dict with PWeather JSON format fields.

        Returns:
            Weather instance, or None if schema doesn't match.
        """
        try:
            return Weather.from_json(obj)
        except (KeyError, TypeError, ValueError):
            return None

    def encodable_types(self) -> Tuple[type, ...]:
        """Return tuple of types this handler can encode."""
        return (Weather,)

    def encode(self, obj: Any) -> Dict[str, Any] | None:
        """Encode Weather to JSON dict.

        Args:
            obj: Weather instance to encode.

        Returns:
            Dict in PWeather format, or None if not a Weather object.
        """
        return obj.to_json()

decodable_schema() #

Return schema: expects weather-related keys.

Source code in capturegraph-lib/capturegraph/data/typed_json/handlers/weather.py
def decodable_schema(self) -> Dict[str, Tuple[type, ...]]:
    """Return schema: expects weather-related keys."""
    return {
        "temperature_celsius": (int, float),
        "condition": str,
        "humidity_ratio": (int, float),
        "wind_speed_mps": (int, float),
        "date_timestamp": (int, float),
    }

decode(obj) #

Decode JSON dict to Weather.

Parameters:

Name Type Description Default
obj Dict[str, Any]

Dict with PWeather JSON format fields.

required

Returns:

Type Description
Any | None

Weather instance, or None if schema doesn't match.

Source code in capturegraph-lib/capturegraph/data/typed_json/handlers/weather.py
def decode(self, obj: Dict[str, Any]) -> Any | None:
    """Decode JSON dict to Weather.

    Args:
        obj: Dict with PWeather JSON format fields.

    Returns:
        Weather instance, or None if schema doesn't match.
    """
    try:
        return Weather.from_json(obj)
    except (KeyError, TypeError, ValueError):
        return None

encodable_types() #

Return tuple of types this handler can encode.

Source code in capturegraph-lib/capturegraph/data/typed_json/handlers/weather.py
def encodable_types(self) -> Tuple[type, ...]:
    """Return tuple of types this handler can encode."""
    return (Weather,)

encode(obj) #

Encode Weather to JSON dict.

Parameters:

Name Type Description Default
obj Any

Weather instance to encode.

required

Returns:

Type Description
Dict[str, Any] | None

Dict in PWeather format, or None if not a Weather object.

Source code in capturegraph-lib/capturegraph/data/typed_json/handlers/weather.py
def encode(self, obj: Any) -> Dict[str, Any] | None:
    """Encode Weather to JSON dict.

    Args:
        obj: Weather instance to encode.

    Returns:
        Dict in PWeather format, or None if not a Weather object.
    """
    return obj.to_json()