def wash_data(self, data_to_wash): # replace any non-word character with a forward slash data_to_wash = Wa.replace_x_with_y("\W+", "/", data_to_wash) # remove st, nd and rd from date, e.g. 21st, 22nd, 23rd data_to_wash = Wa.replace_x_with_y("st|nd|rd", "", data_to_wash) data_to_wash = self.month_string_to_number(data_to_wash) return data_to_wash
def determine_month_format(self, data): # default to number format like 09 for September result = "%m" date_to_check = Wa.replace_x_with_y("\W+", "/", data) split_date = self.split_string("/", date_to_check) for value in split_date: if value.isalpha(): if len(value) > 3: result = "%B" else: result = "%b" return result
def is_valid(self, data_to_validate): result = False data_to_validate = data_to_validate.lstrip(' ') # If there's no numbers in string, just return as is, it's bad data if Va.has_this_many_numbers(0, data_to_validate): date_output = data_to_validate else: washed_data = self.wash_data(data_to_validate) # add zeros if needed washed_data = self.add_zeros(washed_data) # remove all spaces washed_data = washed_data.strip() date_format = self.determine_date_format(washed_data) date_to_check = Wa.replace_x_with_y('\W+', " ", washed_data) result = self.is_real_date(date_to_check, date_format) date_output = Wa.replace_x_with_y(" ", "/", date_to_check) return date_output, result
def month_string_to_number(self, data): date_to_check = Wa.replace_x_with_y("\W+", "/", data) split_date = self.split_string("/", date_to_check) for value in split_date: if value.isalpha(): if len(value) > 3: # result = "%B" text_month = datetime.strptime(value, '%B') split_date[1] = text_month.strftime('%m') else: # result = "%b" text_month = datetime.strptime(value, '%b') split_date[1] = text_month.strftime('%m') output = "" try: output = split_date[0] + "/" + split_date[1] + "/" + split_date[2] except IndexError: pass return output