diff --git a/src/covidify/cli.py b/src/covidify/cli.py index 9c15ce6..07e21ff 100644 --- a/src/covidify/cli.py +++ b/src/covidify/cli.py @@ -1,3 +1,19 @@ +#behavioural design pattern on cli.py +#chain of handlers + +#--------------------------------------------------------------------------------------------------------------------------------------------------- +#pseudo code +#the handler interface declares a method for building a chain of +#handlers.it also declares a method for executing a request +#interface compo_help is + #method show help() +#the base class for simple components + + + + +#--------------------------------------------------------------------------------------------------------------------------------------------------- + import os import sys import click @@ -6,81 +22,105 @@ from covidify.config import SCRIPT, LOG_TOP_N_COUNTRIES, DAYS_IN_FUTURE from covidify.utils.utils import replace_arg_space from covidify.list_countries import get_countries - +from abc import ABC,abstractmethod #include abstract method +#------------this code is chaned with observer behavioural design pattern----------------------------------- USER = getpass.getuser() #get the path of covidify in site-packages env = covidify.__path__[0] - -def check_output_folder(var, country_str, msg): - ''' - Check if the output folder is valid, if not - just default to dekstop - ''' +class U: + '''represents what is being observed''' + def __init__(self): + '''create an empty observer list''' + self._observer=[] + def notify(self,modifier=none): + '''alerts the observers''' + for observer in self._observers: + if modifier!=observer: + observer.update(self) + def attach(self,observer): + '''if the observer is not in the list,append it into the list''' + if observer not in self._observers: + self._observers.append(observer) + - if not var: - print('%sMESSAGE: %s' % (' '*5, msg)) - if country_str == 'Global': - return os.path.join('/Users', USER, 'Desktop', 'covidify-output') +#------------------------------------------------------------- +class P_country(U): + ''' monitor the object''' + def __init__(self,name=''): + U.__init__(self) + self.name=name + + def check_output_folder(var, country_str, msg): + ''' + Check if the output folder is valid, if not + just default to dekstop + ''' + self.notify() + if not var: + print('%sMESSAGE: %s' % (' '*5, msg)) + if country_str == 'Global': + return os.path.join('/Users', USER, 'Desktop', 'covidify-output') + else: + return os.path.join('/Users', USER, 'Desktop', 'covidify-output-{}'.format(country_str)) else: - return os.path.join('/Users', USER, 'Desktop', 'covidify-output-{}'.format(country_str)) - else: - return var + return var -def check_forecast_days(var, msg): - ''' - Default days for forecasting - ''' - if not var: - return DAYS_IN_FUTURE - else: - return var + def check_forecast_days(var, msg): + ''' + Default days for forecasting + ''' + self.notify() + if not var: + return DAYS_IN_FUTURE + else: + return var -def check_top_countries(var, msg): - ''' - Check number of countries for the log plot - ''' - - if not var: - print('%sMESSAGE: %s' % (' '*5, msg)) - return LOG_TOP_N_COUNTRIES - else: - return var + def check_top_countries(var, msg): + ''' + Check number of countries for the log plot + ''' + self.notify() + if not var: + print('%sMESSAGE: %s' % (' '*5, msg)) + return LOG_TOP_N_COUNTRIES + else: + return var -def check_source_arg(var, msg): - ''' - Check if the datasource is valid, if not then just - default to the john hopkin github repo - ''' - - if var is None: - print('%sMESSAGE: %s' % (' '*5, msg)) - return 'JHU' - elif 'wiki' in var or 'JHU' in var: - return var - else: - print('%sMESSAGE: %s' % (' '*5, 'invalid source given')) - sys.exit() + def check_source_arg(var, msg): + ''' + Check if the datasource is valid, if not then just + default to the john hopkin github repo + ''' + self.notify() + if var is None: + print('%sMESSAGE: %s' % (' '*5, msg)) + return 'JHU' + elif 'wiki' in var or 'JHU' in var: + return var + else: + print('%sMESSAGE: %s' % (' '*5, 'invalid source given')) + sys.exit() -def check_country(country, msg): - ''' - Do some regex work on passed country string - because multi word args are not supported - ''' - - if not country: - print('%sMESSAGE: %s' % (' '*5, msg)) - return 'Global' - else: - country_str = replace_arg_space(country[0]) - return country_str - -def check_list_flag(flag, msg): + def check_country(country, msg): + ''' + Do some regex work on passed country string + because multi word args are not supported + ''' + self.notify() + if not country: + print('%sMESSAGE: %s' % (' '*5, msg)) + return 'Global' + else: + country_str = replace_arg_space(country[0]) + return country_str - if not flag: - print('%sMESSAGE: %s' % (' '*5, msg)) - sys.exit(1) - else: - return flag + def check_list_flag(flag, msg): + self.notify() + if not flag: + print('%sMESSAGE: %s' % (' '*5, msg)) + sys.exit(1) + else: + return flag ############################################################ @@ -104,17 +144,26 @@ def run(output, source, country, top, forecast): ''' #Do checks on args - country_str = check_country(country, '\033[1;31m No country specified, defaulting to global cases \033[0;0m') - output = check_output_folder(output, country_str, '\033[1;31m No output directory given, defaulting to /Users/' + USER + '/Desktop/ \033[0;0m') - source = check_source_arg(source, '\033[1;31m No source given, defaulting to John Hopkin CSSE github repo \033[0;0m') - top = check_top_countries(top, '\033[1;31m No top countries given, defaulting to top ' + str(LOG_TOP_N_COUNTRIES) + ' \033[0;0m') - forecast = check_forecast_days(forecast, '\033[1;31m No days for forecasting given, defaulting to ' + str(DAYS_IN_FUTURE) + ' \033[0;0m') + #here mulltiple observer on user can be used + #observer checke any changes in user and the executes all other related functions which depends on user. + +U_1=P_country('USER') +country_str = check_country(country, '\033[1;31m No country specified, defaulting to global cases \033[0;0m') +output = check_output_folder(output, country_str, '\033[1;31m No output directory given, defaulting to /Users/' + USER + '/Desktop/ \033[0;0m') +source = check_source_arg(source, '\033[1;31m No source given, defaulting to John Hopkin CSSE github repo \033[0;0m') +top = check_top_countries(top, '\033[1;31m No top countries given, defaulting to top ' + str(LOG_TOP_N_COUNTRIES) + ' \033[0;0m') +forecast = check_forecast_days(forecast, '\033[1;31m No days for forecasting given, defaulting to ' + str(DAYS_IN_FUTURE) + ' \033[0;0m') + +os.system(env + SCRIPT + ' ' + env + ' ' + output + ' ' + source + ' ' + country_str + ' ' + str(top) + ' ' + str(forecast)) +U_1.attach(country_str) +U_1.attach(output) +U_1.attach(source) +U_1.attach(top) +U_1.attach(forecast) - os.system(env + SCRIPT + ' ' + env + ' ' + output + ' ' + source + ' ' + country_str + ' ' + str(top) + ' ' + str(forecast)) - - @click.option('--countries', help='List countries that have had confirmed cases.', is_flag=True) @cli.command() + def list(countries): ''' List all the countries that have confirmed cases. @@ -122,4 +171,4 @@ def list(countries): countries = check_list_flag(countries, '\033[1;31m Invalid flag passed. Make sure to use --countries\033[0;0m') if countries: - get_countries() \ No newline at end of file + get_countries() diff --git a/src/covidify/data_visualization.py b/src/covidify/data_visualization.py index 8674540..b7bfc35 100644 --- a/src/covidify/data_visualization.py +++ b/src/covidify/data_visualization.py @@ -174,6 +174,8 @@ def log_plot(tmp, col, fig_title): daily_df.to_excel(workbook_writer, sheet_name='daily figures') workbook = workbook_writer.book +#iteravive design pattern implemented + def get_image_types(path): ''' get all the possible types of images in @@ -183,7 +185,14 @@ def get_image_types(path): for fn in glob.glob(os.path.join(path, '*.png')): types.append(fn.split('_',)[-1].split('.')[0]) - return types + yield types +image_types = get_image_types(image_dir) + +padding = 1 # Set padding for images in spreadsheet +for types in set(image_types): + print('... reading images for:', types) + type_dict = read_images(image_dir, types) + # Get all images for each type def read_images(path, graph_type): @@ -208,4 +217,4 @@ def read_images(path, graph_type): padding = 1 workbook.close() -print('Done!') \ No newline at end of file +print('Done!')