2024-09-23 17:46:02 +01:00
|
|
|
def dt_to_date(datetime):
|
|
|
|
return datetime.date()
|
|
|
|
|
2024-09-22 14:27:43 +01:00
|
|
|
def mangrove_by_country_latest_schema(data):
|
2024-09-20 17:49:31 +01:00
|
|
|
return {
|
|
|
|
"country_with_parent": str(data["country_with_parent"]),
|
|
|
|
"original_pixels": int(data["original_pixels"]),
|
|
|
|
"total_n_pixels": int(data["total_n_pixels"]),
|
|
|
|
"cumulative_pixels_diff": int(data["cumulative_pixels_diff"]),
|
|
|
|
"cumulative_pct_diff": float(data["cumulative_pct_diff"]),
|
|
|
|
}
|
|
|
|
|
2024-09-22 14:27:43 +01:00
|
|
|
def mangrove_by_country_agg_schema(data):
|
|
|
|
return {
|
|
|
|
"country_with_parent": str(data["country_with_parent"]),
|
|
|
|
"year": int(data["year"]),
|
|
|
|
"total_pixels": int(data["total_pixels"])
|
|
|
|
}
|
|
|
|
|
2024-09-23 17:46:02 +01:00
|
|
|
def bitcoin_business_growth_percent_diff_schema(data):
|
2024-09-22 14:27:43 +01:00
|
|
|
return {
|
|
|
|
"country_name": str(data["country_name"]),
|
2024-09-23 17:46:02 +01:00
|
|
|
"first_value": int(data["first_value"]),
|
|
|
|
"date_range": str(f'{dt_to_date(data["first_date"])} to {dt_to_date(data["last_date"])}'),
|
|
|
|
"last_value": int(data["last_value"]),
|
|
|
|
"difference": int(data["difference"]),
|
|
|
|
"percent_difference": str(data["percent_difference"])
|
|
|
|
}
|
|
|
|
|
|
|
|
def bitcoin_business_growth_timeseries_schema(data):
|
|
|
|
return {
|
|
|
|
"country_name": str(data["country_name"]),
|
|
|
|
"date": dt_to_date(data["date"]),
|
|
|
|
"cumulative_value": int(data["cumulative_value"])
|
2024-09-22 14:27:43 +01:00
|
|
|
}
|
2024-09-20 17:49:31 +01:00
|
|
|
|
|
|
|
class DataSerializer:
|
|
|
|
def __init__(self, schema_func):
|
|
|
|
self.schema_func = schema_func
|
|
|
|
|
|
|
|
def serialize_one(self, data) -> dict:
|
|
|
|
return self.schema_func(data)
|
|
|
|
|
|
|
|
def serialize_many(self, data_list) -> list:
|
|
|
|
return [self.serialize_one(data) for data in data_list]
|