view weather_server/common.py @ 33:beedfa8eaa3f

Add grid lines to the graph. - Adds time gridlines on the hour and 15 minutes. - Adds temperature gridlines on 10, 5, and 1 degree intervals. - Increases chart range. - Changes from rounding temperature to floor()ing it.
author Paul Fisher <paul@pfish.zone>
date Sat, 12 Jun 2021 20:22:46 +0000
parents 52ef21607b31
children
line wrap: on
line source

import datetime
import json
import typing as t

import bson
import pytz

BSON_OPTIONS = bson.DEFAULT_CODEC_OPTIONS.with_options(
    tz_aware=True, tzinfo=pytz.UTC)


def bson_encode(data: t.Dict[str, t.Any]) -> bytes:
    return bson.BSON.encode(data, codec_options=BSON_OPTIONS)


class DateEncoder(json.JSONEncoder):

    def default(self, o: t.Any) -> t.Any:
        if not isinstance(o, datetime.datetime):
            return super().default(o)
        return o.timestamp()


JSON_ENCODER = DateEncoder(sort_keys=True)


def json_dumps(data: t.Any) -> str:
    return JSON_ENCODER.encode(data)


def utc_now():
    return datetime.datetime.utcnow().replace(tzinfo=pytz.UTC)