def is_valid(to_check): """ >>> ValidateGender.is_valid('male') ('M', True) >>> ValidateGender.is_valid('female') ('F', True) >>> ValidateGender.is_valid('person') ('Person', False) >>> ValidateGender.is_valid('Gi344#@$@#$rl') ('F', True) >>> ValidateGender.is_valid('') ('INVALID', False) >>> ValidateGender.is_valid(' 789 ') ('INVALID', False) """ result = False gender_list_m = ['M', 'Boy', 'Male', 'Dude', 'Guy'] gender_list_f = ['F', 'Girl', 'Female', 'Lady'] g = to_check g = Wa.wash_all_but_string_characters(g) g = Wa.set_case(g) if g == '': g = "INVALID" elif Va.is_in_list(g, gender_list_m): g = 'M' result = True elif Va.is_in_list(g, gender_list_f): g = 'F' result = True return g, result
def is_valid(data_to_validate): correct_length = False correct_number_of_characters = False correct_characters = False correct_format = False # Wash the data before verifying it washed_data = Wa.keep_only_these_in_string("a-zA-Z0-9", data_to_validate) washed_data = Wa.set_case(washed_data) # \D where a non-number should be, and \d where a number should be if Va.is_correct_pattern("\D\d\d\d", washed_data) is True: correct_format = True if Va.is_within_length(4, 4, washed_data) is True: correct_length = True if Va.has_this_many_numbers(3, washed_data) is True \ and Va.has_this_many_letters(1, washed_data) is True: correct_number_of_characters = True if washed_data.isalnum() is True: correct_characters = True is_valid = correct_length and correct_number_of_characters and\ correct_characters and correct_format return washed_data, is_valid
def is_valid(self, sales): """ >>> i = ValidateSales() >>> i.is_valid("780") ('780', True) >>> i.is_valid(7800) ('7800', False) >>> i.is_valid(' RFGVHJ#$%^&* 67 @#$%^&*(DFGHJ') ('067', True) >>> i.is_valid(' twenty-two ') (' twenty-two ', False) :param sales: :return: """ result = False try: if isinstance(sales, int): sales = Wa.to_string(sales, self.min_length) if Va.is_minimum(sales, self.min_sales): result = Va.is_within_length(self.min_length, self.max_length, str(sales)) elif isinstance(int(Wa.keep_only_nums(sales)), int): if Wa.strip_string(sales): sales = Wa.keep_only_nums(sales) sales = Wa.to_string(sales, self.min_length) if Va.is_minimum(sales, self.min_sales): result = Va.is_within_length(self.min_length, self.max_length, str(sales)) else: result = False return sales, result except ValueError: result = False return sales, result
def is_valid(to_check): """ >>> ValidateBmi.is_valid('Obese') ('Obesity', True) >>> ValidateBmi.is_valid('norm3123123#@$@#$#@4al') ('Normal', True) >>> ValidateBmi.is_valid('person') ('Person', False) >>> ValidateBmi.is_valid('Normal') ('Normal', True) >>> ValidateBmi.is_valid('') ('INVALID', False) >>> ValidateBmi.is_valid(' ') ('INVALID', False) """ result = False list_bmi = ['Obesity', 'Overweight', 'Normal', 'Underweight'] g = to_check g = Wa.wash_all_but_string_characters(g) g = Wa.set_case(g) if g == '': g = "INVALID" if g == 'Obese': g = 'Obesity' if Va.is_in_list(g, list_bmi): result = True return g, result
def is_valid(self, age): """ >>> i = ValidateAge() >>> i.is_valid("78") ('78', True) >>> i.is_valid(780) ('780', False) >>> i.is_valid(' RFGVHJ#$%^&* 67 @#$%^&*(DFGHJ') ('67', True) >>> i.is_valid(' twenty ') (' twenty ', False) :param age: :return: """ result = False try: if isinstance(age, int): age = Wa.to_string(age, self.min_length) if Va.is_minimum(age, self.min_age): result = Va.is_within_length(self.min_length, self.max_length, str(age)) elif isinstance(int(Wa.keep_only_nums(age)), int): if Wa.strip_string(age): age = Wa.keep_only_nums(age) age = Wa.to_string(age, self.min_length) if Va.is_minimum(age, self.min_age): result = Va.is_within_length(self.min_length, self.max_length, str(age)) return age, result except ValueError: result = False return age, result
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 is_valid(self, salary): """ >>> i = ValidateSalary() >>> i.is_valid("gfdjhs804") ('804', True) >>> i.is_valid(1000) ('1000', False) >>> i.is_valid(' RFGVHJ#$%^&* 6 @#$%^&*(DFGHJ') ('06', True) >>> i.is_valid(' twenty-two ') (' twenty-two ', False) :param salary: :return: """ result = False try: if isinstance(salary, int): salary = Wa.to_string(salary, self.min_length) if Va.is_minimum(salary, self.min_salary): result = Va.is_within_length(self.min_length, self.max_length, str(salary)) elif isinstance(int(Wa.keep_only_nums(salary)), int): if Wa.strip_string(salary): salary = Wa.keep_only_nums(salary) salary = Wa.to_string(salary, self.min_length) if Va.is_minimum(salary, self.min_salary): result = Va.is_within_length(self.min_length, self.max_length, str(salary)) else: result = False try: salary = int(salary) except ValueError: result = False return salary, result except ValueError: result = False return salary, result
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 check_valid(num, min_num, min_length, max_length): if isinstance(num, int): num = Wa.to_string(num, min_length) if Validator.is_minimum(num, min_num): result = Validator.is_within_length(min_length, max_length, str(num)) else: isinstance(int(Wa.keep_only_nums(num)), int) Wa.strip_string(num) num = Wa.keep_only_nums(num) num = Wa.to_string(num, min_length) result = Validator.is_within_length(min_length, max_length, str(num)) return num, 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