5
\$\begingroup\$

If you have a Gmail ID, then you can also receive emails on the same account even if you add extra dots in the username or add '+' and some characters.

Say your Gmail ID is [email protected]. You will receive emails on same account even if it is sent to:

Following is the code, which gives me back username removing dots and characters after '+' symbol. Do note that I really don't care if user enters an invalid Gmail ID.

import re

def return_gmail_user(email):
    username = email.split('@')[0]
    domain = email.split('@')[1]
    valid_email_char = '[\w\d\.]+'
    # check if email is of gmail
    if domain != 'gmail.com':
        return False
    # get username
    m = re.match(valid_email_char, username) 
    if m:
        username = m.group(0)
        #print username
        return "".join(username.split('.'))
    return False

def main():
    assert('my' == return_gmail_user('[email protected]'))
    assert('my' == return_gmail_user('[email protected]'))
    assert('avinassh' != return_gmail_user('[email protected]'))
    assert('avinassh' == return_gmail_user('[email protected]'))
    assert('avinassh' == return_gmail_user('[email protected]'))
    assert('avinassh' == return_gmail_user('[email protected]'))
    assert('avinassh' == return_gmail_user('[email protected]'))

if __name__ == '__main__':
    main()
\$\endgroup\$
1
  • \$\begingroup\$ Note that gmail.com is not the only domain used for Google's mail service; googlemail.com works as well. \$\endgroup\$ Apr 25, 2014 at 1:29

1 Answer 1

8
\$\begingroup\$
def return_gmail_user(email):

Return isn't a very descriptive word to use as the beginning of a function name. Call it parse_gmail_user or something along those lines instead.

    username = email.split('@')[0]
    domain = email.split('@')[1]

Read up about sequence unpacking. There's no need to use indices directly, just assign the result of the split operation directly to both variables.

    valid_email_char = '[\w\d\.]+'
    # check if email is of gmail
    if domain != 'gmail.com':

The comment is redundant, it adds nothing to the code — remove it.

        return False

It would be more Pythonic to raise an exception instead of returning False.

    # get username
    m = re.match(valid_email_char, username) 

Give m a descriptive name, such as valid_user, making the code more fluent.

    if m:

You could combine the two if statements to fail early if an incorrect value is given for email.

        username = m.group(0)

group retrieves the first group by default, no need to supply 0.

        #print username

This comment is probably a remnant from testing; commented-out code should be removed immediately as it adds clutter and serves only to confuse.

        return "".join(username.split('.'))

replace('.', '') would be a more descriptive option of achieving the above.

    return False

Following my suggestions, you could arrive the below solution. Note that the semantics are insignificantly different (the match is performed regardless of the validity of the domain), but readability and simplicity is substantially better.

def return_gmail_user(email):
    user, domain = email.split('@')
    valid_user = re.match('[\w\d\.]+', user)
    if domain != 'gmail.com' or not valid_user:
        raise ValueError('invalid gmail address: ' + email)
    return valid_user.group().replace('.', '')
\$\endgroup\$
1
  • \$\begingroup\$ Thanks a lot! So much clean code. Will implement all of your suggestions. \$\endgroup\$
    – avi
    Apr 20, 2014 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.