From 370b3f2eace7b4176241b0950a596bfa931b0ce6 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Sun, 22 Mar 2020 05:53:36 +0000 Subject: [PATCH] Basic arithmetic for each snapshot --- covid.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 51 insertions(+), 6 deletions(-) diff --git a/covid.py b/covid.py index 2d934a3..ee4376d 100755 --- a/covid.py +++ b/covid.py @@ -5,6 +5,21 @@ import datetime import requests +def with_snapshot(func): + def wrapper(self, ts=None): + if not ts: + ts = self.Latest() + return func(self, self.Snapshots[ts]) + return wrapper + + +def per_million(func): + def wrapper(self, *args, **kwargs): + count = func(self, *args, **kwargs) + return round(count * 1000000 / self.Population) + return wrapper + + class State: def __init__(self, population): self.Population = population @@ -13,17 +28,47 @@ class State: def AddSnapshot(self, ts, positive, negative, pending, hospitalized, dead): self.Snapshots[ts] = Snapshot(positive, negative, pending, hospitalized, dead) + def Latest(self): + return max(self.Snapshots) + + @with_snapshot + @per_million + def TestsPerMillion(self, snap): + return snap.Tests() + + @with_snapshot + @per_million + def PositivePerMillion(self, snap): + return snap.Positive + + @with_snapshot + @per_million + def HospitalizedPerMillion(self, snap): + return snap.Hospitalized + + @with_snapshot + @per_million + def DeadPerMillion(self, snap): + return snap.Dead + + @with_snapshot + def PositivePerTestBP(self, snap): + return round(snap.Positive * 10000 / snap.Tests()) if snap.Tests() else 0 + def __str__(self): - return f'{self.Population:>10}=pop {len(self.Snapshots):>4}=snaps' + return f'{self.Population:>10}=pop {len(self.Snapshots):>4}=snaps {self.TestsPerMillion():>6}=tpm {self.PositivePerMillion():>6}=ppm {self.HospitalizedPerMillion():>6}=hpm {self.DeadPerMillion():>6}=dpm {self.PositivePerTestBP():>4}=p‱' class Snapshot: def __init__(self, positive, negative, pending, hospitalized, dead): - self.Positive = positive - self.Negative = negative - self.Pending = pending - self.Hospitalized = hospitalized - self.Dead = dead + self.Positive = positive or 0 + self.Negative = negative or 0 + self.Pending = pending or 0 + self.Hospitalized = hospitalized or 0 + self.Dead = dead or 0 + + def Tests(self): + return self.Positive + self.Negative states = {}