Master complex data transformation and validation using Marshmallow schemas. Learn to serialize Python objects to JSON, deserialize and validate incoming data, handle nested relationships, and implement custom validation logic essential for robust API development.
This document explains the Marshmallow example for processing weather station data.
The example demonstrates how to:
from marshmallow import Schema, fields, validates, ValidationError, post_load from datetime import datetime from typing import List, Dict
This section imports necessary modules from Marshmallow and Python's standard library.
class LocationSchema(Schema): latitude = fields.Float(required=True) longitude = fields.Float(required=True) @validates('latitude') def validate_latitude(self, value): if not -90 <= value <= 90: raise ValidationError("Latitude must be between -90 and 90.") @validates('longitude') def validate_longitude(self, value): if not -180 <= value <= 180: raise ValidationError("Longitude must be between -180 and 180.")
Here, we define the LocationSchema with latitude and longitude fields. Custom validators ensure these values are within valid ranges.
class WeatherReadingSchema(Schema): temperature = fields.Float(required=True) humidity = fields.Float(required=True) wind_speed = fields.Float(required=True) timestamp = fields.DateTime(required=True) @validates('humidity') def validate_humidity(self, value): if not 0 <= value <= 100: raise ValidationError("Humidity must be between 0 and 100.")
The WeatherReadingSchema defines fields for a single weather reading. It includes a custom validator for humidity.
class WeatherStationSchema(Schema): station_id = fields.String(required=True) location = fields.Nested(LocationSchema, required=True) readings = fields.List(fields.Nested(WeatherReadingSchema), required=True) @post_load def make_weather_station(self, data, **kwargs): return WeatherStation(**data)
This schema combines the previous schemas, demonstrating nested structures. The post_load decorator creates a WeatherStation object after successful data loading.
class WeatherStation: def __init__(self, station_id: str, location: Dict, readings: List[Dict]): self.station_id = station_id self.location = location self.readings = readings
This class represents a weather station, used to create Python objects from validated data.
def process_weather_data(data: Dict): try: schema = WeatherStationSchema() weather_station = schema.load(data) print(f"Processed data for station {weather_station.station_id}") print(f"Location: {weather_station.location}") print(f"Number of readings: {len(weather_station.readings)}") return schema.dump(weather_station) except ValidationError as err: print(f"Validation error: {err.messages}") return None
This function demonstrates how to use the schema for data validation and processing. It handles validation errors and returns the processed data.
weather_data = { "station_id": "WS001", "location": { "latitude": 40.7128, "longitude": -74.0060 }, "readings": [ { "temperature": 22.5, "humidity": 60.0, "wind_speed": 5.5, "timestamp": "2023-05-15T14:30:00" }, { "temperature": 23.0, "humidity": 58.5, "wind_speed": 6.0, "timestamp": "2023-05-15T15:00:00" } ] } result = process_weather_data(weather_data) print(result)
This section provides sample data and demonstrates how to use the process_weather_data function.
To run this example:
Ensure you have Marshmallow installed:
pip install marshmallow
Save the code in a file (e.g., marshmallow_example.py)
Run the script:
python marshmallow_example.py
The script will validate the sample weather data, create a WeatherStation object, and then serialize and print the processed data.