Skip to content

Commit

Permalink
Update deprecated calls
Browse files Browse the repository at this point in the history
run ruff on code
  • Loading branch information
mcarans committed Jul 8, 2024
1 parent 72ee7a8 commit f476b73
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 42 deletions.
45 changes: 24 additions & 21 deletions ipc.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ def parse_date(datestring):
return date.replace(tzinfo=timezone.utc)

analysis_date = parse_date(most_recent_analysis["analysis_date"])
if analysis_date <= self.state.get(countryiso3, self.default_start_date):
if analysis_date <= self.state.get(countryiso3,
self.default_start_date):
update = False
else:
update = True
Expand All @@ -117,11 +118,11 @@ def parse_date_range(date_range):
return startdatestr, enddatestr

def add_country_subnational_rows(
base_row,
location,
rows,
rows_wide,
analysis=None,
base_row,
location,
rows,
rows_wide,
analysis=None,
):
if analysis is None:
analysis = location
Expand Down Expand Up @@ -157,7 +158,8 @@ def add_country_subnational_rows(
else:
colname = f"Phase {phase} number {projection_name_l}"
row_wide[colname] = affected
percentage = location.get(f"{prefix}_percentage{projection_suffix}")
percentage = location.get(
f"{prefix}_percentage{projection_suffix}")
row["Percentage"] = percentage
if prefix != "estimated":
row_wide[
Expand Down Expand Up @@ -185,7 +187,8 @@ def add_country_rows(analysis, rows, rows_wide):
)

def add_subnational_rows(
analysis, group_rows, group_rows_wide, area_rows, area_rows_wide
analysis, group_rows, group_rows_wide, area_rows,
area_rows_wide
):
def process_areas(adm_row, adm):
if adm["areas"] is None:
Expand Down Expand Up @@ -326,7 +329,7 @@ def generate_dataset_and_showcase(self, folder, output):
filename = f"ipc_{countryiso3lower}_national_long_latest.csv"
resourcedata = {
"name": filename,
"description": f"Latest IPC national data in long form with HXL tags",
"description": "Latest IPC national data in long form with HXL tags",
}
country_rows = output["country_rows_latest"]
if not country_rows:
Expand All @@ -350,7 +353,7 @@ def generate_dataset_and_showcase(self, folder, output):
filename = f"ipc_{countryiso3lower}_national_wide_latest.csv"
resourcedata = {
"name": filename,
"description": f"Latest IPC national data in wide form with HXL tags",
"description": "Latest IPC national data in wide form with HXL tags",
}
success, results = dataset.generate_resource_from_iterable(
list(country_rows_wide[0].keys()),
Expand All @@ -372,7 +375,7 @@ def generate_dataset_and_showcase(self, folder, output):
{
"name": f"{name}-showcase",
"title": f"{title} showcase",
"notes": f"IPC-CH Dashboard",
"notes": "IPC-CH Dashboard",
"url": showcase_url,
"image_url": "https://www.ipcinfo.org/fileadmin/user_upload/ipcinfo/img/dashboard_thumbnail.jpg",
}
Expand All @@ -383,7 +386,7 @@ def generate_dataset_and_showcase(self, folder, output):
filename = f"ipc_{countryiso3lower}_level1_long_latest.csv"
resourcedata = {
"name": filename,
"description": f"Latest IPC level 1 data in long form with HXL tags",
"description": "Latest IPC level 1 data in long form with HXL tags",
}
success, results = dataset.generate_resource_from_iterable(
list(group_rows[0].keys()),
Expand All @@ -399,7 +402,7 @@ def generate_dataset_and_showcase(self, folder, output):
filename = f"ipc_{countryiso3lower}_level1_wide_latest.csv"
resourcedata = {
"name": filename,
"description": f"Latest IPC level 1 data in wide form with HXL tags",
"description": "Latest IPC level 1 data in wide form with HXL tags",
}
success, results = dataset.generate_resource_from_iterable(
list(group_rows_wide[0].keys()),
Expand All @@ -414,7 +417,7 @@ def generate_dataset_and_showcase(self, folder, output):
filename = f"ipc_{countryiso3lower}_area_long_latest.csv"
resourcedata = {
"name": filename,
"description": f"Latest IPC area data in long form with HXL tags",
"description": "Latest IPC area data in long form with HXL tags",
}
success, results = dataset.generate_resource_from_iterable(
list(area_rows[0].keys()),
Expand All @@ -429,7 +432,7 @@ def generate_dataset_and_showcase(self, folder, output):
filename = f"ipc_{countryiso3lower}_area_wide_latest.csv"
resourcedata = {
"name": filename,
"description": f"Latest IPC area data in wide form with HXL tags",
"description": "Latest IPC area data in wide form with HXL tags",
}
success, results = dataset.generate_resource_from_iterable(
list(area_rows_wide[0].keys()),
Expand All @@ -448,7 +451,7 @@ def generate_dataset_and_showcase(self, folder, output):
filename = f"ipc_{countryiso3lower}_national_long.csv"
resourcedata = {
"name": filename,
"description": f"All IPC national data in long form with HXL tags",
"description": "All IPC national data in long form with HXL tags",
}
success, results = dataset.generate_resource_from_iterable(
list(country_rows[0].keys()),
Expand All @@ -462,7 +465,7 @@ def generate_dataset_and_showcase(self, folder, output):
filename = f"ipc_{countryiso3lower}_national_wide.csv"
resourcedata = {
"name": filename,
"description": f"All IPC national data in wide form with HXL tags",
"description": "All IPC national data in wide form with HXL tags",
}
success, results = dataset.generate_resource_from_iterable(
list(country_rows_wide[0].keys()),
Expand All @@ -478,7 +481,7 @@ def generate_dataset_and_showcase(self, folder, output):
filename = f"ipc_{countryiso3lower}_level1_long.csv"
resourcedata = {
"name": filename,
"description": f"All IPC level 1 data in long form with HXL tags",
"description": "All IPC level 1 data in long form with HXL tags",
}
success, results = dataset.generate_resource_from_iterable(
list(group_rows[0].keys()),
Expand All @@ -494,7 +497,7 @@ def generate_dataset_and_showcase(self, folder, output):
filename = f"ipc_{countryiso3lower}_level1_wide.csv"
resourcedata = {
"name": filename,
"description": f"All IPC level 1 data in wide form with HXL tags",
"description": "All IPC level 1 data in wide form with HXL tags",
}
success, results = dataset.generate_resource_from_iterable(
list(group_rows_wide[0].keys()),
Expand All @@ -509,7 +512,7 @@ def generate_dataset_and_showcase(self, folder, output):
filename = f"ipc_{countryiso3lower}_area_long.csv"
resourcedata = {
"name": filename,
"description": f"All IPC area data in long form with HXL tags",
"description": "All IPC area data in long form with HXL tags",
}
success, results = dataset.generate_resource_from_iterable(
list(area_rows[0].keys()),
Expand All @@ -524,7 +527,7 @@ def generate_dataset_and_showcase(self, folder, output):
filename = f"ipc_{countryiso3lower}_area_wide.csv"
resourcedata = {
"name": filename,
"description": f"All IPC area data in wide form with HXL tags",
"description": "All IPC area data in wide form with HXL tags",
}
success, results = dataset.generate_resource_from_iterable(
list(area_rows_wide[0].keys()),
Expand Down
24 changes: 14 additions & 10 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from hdx.api.configuration import Configuration
from hdx.facades.infer_arguments import facade
from hdx.utilities.downloader import Download
from hdx.utilities.path import progress_storing_folder, wheretostart_tempdir_batch
from hdx.utilities.path import progress_storing_folder, \
wheretostart_tempdir_batch
from hdx.utilities.retriever import Retrieve
from hdx.utilities.state import State
from ipc import IPC
Expand All @@ -34,18 +35,20 @@ def main(save: bool = False, use_saved: bool = False) -> None:

configuration = Configuration.read()
with State(
"analysis_dates.txt",
State.dates_str_to_country_date_dict,
State.country_date_dict_to_dates_str,
"analysis_dates.txt",
State.dates_str_to_country_date_dict,
State.country_date_dict_to_dates_str,
) as state:
state_dict = deepcopy(state.get())
with wheretostart_tempdir_batch(lookup) as info:
folder = info["folder"]
with Download(
extra_params_yaml=join(expanduser("~"), ".extraparams.yaml"),
extra_params_lookup=lookup,
extra_params_yaml=join(expanduser("~"),
".extraparams.yaml"),
extra_params_lookup=lookup,
) as downloader:
_, iterator = downloader.get_tabular_rows(join("config", "ch_countries.csv"), dict_form=True)
_, iterator = downloader.get_tabular_rows(
join("config", "ch_countries.csv"), dict_form=True)
ch_countries = [row["ISO_3"] for row in iterator]
retriever = Retrieve(
downloader, folder, "saved_data", folder, save, use_saved
Expand All @@ -55,8 +58,8 @@ def main(save: bool = False, use_saved: bool = False) -> None:
logger.info(f"Number of countries: {len(countries)}")

def create_dataset(
dataset,
showcase,
dataset,
showcase,
):
if not dataset:
return
Expand All @@ -81,7 +84,8 @@ def create_dataset(
showcase.add_dataset(dataset)

country_data_updated = False
for _, country in progress_storing_folder(info, countries, "iso3"):
for _, country in progress_storing_folder(info, countries,
"iso3"):
countryiso = country["iso3"]
output = ipc.get_country_data(countryiso)
if output:
Expand Down
29 changes: 18 additions & 11 deletions tests/test_ipc.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ def input_folder(self, fixtures):
return join(fixtures, "input")

def test_generate_datasets_and_showcases(
self, configuration, fixtures, input_folder
self, configuration, fixtures, input_folder
):
with temp_dir(
"test_ipc", delete_on_success=True, delete_on_failure=False
"test_ipc", delete_on_success=True, delete_on_failure=False
) as folder:
with Download() as downloader:
retriever = Retrieve(
Expand All @@ -87,15 +87,16 @@ def check_files(resources):
]

output = ipc.get_country_data("AFG")
dataset, showcase = ipc.generate_dataset_and_showcase(folder, output)
dataset, showcase = ipc.generate_dataset_and_showcase(folder,
output)
assert dataset == {
"data_update_frequency": "-2",
"dataset_date": "[2017-05-01T00:00:00 TO 2023-10-31T23:59:59]",
"groups": [{"name": "afg"}],
"maintainer": "196196be-6037-4488-8b71-d786adf4c081",
"name": "afghanistan-acute-food-insecurity-country-data",
"notes": "There is also a [global "
"dataset](https://stage.data-humdata-org.ahconu.org/dataset/global-acute-food-insecurity-country-data).",
"dataset](https://stage.data-humdata-org.ahconu.org/dataset/global-acute-food-insecurity-country-data).",
"owner_org": "da501ffc-aadb-43f5-9d28-8fa572fd9ce0",
"subnational": "1",
"tags": [
Expand Down Expand Up @@ -228,7 +229,8 @@ def check_files(resources):
}

output = ipc.get_country_data("AGO")
dataset, showcase = ipc.generate_dataset_and_showcase(folder, output)
dataset, showcase = ipc.generate_dataset_and_showcase(folder,
output)
resources = dataset.get_resources()
assert resources == [
{
Expand Down Expand Up @@ -311,22 +313,25 @@ def check_files(resources):
]
check_files(resources)
output = ipc.get_country_data("CAF")
dataset, showcase = ipc.generate_dataset_and_showcase(folder, output)
dataset, showcase = ipc.generate_dataset_and_showcase(folder,
output)
check_files(dataset.get_resources())
output = ipc.get_country_data("ETH")
dataset, showcase = ipc.generate_dataset_and_showcase(folder, output)
dataset, showcase = ipc.generate_dataset_and_showcase(folder,
output)
check_files(dataset.get_resources())

output = ipc.get_all_data()
dataset, showcase = ipc.generate_dataset_and_showcase(folder, output)
dataset, showcase = ipc.generate_dataset_and_showcase(folder,
output)
assert dataset == {
"data_update_frequency": "-2",
"dataset_date": "[2017-02-01T00:00:00 TO 2024-03-31T23:59:59]",
"groups": [{"name": "world"}],
"maintainer": "196196be-6037-4488-8b71-d786adf4c081",
"name": "global-acute-food-insecurity-country-data",
"notes": "There are also [country "
"datasets](https://stage.data-humdata-org.ahconu.org/organization/da501ffc-aadb-43f5-9d28-8fa572fd9ce0)",
"datasets](https://stage.data-humdata-org.ahconu.org/organization/da501ffc-aadb-43f5-9d28-8fa572fd9ce0)",
"owner_org": "da501ffc-aadb-43f5-9d28-8fa572fd9ce0",
"subnational": "1",
"tags": [
Expand Down Expand Up @@ -459,7 +464,9 @@ def check_files(resources):
"AGO": datetime(2021, 6, 1, 0, 0, tzinfo=timezone.utc),
"CAF": datetime(2023, 4, 1, 0, 0, tzinfo=timezone.utc),
"DEFAULT": datetime(2017, 1, 1, 0, 0, tzinfo=timezone.utc),
"END_DATE": datetime(2024, 3, 31, 0, 0, tzinfo=timezone.utc),
"END_DATE": datetime(2024, 3, 31, 0, 0,
tzinfo=timezone.utc),
"ETH": datetime(2021, 5, 1, 0, 0, tzinfo=timezone.utc),
"START_DATE": datetime(2017, 2, 1, 0, 0, tzinfo=timezone.utc),
"START_DATE": datetime(2017, 2, 1, 0, 0,
tzinfo=timezone.utc),
}

0 comments on commit f476b73

Please sign in to comment.