HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Simple email validation script

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
scriptsimplevalidationemail

Problem

I was doing a programming challenge and the object was to validate an email address, I wrote a simple little email validator and would like some input on how I can make it more efficient.

How it works, it has two validations, one with a regex, and one validating the length (most email addresses are over 12 characters, so that's what I went with).
The regex checks for an @ and a . symbol. This is all inside of its own validation class.

Source:

import re

class Validator(object):

    def __init__(self, email):
        self.email = email

    def check_for_symbol(self):
        if re.search("[@.]", self.email) is None:
            return False
        else:
            return True

    def check_length(self):
        if len(self.email) >= 12:
            return True
        else:
            return False

def email_to_verify():
    return raw_input('Enter your email address: ')

def check_email(email):
    validator = Validator(email)
    symbol = validator.check_for_symbol()
    if symbol is False:
        print "Your email address is not valid. Failed symbols.."

    length = validator.check_length()
    if length is False:
        print "Your email is not valid. Failed length"

    if length and symbol is True:
        print "Email is a valid email address."

if __name__ == '__main__':
    e = email_to_verify()
    check_email(e)


Is there a way to make this more efficient?

Examples of usage:

C:\challenges>python email_val.py
Enter your email address: test
Your email is not valid. Failed symbols check.
Your email is not valid. Failed length check.

C:\challenges>python email_val.py
Enter your email address: te@t.t
Your email is not valid. Failed length check.

C:\challenges>python email_val.py
Enter your email address: gmail@gmail.gmail
Email is a valid email address.

Solution

Interface design

The current design of the validator class is not very intuitive.
Users of this class must call the check_for_symbol and check_length methods to validate an email address.
They would only know this by reading the implementation.
You could add documentation,
but it would still not be easy to use.
The best is when the usage and behavior is obvious.

I would imagine the design of an email validator something like this:

class EmailValidator:
    def validate(self, email):
        # ...


Where the validate method would raise exceptions depending on what failed (such as length, symbols).

With such outline, even without documentation,
when a user does help(EmailValidator) and sees a single validate method,
the usage is obvious, clear.

What's worse,
the check_email function is not part of the validator class.
As such, the validator doesn't encapsulate the validation logic.
That might be acceptable if the public API is the check_email and the class is the implementation detail.

Coding style

Use boolean expressions directly. For example, instead of this:

def check_for_symbol(self):
    if re.search("[@.]", self.email) is None:
        return False
    else:
        return True


You could write as:

def check_for_symbol(self):
    return re.search("[@.]", self.email) is not None


Also, instead of x is True, use simply x, and instead of x is False, use not x.

See more on this in the style guide.

Compatibility with Python 3

When using Python 2,
to future-proof yourself it's good to adopt a writing style compatible with Python 3. For example, instead of print 'something', write print('something').

Code Snippets

class EmailValidator:
    def validate(self, email):
        # ...
def check_for_symbol(self):
    if re.search("[@.]", self.email) is None:
        return False
    else:
        return True
def check_for_symbol(self):
    return re.search("[@.]", self.email) is not None

Context

StackExchange Code Review Q#140047, answer score: 9

Revisions (0)

No revisions yet.