Mask email for public viewing as user identifier.
I'm sharing small utility to convert an email to publically viewable user id. The use case is a system where user's don't have username but we still need to identify the person via some automatically generated user id.
e.g. [email protected]
would become saur…@example.com
This type email masking can also be found on google-groups user interface, where if you click on a masked email, it will do a captcha verification before displaying the full email.
import math
def mask_email(email):
"""
Converts an email to publically viewable as user identifier after hiding parts
email.
Usages:
>>> mask_email("[email protected]")
'a…@a.com'
>>> mask_email("[email protected]")
'a…@a.com'
>>> mask_email("[email protected]")
'ab…@a.com'
>>> mask_email("[email protected]")
'ab…@a.com'
>>> mask_email("[email protected]")
'this-is-a-…@gmail.com'
>>> mask_email("invalidemail")
'invalidemail'
"""
if '@' not in email:
return email
first, domain = email.split("@", 1)
hide_after = min(math.ceil(len(first) / 2), 10)
return '{}…@{}'.format(first[:hide_after], domain)
Written by Saurabh Kumar
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Python
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#