def valid(self, data_row): for ind in self.required_column_indices: if data_row[ind] == "": return False if not is_date(data_row[4]) or not is_int(data_row[6]): return False return True
def has_dates(doc, inv_vocab): """ True if the document has dates, else False. """ for idx in doc.indices: if utils.is_date(inv_vocab[idx]): return 1 return 0
def do_where(self, my_df, attr, value, opr): tbl, attr = self.extract_ta(attr) # if tbl is None: # pass # else: # table = self.alias_map[tbl] if isinstance(value, list): return self.do_dynamic_where(my_df, attr, value[0], opr, value[2], value[1]) elif utils.is_float(value) or utils.is_date(value) or utils.is_quoted( value): par = utils.extract_data(value) return self.do_fix_where(my_df, attr, par, opr) else: return self.do_dynamic_where(my_df, attr, value, opr)
def test_is_date(self): app = MagicMock() today = datetime.date.today() tomorrow = today + datetime.timedelta(days=1) tomorrow = tomorrow.strftime('%d/%m/%Y') print("tomorrow: " + tomorrow) test_cases = [("I will travel on 05/09/2020", False), ("on 05/09/2021 and on 03/09/2021", False), ("2021/06/30", False), (tomorrow, datetime.datetime.strptime(tomorrow, "%d/%m/%Y")), ("on " + tomorrow, datetime.datetime.strptime(tomorrow, "%d/%m/%Y"))] for option, answer in test_cases: self.assertEqual(is_date(option, app), answer)
def add_transaction(self, date, amount, payee, type): if not is_date(date): raise InvalidDateError try: if datetime.strptime(date, '%Y-%m-%d %H:%M:%S') < self.start_date: raise AddTransactionError() if isinstance(amount, int) or isinstance(amount, float): self.transactions[date] = [ amount, payee, self.get_balance(date) + amount, type ] else: raise InvalidNumberError except TypeError: raise
def get_balance(self, desired_date): if not is_date(desired_date): raise InvalidDateError dates = list(self.transactions.keys()) dates.sort() prior_date = None for date in dates: if date < desired_date: prior_date = date else: break if prior_date == None: return -1 else: return float(self.transactions[prior_date][2])
def parse_inputs(ini, section='INPUTS'): """Parse INPUTS section of INI file""" # MANDATORY PARAMETERS # section, input_name, output_name, get_type param_list = [ ['et_model', 'et_model', str], # Move to INTERPOLATION section eventually ['start_date', 'start_date', str], ['end_date', 'end_date', str], ['study_area_path', 'study_area_path', str] ] for input_name, output_name, get_type in param_list: get_param(ini, section, input_name, output_name, get_type) ini[section]['et_model'] = ini[section]['et_model'].upper() # Check ET Model et_model_list = ['NDVI', 'EEFLUX'] if not utils.is_option(ini[section]['et_model'], et_model_list): logging.error( '\nERROR: Invalid ET Model type: {}\n Must be {}'.format( ini[section]['et_model'], ', '.join(et_model_list))) sys.exit() # Build and check file paths if not os.path.isfile(ini[section]['study_area_path']): logging.error( '\nERROR: The study area shapefile does not exist, ' 'exiting\n {}'.format(ini[section]['study_area_path'])) sys.exit() ini[section]['study_area_name'] = os.path.splitext( os.path.basename(ini[section]['study_area_path']))[0] # Check dates if not utils.is_date(ini[section]['start_date']): logging.error('\nERROR: Invalid start date') sys.exit() elif not utils.is_date(ini[section]['end_date']): logging.error('\nERROR: Invalid end date') sys.exit() ini[section]['start_dt'] = datetime.datetime.strptime( ini['INPUTS']['start_date'], '%Y-%m-%d') ini[section]['end_dt'] = datetime.datetime.strptime( ini['INPUTS']['end_date'], '%Y-%m-%d') # OPTIONAL PARAMETERS # param_section, input_name, output_name, get_type, default param_list = [ # Control which Landsat images are used ['landsat5_flag', 'landsat5_flag', bool, True], ['landsat7_flag', 'landsat7_flag', bool, True], ['landsat8_flag', 'landsat8_flag', bool, True], # Average cloud cover percentage from metadata ['cloud_cover', 'cloud_cover', int, 100], # Path/row filtering ['wrs2_tiles', 'wrs2_tiles', list, []], ['wrs2_tile_fmt', 'wrs2_tile_fmt', str, 'p{:03d}r{:03d}'], ['wrs2_coll', 'wrs2_coll', str, 'projects/eeflux/wrs2_descending_custom'], ['wrs2_buffer', 'wrs2_buffer', int, 0], ] for input_name, output_name, get_type, default in param_list: get_param(ini, section, input_name, output_name, get_type, default) if ini[section]['cloud_cover'] < 0 or ini[section]['cloud_cover'] > 100: logging.error('\nERROR: cloud_cover must be in the range 0-100\n') sys.exit() # I'm not sure this needs to be checked every time try: ee.FeatureCollection(ini[section]['wrs2_coll']).first().getInfo() except Exception as e: logging.error( '\nUnhandled exception attempting to read the WRS2 collection\n ' 'wrs2_coll: {}\n{}\n'.format(ini[section]['wrs2_coll'], e)) sys.exit() if ini[section]['wrs2_buffer'] < 0: logging.error( '\nERROR: wrs2_buffer must be greater than or equal 0\n') sys.exit() # Convert WRS2 tile ranges to list if ini[section]['wrs2_tiles']: ini[section]['wrs2_tiles'] = sorted([ wrs2_tile.strip() for wrs2_tile in ini[section]['wrs2_tiles'].split(',')]) # Filter user WRS2 tiles based on pre-defined WRS2 tile format wrs2_tile_re = re.compile('p(\d{1,3})r(\d{1,3})') # logging.debug('\n {:16s} {}'.format( # 'Input WRS2 tiles:', ', '.join(ini['INPUTS']['wrs2_tiles']))) ini[section]['wrs2_tiles'] = [ ini['INPUTS']['wrs2_tile_fmt'].format( *map(int, wrs2_tile_re.findall(wrs2_tile)[0])) for wrs2_tile in ini[section]['wrs2_tiles']] logging.debug('\n {:16s} {}'.format( 'WRS2 tiles:', ', '.join(ini[section]['wrs2_tiles']))) # Inputs not yet set in INI file ini[section]['wrs2_tile_field'] = 'WRS2_TILE' # Read WRS2 tiles from file # Eventually make this a parameter? # pr_input_file = os.path.join('tcorr_conus', 'wrs2_tile_conus.txt') # pr_input_file = os.path.join( # 'tcorr_western_us', 'wrs2_tile_western_us.txt') # pr_input_file = os.path.join( # 'tcorr_red_river_basin', 'wrs2_tile_red_river_basin.txt') # pr_input_path = os.path.join(output_ws, pr_input_file) # logging.debug('\nReading WRS2 tile list from file:\n {}'.format( # pr_input_path)) # with open(pr_input_path) as input_f: # user_wrs2_tiles = [x.strip() for x in input_f.readlines()] # logging.debug(' {} WRS2 tiles'.format(len(wrs2_tiles))) logging.info('\n {:16s} {}'.format( 'Start Date:', ini[section]['start_date'])) logging.info(' {:16s} {}'.format( 'End Date:', ini[section]['end_date']))
def organize_data(keys, table, wanted_keys): '''Organize all of the mushers and their stats for that log''' windex = 0 musher_list = [] musher = {} count_spaces = 0 for ele in table: # strip extra symbols, not annoyed at all if ' •' in ele: ele = ele.strip(' •') if ' *' in ele: ele = ele.strip(' *') # if its a date, continue to next if is_date(ele): continue # if its a float, continue to next if is_float(ele): continue # white space of empty values or end of row if ele == '': count_spaces += 1 continue # break if scratched data if ele == 'Scratched' or ele == 'Withdrawn': return [] if count_spaces >= 4: count_spaces = 0 windex = 0 musher = {} if '(r)' in ele: ele = ele.strip(' (r)') musher['rookie_status'] = True if ele.isdigit(): ele = int(ele) musher[wanted_keys[windex]] = ele if 'Dogs' in musher.keys(): if 'rookie_status' not in musher.keys(): musher['rookie_status'] = False musher_list.append(musher) musher = {} windex = 0 continue # if Pos isnt an int, its been written as a checkpoint if 'Pos' in musher: if not type(musher['Pos']) == int: continue if windex == (len(wanted_keys) - 1): windex = 0 else: windex += 1 return musher_list
def test_is_date(self): # Test that the date finding function is correct date = utils.is_date('3/4 4:45:23') not_date = utils.is_date('Musher') self.assertTrue(date) self.assertFalse(not_date)
def get_bot_response(self): """ Process the user input and return a string response from the bot :return: the bot response """ try: user_input = request.args.get("msg") # get data from input, we write js too index.html response = str(self.bot.get_response(user_input)) current_app.logger.debug("You:" + user_input) current_app.logger.debug("Bot:" + response) if self.trip_forecast != "": # If we already collected the forecast if self.conversation.is_affirmative(user_input): response = suggest_clothes(self.trip_forecast) self.trip_forecast = '' else: response = "Ok. Take care and enjoy your trip." self.trip_forecast = '' if self.trip_date != "": # If we already collected the date if is_location(user_input): user_location = user_input self.trip_forecast = get_forecast(self.trip_date, user_location) response = self.trip_forecast.get('text') + " Can I sugest you something to wear? "; self.trip_date = '' else: current_app.logger.warning("Error while getting user location.") response = "You entered an invalid location. Please, try again." if self.check_date: current_app.logger.debug("1") if not is_date(user_input, current_app): current_app.logger.debug("2") response = "You entered an invalid date. The date should be within the next 5 days and in the format dd/mm/yyyy. Please, try again." if self.conversation.is_negative(user_input): current_app.logger.debug("2") response = "Ok. Understood." self.check_date = False if self.conversation.is_question(user_input): current_app.logger.debug("3") response = "I will help you. Please enter the date for your next trip?" else: current_app.logger.debug("4") self.trip_date = is_date(user_input, current_app) self.check_date = False response = "Thank you. Please provide the location that you will go. Format: City, Country" if self.other_trip: if self.conversation.is_affirmative(user_input): response = "Ok! Please provide the date of your next trip? The date must be within the next 5 days. Format: dd/mm/yyyy" self.check_date = True self.other_trip = False else: response = "Ok. Take care and enjoy your trip." self.other_trip = False if "next trip" in response: current_app.logger.info("Bot waiting for the date.") self.check_date = True if "anything else" in response: current_app.logger.info("Bot waiting for the other trip info.") self.other_trip = True if self.conversation.is_goodbye(user_input): return "Bye." current_app.logger.debug("Variables monitoring:") current_app.logger.debug("trip_date:") current_app.logger.debug(self.trip_date) current_app.logger.debug("trip_forecast:") current_app.logger.debug(self.trip_forecast) current_app.logger.debug("check_date:") current_app.logger.debug(self.check_date) return response except(KeyboardInterrupt, EOFError, SystemExit): current_app.logger.info("End of conversation.")
import openpyxl #Add the library we need to process excel files from datetime import date from dateutil import parser as dateParser from utils import to_excel_date from utils import is_date #Create a variable wb to hold an instance of the excel file to process modelTemplateWorkbook = openpyxl.load_workbook('ModelTemplate.xlsx') #Create a variable with all the worksheets from this workbook sheets = modelTemplateWorkbook._sheets for sheet in sheets: print("Sheet: \t" + sheet.title) for i in range(2, 734): if is_date(str(sheet.cell(row=i, column=1).value)): dateFromExcel = dateParser.parse( str(sheet.cell(row=i, column=1).value)) print("ToExcelFormat:\t" + to_excel_date(dateFromExcel)) print("IsoFormat:\t" + dateFromExcel.isoformat()) print("RegFormat:\t" + str(sheet.cell(row=i, column=1).value)) # b738 = sheet["B738"].value # print("B738: \t" + str(b738)) print('\n')