How to get COVID 19 update using Covid module in Python? - GeeksforGeeks

03 Jun, 2020
A new Python library which tells the COVID-19 related information (country-wise) and it show that how many cases of confirmed, active, deaths, recovered found in that particular Country.
Requirement:You have python package named COVID and python >= 3.6
Installation:
pip install covid
Dependencies:
pydantic
requests
Example:
from covid import Covid    covid = Covid()india = covid.get_status_by_country_name("india")  data ={    key:india[key]    for key in india.keys() and {'confirmed',                                  'active',                                 'deaths',                                 'recovered'}}  print(data)
Output:
{‘confirmed’: 119419, ‘active’: 66863, ‘recovered’: 48957, ‘deaths’: 3599}
Let’s look at the modules basic functionality –
List Countries and their Id’s: This comes in need when you need to know the names of countries while using get_status_by_country_name, eg to use ‘America’ or ‘United States of America’ or ‘US’from covid import Covid  covid = Covid()countries = covid.list_countries()  print(countries)Output:[{‘id’: ’18’, ‘name’: ‘US’}, {‘id’: ’14’, ‘name’: ‘Russia’}, {‘id’: ’22’, ‘name’: ‘Brazil’}, {‘id’: ’17’, ‘name’: ‘United Kingdom’}, {‘id’: ’19’, ‘name’: ‘Spain’}, {‘id’: ’11’, ‘name’: ‘Italy’}, {‘id’: ‘7’, ‘name’: ‘France’}, {‘id’: ‘8’, ‘name’: ‘Germany’}, {‘id’: ‘176’, ‘name’: ‘Turkey’}, {‘id’: ’93’, ‘name’: ‘Iran’}, {‘id’: ’91’, ‘name’: ‘India’}, {‘id’: ‘141’, ‘name’: ‘Peru’}, {‘id’: ‘4’, ‘name’: ‘China’}, {‘id’: ‘3’, ‘name’: ‘Canada’}, {‘id’: ‘153’, ‘name’: ‘Saudi Arabia’}, {‘id’: ’20’, ‘name’: ‘Mexico’},Note: The list is too long, the above output is just a part of the list.
from covid import Covid  covid = Covid()countries = covid.list_countries()  print(countries)
Output:
[{‘id’: ’18’, ‘name’: ‘US’}, {‘id’: ’14’, ‘name’: ‘Russia’}, {‘id’: ’22’, ‘name’: ‘Brazil’}, {‘id’: ’17’, ‘name’: ‘United Kingdom’}, {‘id’: ’19’, ‘name’: ‘Spain’}, {‘id’: ’11’, ‘name’: ‘Italy’}, {‘id’: ‘7’, ‘name’: ‘France’}, {‘id’: ‘8’, ‘name’: ‘Germany’}, {‘id’: ‘176’, ‘name’: ‘Turkey’}, {‘id’: ’93’, ‘name’: ‘Iran’}, {‘id’: ’91’, ‘name’: ‘India’}, {‘id’: ‘141’, ‘name’: ‘Peru’}, {‘id’: ‘4’, ‘name’: ‘China’}, {‘id’: ‘3’, ‘name’: ‘Canada’}, {‘id’: ‘153’, ‘name’: ‘Saudi Arabia’}, {‘id’: ’20’, ‘name’: ‘Mexico’},
Note: The list is too long, the above output is just a part of the list.
Get Data: To get COVID-19 related information.from covid import Covid    covid = Covid()print(covid.get_data())Output:[{‘id’: ’18’, ‘country’: ‘US’, ‘confirmed’: 1577758, ‘active’: 1181132, ‘deaths’: 94729, ‘recovered’: 298418, ‘latitude’: 40.0, ‘longitude’: -100.0, ‘last_update’: 1590143562000}, {‘id’: ’14’, ‘country’: ‘Russia’, ‘confirmed’: 326448, ‘active’: 223374, ‘deaths’: 3249, ‘recovered’: 99825, ‘latitude’: 61.524, ‘longitude’: 105.3188, ‘last_update’: 1590143562000},Note: The list is too long, the above output is just a part of the list.
from covid import Covid    covid = Covid()print(covid.get_data())
Output:
[{‘id’: ’18’, ‘country’: ‘US’, ‘confirmed’: 1577758, ‘active’: 1181132, ‘deaths’: 94729, ‘recovered’: 298418, ‘latitude’: 40.0, ‘longitude’: -100.0, ‘last_update’: 1590143562000}, {‘id’: ’14’, ‘country’: ‘Russia’, ‘confirmed’: 326448, ‘active’: 223374, ‘deaths’: 3249, ‘recovered’: 99825, ‘latitude’: 61.524, ‘longitude’: 105.3188, ‘last_update’: 1590143562000},
Note: The list is too long, the above output is just a part of the list.
Get Status By Country ID: To get COVID-19 related information by Country Idfrom covid import Covid  covid = Covid()cases = covid.get_status_by_country_id(18)  print(cases)Output:{‘id’: ’18’, ‘country’: ‘US’, ‘confirmed’: 1541110, ‘active’: 1154535, ‘deaths’: 92712, ‘recovered’: 289392, ‘latitude’: 40.0, ‘longitude’: -100.0, ‘last_update’: 1590003166000}
from covid import Covid  covid = Covid()cases = covid.get_status_by_country_id(18)  print(cases)
Output:
{‘id’: ’18’, ‘country’: ‘US’, ‘confirmed’: 1541110, ‘active’: 1154535, ‘deaths’: 92712, ‘recovered’: 289392, ‘latitude’: 40.0, ‘longitude’: -100.0, ‘last_update’: 1590003166000}
Get Status By Country Namefrom covid import Covid  covid = Covid()italy_cases = covid.get_status_by_country_name("italy")  print(italy_cases)Output:{‘id’: ’11’, ‘country’: ‘Italy’, ‘confirmed’: 227364, ‘active’: 62752, ‘deaths’: 32330, ‘recovered’: 132282, ‘latitude’: 41.8719, ‘longitude’: 12.5674, ‘last_update’: 1590003166000}
from covid import Covid  covid = Covid()italy_cases = covid.get_status_by_country_name("italy")  print(italy_cases)
Output:
{‘id’: ’11’, ‘country’: ‘Italy’, ‘confirmed’: 227364, ‘active’: 62752, ‘deaths’: 32330, ‘recovered’: 132282, ‘latitude’: 41.8719, ‘longitude’: 12.5674, ‘last_update’: 1590003166000}
Get Total Confirmed cases, Active cases, Recovered cases and Deathsfrom covid import Covid    covid = Covid()  confirmed = covid.get_total_confirmed_cases()print('Confirmed :', end =" ")print(confirmed)  active = covid.get_total_active_cases()print("Active:", end =" ")print(active)  recovered = covid.get_total_recovered()print('Recovered:', end =" ")print(recovered)  deaths = covid.get_total_deaths()print('Deaths:', end =" ")print(deaths)Output:Confirmed : 4955312
Active: 2750033
Recovered: 1874998
Deaths: 325810
from covid import Covid    covid = Covid()  confirmed = covid.get_total_confirmed_cases()print('Confirmed :', end =" ")print(confirmed)  active = covid.get_total_active_cases()print("Active:", end =" ")print(active)  recovered = covid.get_total_recovered()print('Recovered:', end =" ")print(recovered)  deaths = covid.get_total_deaths()print('Deaths:', end =" ")print(deaths)
Output:
Confirmed : 4955312
Active: 2750033
Recovered: 1874998
Deaths: 325810
python-utility
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Defaultdict in Python
How to Install PIP on Windows ?
Deque in Python
Bar Plot in Matplotlib
Check if element exists in list in Python
Python math function | sqrt()
Python | Output Formatting
Python - Pandas dataframe.append()
How to drop one or multiple columns in Pandas Dataframe
Python Classes and Objects