[docs]classAfricaCDC(CountryTestBase):location:str="ACDC"# Arbitrary location to pass checksunits:str="tests performed"_base_url=("https://services8.arcgis.com/vWozsma9VzGndzx7/ArcGIS/rest/services/""DailyCOVIDDashboard_5July21_1/FeatureServer/0/")source_url_ref:str="https://africacdc.org/covid-19/"source_label:str="Africa Centres for Disease Control and Prevention"date:str=Nonecolumns_use:list=["Country","Tests_Conducted",]rename_columns:dict={"Country":"location","Tests_Conducted":"Cumulative total",}@propertydefsource_url(self):returnf"{self._base_url}/query?f=json&where=1=1&outFields=*"@propertydefsource_url_date(self):returnf"{self._base_url}?f=pjson"
[docs]defread(self)->pd.DataFrame:# Pull data from APIdata=request_json(self.source_url)df=self._parse_data(data)returndf
[docs]defpipe_rename_countries(self,df:pd.DataFrame)->pd.DataFrame:"""Renames countries to match OWID naming convention."""df["location"]=df.location.replace(ACDC_COUNTRIES)returndf
[docs]defpipe_filter_entries(self,df:pd.DataFrame)->pd.DataFrame:""" Gets valid entries: - Countries not coming from OWID (avoid loop) """df=df[df.location.isin(ACDC_COUNTRIES.values())]returndf
[docs]defpipe_metadata(self,df:pd.DataFrame)->pd.DataFrame:"""Adds metadata to DataFrame"""mapping={"Country":df["location"],"Units":self.units,"Notes":self.notes,"Source URL":self.source_url_ref,"Source label":self.source_label,"Date":self.date,}mapping={k:vfork,vinmapping.items()ifknotindf}self._check_attributes(mapping)returndf.assign(**mapping)
[docs]defincrement_countries(self,df:pd.DataFrame):"""Exports data to the relevant csv and logs the confirmation."""locations=set(df.location)df["Cumulative total"].replace(",","",regex=True,inplace=True)df["Cumulative total"]=df["Cumulative total"].apply(pd.to_numeric,errors="coerce")forlocationinlocations:df_c=df[df.location==location]df_c=df_c.dropna(subset=["Cumulative total"],how="all",)df_current=pd.read_csv(os.path.join(PATHS.INTERNAL_OUTPUT_TEST_MAIN_DIR,f"{location}.csv"))# Ensure that cumulative total has changed since last updateifnotdf_c.emptyanddf_c["Cumulative total"].max()>df_current["Cumulative total"].max():self.export_datafile(df_c,filename=location,attach=True)logger.info(f"\tcowidev.testing.incremental.africacdc.{location}: SUCCESS ✅")
[docs]defpipeline(self,df:pd.DataFrame):"""Pipeline for data"""return(df.pipe(self.pipe_rename_columns).pipe(self.pipe_rename_countries).pipe(self.pipe_filter_entries).pipe(self.pipe_metadata))