def test_domain_typo_invalid_set(): sugg_correct = 0 sugg_total = 0 print '' for line in DOMAIN_TYPO_INVALID_TESTS.split('\n'): # strip line, skip over empty lines line = line.strip() if line == '': continue # skip over comments or empty lines match = COMMENT.match(line) if match: continue test_str = 'username@' + line sugg_str = validate.suggest_alternate(test_str) if sugg_str == None: sugg_correct += 1 else: print 'incorrect correction: {0}, {1}'.format(test_str, sugg_str) sugg_total += 1 # ensure that we have greater than 90% accuracy accuracy = float(sugg_correct) / sugg_total print 'external invalid: accuracy: {0}, correct: {1}, total: {2}'.\ format(accuracy, sugg_correct, sugg_total) ok_(accuracy > 0.90)
def test_domain_typo_valid_set(): sugg_correct = 0 sugg_total = 0 print '' for line in DOMAIN_TYPO_VALID_TESTS.split('\n'): # strip line, skip over empty lines line = line.strip() if line == '': continue # skip over comments or empty lines match = COMMENT.match(line) if match: continue parts = line.split(',') test_str = 'username@' + parts[0] corr_str = 'username@' + parts[1] sugg_str = validate.suggest_alternate(test_str) if sugg_str == corr_str: sugg_correct += 1 else: print 'did not match: {}, {}'.format(test_str, sugg_str) sugg_total += 1 # ensure that we have greater than 90% accuracy accuracy = float(sugg_correct) / sugg_total print 'external valid: accuracy: {}, correct: {}, total: {}'.\ format(accuracy, sugg_correct, sugg_total) assert_greater(accuracy, 0.90)
def test_domain_typo_valid_set(): sugg_correct = 0 sugg_total = 0 print('') for line in DOMAIN_TYPO_VALID_TESTS.split('\n'): # strip line, skip over empty lines line = line.strip() if line == '': continue # skip over comments or empty lines match = COMMENT.match(line) if match: continue parts = line.split(',') test_str = 'username@' + parts[0] corr_str = 'username@' + parts[1] sugg_str = validate.suggest_alternate(test_str) if sugg_str == corr_str: sugg_correct += 1 else: print('did not match: {0}, {1}'.format(test_str, sugg_str)) sugg_total += 1 # ensure that we have greater than 90% accuracy accuracy = float(sugg_correct) / sugg_total print('external valid: accuracy: {0}, correct: {1}, total: {2}'.format( accuracy, sugg_correct, sugg_total)) ok_(accuracy > 0.90)
def __call__(self, value): validate_email(value) value = force_text(value) if value and address.validate_address(value) is None: suggestion = validate.suggest_alternate(value) if suggestion: raise ValidationError(self.message_typo, code=self.code_typo, params={'suggestion': suggestion}) raise ValidationError(self.message_invalid, code=self.code_invalid)
def __call__(self, value): super(FlankerValidator, self).__call__(value) if address.validate_address(value) is None: suggestion = validate.suggest_alternate(value) if suggestion: message = string_concat(self.message, ' ', self.suggest_fragment) raise ValidationError(message, code=self.code, params={ 'suggestion': suggestion }) raise ValidationError(self.message, code=self.code)
def get_valid_email(email): """Return (valid email address or None, True if email is a suggestion). It uses flanker to correct commonly misspelled domains (e.g. gmil.com) and to check to make sure MX records exist for the domain. """ if not email: return None, None # returns None if it has no alternate or if the email is invalid good_email = validate.suggest_alternate(email) or email suggestion = False if email != good_email: log.info('Using suggested alternate email') suggestion = True good_email = address.validate_address(good_email) if isinstance(good_email, address.EmailAddress): good_email = good_email.address # returns None if the email is invalid, or the email if all's well return good_email, suggestion
def test_suggest_alternate_shorter_valid(): sugg_correct = 0 sugg_total = 0 print '' for i in range(1, 3): for j in range(100): domain = random.choice(corrector.MOST_COMMON_DOMAINS) orig_str = 'username@' + domain sstr = 'username@' + generate_shorter_string(domain, i) sugg_str = validate.suggest_alternate(sstr) if sugg_str == orig_str: sugg_correct += 1 sugg_total += 1 # ensure that we have greater than 60% accuracy accuracy = float(sugg_correct) / sugg_total print 'shorter valid: accuracy: {0}, correct: {1}, total: {2}'.\ format(accuracy, sugg_correct, sugg_total) ok_(accuracy > 0.60)
def test_suggest_alternate_mutations_valid(): sugg_correct = 0 sugg_total = 0 print '' for i in range(1, 3): for j in range(100): domain = random.choice(corrector.MOST_COMMON_DOMAINS) orig_str = 'username@' + domain mstr = 'username@' + generate_mutated_string(domain, i) sugg_str = validate.suggest_alternate(mstr) if sugg_str == orig_str: sugg_correct += 1 sugg_total += 1 # ensure that we have greater than 60% accuracy accuracy = float(sugg_correct) / sugg_total print 'mutations valid: accuracy: {}, correct: {}, total: {}'.\ format(accuracy, sugg_correct, sugg_total) assert_greater(accuracy, 0.60)
def test_suggest_alternate_invalid(): sugg_correct = 0 sugg_total = 0 print '' for i in range(3, 10): for j in range(100): domain = domain_generator(i) orig_str = 'username@' + domain sugg_str = validate.suggest_alternate(orig_str) if sugg_str == None: sugg_correct += 1 else: print 'did not match: {0}, {1}'.format(orig_str, sugg_str) sugg_total += 1 # ensure that we have greater than 60% accuracy accuracy = float(sugg_correct) / sugg_total print 'alternative invalid: accuracy: {0}, correct: {1}, total: {2}'.\ format(accuracy, sugg_correct, sugg_total) ok_(accuracy > 0.60)
def main_view(): if request.method == 'POST': email = request.form['input'] list_email = list(set(email.split("\r\n"))) #remove duplicate emails and split each email from the new line #sort out the emails with commas, colons and spaces and the ones without list_without_problems, list_with_problems = [], [] for email in list_email: if ' ' in email or ';' in email or ',' in email: list_with_problems.append(email) else: list_without_problems.append(email) unique_emails = ','.join(list_without_problems) #transform list to string #validate the good list of emails valid = address.validate_list(unique_emails, as_tuple=True) valids = valid[0] #first list in tuple is valid emails invalids = valid[1] #second is invalid emails #individually check the list with commas, colons, and spaces if len(list_with_problems) != 0: for email in list_with_problems: if address.validate_address(email): valids.append(valids) else: invalids.append(email) #add suggestions if there are invalid emails suggests = [] if len(invalids) != 0: invalids = [unique_emails.encode("ascii") for unique_emails in invalids] #invalid emails come out as unicode - change to regular str and replace @@@ back to space suggests = [str(validate.suggest_alternate(unique_emails)).replace(" ", "") for unique_emails in invalids] #suggest alternate returns None - cast None to str return render_template('home.html', invalids=invalids, valids=valids, suggests=suggests, valid_length=len(valids), invalid_length=len(invalids)) else: return render_template('home.html')
def test_form_invalid_suggest(self): bad_email = '*****@*****.**' suggestion = validate.suggest_alternate(bad_email) form = EmailForm({'email': bad_email}) assert form.is_valid() is False assert suggestion in form.errors['email'].as_text()
multi_address = address.parse_list( '[email protected], [email protected]') print(multi_address) multi_address2 = address.parse_list( '[email protected], [email protected]', as_tuple=True) print(multi_address2) multi_address3 = address.parse_list( '[email protected], [email protected]', strict=True) print(multi_address3) from flanker.addresslib import validate sa = validate.suggest_alternate('*****@*****.**') print(sa) msg = '''MIME-Version: 1.0 Content-Type: text/plain From: Example1 <*****@*****.**> To: Example2 <*****@*****.**> Subject: hello, message Date: Mon, 10 Sep 2019 12:43:03 -0700 this is a single part message.''' from flanker import mime fs = mime.from_string(msg) print(fs.body)