Home > Learning, Lesson, Problem, Projects/Experiments, Python > Python: Covid19 Function Upgrade 1

Python: Covid19 Function Upgrade 1

Learning : Using Beautifulsoup in Python
Subject: Python & Covid19 Function ‘Data By Country Upgrade

In last post (Python and Covid19) we use the Beautifulsoup and other libraries to grab COVID19 data from the internet and display it on the screen using our Main-Menu and some functions, and we said that some of those functions need some enhancement. In this post we will do some upgrade to one of the functions ‘def covid19_in_country()‘ and we will add the version number to it’s name as (def covid19_in_country_v02()).

In the previous Version of the function, we list down all the countries name with there indexes and ask the user to enter the index number of a country to get it’s COVID19 Data. However in this version we will give the user an option to select wither to:
1. Select the Country Name from a list.
2. Write the Country Name.

So if the user go-for option [1] then this will be as version 1, and if the user select option [2] then we will provide the opportunity to Enter Full or Part of a country Name and we will search for similar input (in the country list) and list them. Here is a screen shot of the RUN-Time ..

First we will write a code to create a list of Countries and there Indexes, this new function will called ‘def country_list()’ and it will return a list variable named ‘c_name_list’, here is the code for it.

# Function to create Country List

def country_list() :
    total_rows = soup.find_all('th',class_='covid-total-row')
    Total_Countries = int((total_rows)[0].b.text)
    for x in range (2,Total_Countries) :
                c_name_list.append([x,trs[x].find('a',title=re.compile('2020 coronavirus pandemic in *')).text])

                pass # If any errors, just pass.
    return c_name_list

Now we will split a part that (displaying the Country Data) from the main function ‘covid19_in_country()’ to be as an objet so we can call it any time with passing the country index to it. Here is the code..

# Code to display COVID19 data on the screen.

def show_counry_data(user_c_index) :
    # This will print the country name.
    print('\n\n     ',trs[int(user_c_index)].find('a',title=re.compile('2020 coronavirus pandemic in *')).text)
    # This will print the country information.
    tds = trs[int(user_c_index)].find_all('td')
    print('        Country Rank is: ',int(user_c_index)-1)
    print('        Cases:',tds[0].text.strip())
    print('        Deths:',tds[1].text.strip())
    print('        Recoveries:',tds[2].text.strip())

No we will re-write the main function of covid19_in_country() and we will add version number to it so it will be “covid19_in_country_v02()“. Here is the code ..

# covid19_in_country_v02()

def covid19_in_country_v02() :
    print('\n    Covid19 Country Data.')
    print('\n    ',last_update,'\n')

    while True:

        print('\n   Do you want to:' )
        print('   1. Select the Country name from a list.' )
        print('   2. Write the Country Name.')
        country_as = input('\n   Select from the Menu [1,2]: ')
        if country_as in ['1','2'] :
            print('\n    You Must Select [1 or 2] ... Try Again .. ')

    c_name_list = country_list()
    if country_as =='1' :
    # First we will print-out the list of counries name.
        for cou in range (0,(len(c_name_list)),4) :
                print ('    ',c_name_list[cou],c_name_list[cou+1],c_name_list[cou+2],c_name_list[cou+3])
                pass # if any error just continue.

        while True :
            user_c_index = (input('\n\n    Enter the Index number of the Country to see it''s Data. [e to Exit] '))
            try :
            except :
                if ((str(user_c_index)) in ['e','E']) :
                else :
                    print('\n    You must Enter a Number or ''e''/''E'' to exit')

    if country_as =='2' :
        print('\n   You Select the option to Enter a Country Name.')
        search_country = input('   Enter the Country Name (or Part of it): ')       
        print('  You enter: ',search_country)
        for x in range (0,(len(c_name_list))) :
            try :
                if (search_country.casefold()) in (c_name_list[x][1]).casefold() :
        if (len(c_indexes)==0) :
            print('\n   We can''t find your search, this may due to:')
            print('   1. Spelling error.')
            print('   2. Country Name drops for some technical error.')
            input('\n    ... Press any key ...'')

        else :
            print('\n   We fond {} matches with your search, Enter the Index Number for the\n   one you are looking for: '.format(len(c_indexes)))
            print('\n\n ',c_indexes)

            show_counry_data(int(input('\n   Enter the Counrty Index Now: ')))

    input('   ... Press Any Key ...')

This application and the code was written as a matter of training and learning. The owner/creator is not responsible for any missing or wrong data may exist in the file and/or the Data. (the code and/or the data-sets).

Note That: The file will only contain the changes in the function, NOT the Full COVID19 APP.

To Download my Python code (.py) files Click-Here

Follow me on Twitter..

By: Ali Radwani

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: