Basic arithmetic for each snapshot
This commit is contained in:
57
covid.py
57
covid.py
@@ -5,6 +5,21 @@ import datetime
|
|||||||
import requests
|
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:
|
class State:
|
||||||
def __init__(self, population):
|
def __init__(self, population):
|
||||||
self.Population = population
|
self.Population = population
|
||||||
@@ -13,17 +28,47 @@ class State:
|
|||||||
def AddSnapshot(self, ts, positive, negative, pending, hospitalized, dead):
|
def AddSnapshot(self, ts, positive, negative, pending, hospitalized, dead):
|
||||||
self.Snapshots[ts] = Snapshot(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):
|
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:
|
class Snapshot:
|
||||||
def __init__(self, positive, negative, pending, hospitalized, dead):
|
def __init__(self, positive, negative, pending, hospitalized, dead):
|
||||||
self.Positive = positive
|
self.Positive = positive or 0
|
||||||
self.Negative = negative
|
self.Negative = negative or 0
|
||||||
self.Pending = pending
|
self.Pending = pending or 0
|
||||||
self.Hospitalized = hospitalized
|
self.Hospitalized = hospitalized or 0
|
||||||
self.Dead = dead
|
self.Dead = dead or 0
|
||||||
|
|
||||||
|
def Tests(self):
|
||||||
|
return self.Positive + self.Negative
|
||||||
|
|
||||||
|
|
||||||
states = {}
|
states = {}
|
||||||
|
|||||||
Reference in New Issue
Block a user