patternpythonMinor
Giving actual username from Gmail ID
Viewed 0 times
gmailactualusernamefromgiving
Problem
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 montypython@gmail.com. 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.
Say your Gmail ID is montypython@gmail.com. You will receive emails on same account even if it is sent to:
- monty.python@gmail.com
- montypython+python@gmail.com
- m.o.nty.p.y.t.hon@gmail.com
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('my@gmail.com'))
assert('my' == return_gmail_user('m.y@gmail.com'))
assert('avinassh' != return_gmail_user('avinassh@reddit.com'))
assert('avinassh' == return_gmail_user('avinassh+reddit@gmail.com'))
assert('avinassh' == return_gmail_user('av.ina.s.sh@gmail.com'))
assert('avinassh' == return_gmail_user('avi.na.ss.h+reddit@gmail.com'))
assert('avinassh' == return_gmail_user('avi.na.ssh+red+dit@gmail.com'))
if __name__ == '__main__':
main()Solution
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 FalseIt 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 usernameThis 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 FalseFollowing 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('.', '')Code Snippets
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)Context
StackExchange Code Review Q#47451, answer score: 8
Revisions (0)
No revisions yet.