示例#1
0
    def _create_custom_dicts(self, dictreader) -> List[Dict]:
        """
        Create an intermediate structure for transformation. The structure of
        dicts in the DictReader is altered to dicts with custom keys, removing
        latitude, longitude, last-updated and others. Also transforms country
        names to preferred/consistent names. Dicts are returned in a list sorted
        alphabetically by country.

        Example resulting dict:
        {
            "country/region": italy,
            "province/state": "",
            "confirmed": "10",
            "recovered": "",
            "deaths": ""
        }

        :param dictreader: DictReader object of data retrieved from GitHub
        :return: list of custom dicts sorted alphabetically by country
        """
        new_dicts = []
        for row in dictreader:
            try:
                country = row[self.COUNTRY_REGION]
                province = row[self.PROVINCE_STATE]
            except KeyError:
                # Older documents have different keys
                country = row[self.COUNTRY_REGION_OLD]
                province = row[self.PROVINCE_STATE_OLD]

            country_transformer = CountryTransformer(country)
            custom_country = country_transformer.transform()

            new_dicts.append({
                self.COUNTRY_REGION_LC: custom_country,
                "province/state": province,
                "confirmed": row["Confirmed"],
                "recovered": row["Recovered"],
                "deaths": row["Deaths"],
            })

        return sorted(new_dicts, key=lambda d: d[self.COUNTRY_REGION_LC])
 def test_transform_returns_united_kingdom(self):
     self.assertEqual(CountryTransformer.transform("uk"), "United Kingdom")
 def test_transform_returns_china(self):
     self.assertEqual(CountryTransformer.transform("Mainland China"), "China")
 def test_transform_returns_vietnam(self):
     self.assertEqual(CountryTransformer.transform("viet nam"), "Vietnam")
 def test_transform_returns_congo_3(self):
     self.assertEqual(CountryTransformer.transform("Republic of The Congo"), "Congo")
 def test_transform_returns_congo_2(self):
     self.assertEqual(CountryTransformer.transform("congo (kinshasa)"), "Congo")
 def test_transform_returns_congo_1(self):
     self.assertEqual(CountryTransformer.transform("congo (brazzaville)"), "Congo")
 def test_transform_returns_united_states(self):
     self.assertEqual(CountryTransformer.transform("us"), "United States")