Python program that obfuscates an email address
Clash Royale CLAN TAG#URR8PPP
up vote
5
down vote
favorite
I decided to resume my (limited) Python skills after not coding for a long time.
Here's a very simple self-contained program to convert the characters of an email address into their HTML entities (the purpose is to make the email invisible to spamrobots).
Any comment for improvement? (e.g. args parsing, code style, etc.)
Apart from the
-h
option inargparse
, what is the standard way to add some documentation for it, such as a manpage or some embedded help?
#!/usr/bin/python
#
# mung - Convert a string into its HTML entities
#
import argparse
parser = argparse.ArgumentParser(description = 'Convert a string into its HTML entities.')
parser.add_argument('string_to_mung', help = 'String to convert')
parser.add_argument('-l', '--link', action = 'store_true', help = 'Embed the munged string into a mailto: link')
args = parser.parse_args()
def mung(plain):
munged = ''
for c in plain:
munged = munged + '&#' + str(ord(c)) + ';'
return munged
string_munged = mung(args.string_to_mung)
if (args.link):
print('<a href="mailto:')
print(string_munged + '">')
print(string_munged + '</a>')
else:
print(string_munged)
python html email escaping
New contributor
add a comment |Â
up vote
5
down vote
favorite
I decided to resume my (limited) Python skills after not coding for a long time.
Here's a very simple self-contained program to convert the characters of an email address into their HTML entities (the purpose is to make the email invisible to spamrobots).
Any comment for improvement? (e.g. args parsing, code style, etc.)
Apart from the
-h
option inargparse
, what is the standard way to add some documentation for it, such as a manpage or some embedded help?
#!/usr/bin/python
#
# mung - Convert a string into its HTML entities
#
import argparse
parser = argparse.ArgumentParser(description = 'Convert a string into its HTML entities.')
parser.add_argument('string_to_mung', help = 'String to convert')
parser.add_argument('-l', '--link', action = 'store_true', help = 'Embed the munged string into a mailto: link')
args = parser.parse_args()
def mung(plain):
munged = ''
for c in plain:
munged = munged + '&#' + str(ord(c)) + ';'
return munged
string_munged = mung(args.string_to_mung)
if (args.link):
print('<a href="mailto:')
print(string_munged + '">')
print(string_munged + '</a>')
else:
print(string_munged)
python html email escaping
New contributor
add a comment |Â
up vote
5
down vote
favorite
up vote
5
down vote
favorite
I decided to resume my (limited) Python skills after not coding for a long time.
Here's a very simple self-contained program to convert the characters of an email address into their HTML entities (the purpose is to make the email invisible to spamrobots).
Any comment for improvement? (e.g. args parsing, code style, etc.)
Apart from the
-h
option inargparse
, what is the standard way to add some documentation for it, such as a manpage or some embedded help?
#!/usr/bin/python
#
# mung - Convert a string into its HTML entities
#
import argparse
parser = argparse.ArgumentParser(description = 'Convert a string into its HTML entities.')
parser.add_argument('string_to_mung', help = 'String to convert')
parser.add_argument('-l', '--link', action = 'store_true', help = 'Embed the munged string into a mailto: link')
args = parser.parse_args()
def mung(plain):
munged = ''
for c in plain:
munged = munged + '&#' + str(ord(c)) + ';'
return munged
string_munged = mung(args.string_to_mung)
if (args.link):
print('<a href="mailto:')
print(string_munged + '">')
print(string_munged + '</a>')
else:
print(string_munged)
python html email escaping
New contributor
I decided to resume my (limited) Python skills after not coding for a long time.
Here's a very simple self-contained program to convert the characters of an email address into their HTML entities (the purpose is to make the email invisible to spamrobots).
Any comment for improvement? (e.g. args parsing, code style, etc.)
Apart from the
-h
option inargparse
, what is the standard way to add some documentation for it, such as a manpage or some embedded help?
#!/usr/bin/python
#
# mung - Convert a string into its HTML entities
#
import argparse
parser = argparse.ArgumentParser(description = 'Convert a string into its HTML entities.')
parser.add_argument('string_to_mung', help = 'String to convert')
parser.add_argument('-l', '--link', action = 'store_true', help = 'Embed the munged string into a mailto: link')
args = parser.parse_args()
def mung(plain):
munged = ''
for c in plain:
munged = munged + '&#' + str(ord(c)) + ';'
return munged
string_munged = mung(args.string_to_mung)
if (args.link):
print('<a href="mailto:')
print(string_munged + '">')
print(string_munged + '</a>')
else:
print(string_munged)
python html email escaping
python html email escaping
New contributor
New contributor
edited 15 mins ago
200_success
125k14145406
125k14145406
New contributor
asked 5 hours ago
dr01
1263
1263
New contributor
New contributor
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
5
down vote
The code is straightforward and reads well, but:
- string concatenation is not very efficient, especially in a loop. You'd be better off using
str.join
on an iterable; - encoding the
mailto:
part yourself impairs readability and maintenance, if only you had a function to do it for you. Oh wait... - The comment at the beginning would be better as a module docstrings, you would then be able to use it as the
argparse
description using__doc__
; - You should avoid interleaving code and function definition, and protect top level code using an
if __name__ == '__main__':
guard.
Proposed improvements:
#!/usr/bin/python
"""Convert a string into its HTML entities"""
import argparse
def command_line_parser():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('string_to_mung', help='String to convert')
parser.add_argument('-l', '--link', action='store_true',
help='Embed the munged string into a mailto: link')
return parser
def mung(plain):
return ''.join('&#;'.format(ord(c)) for c in plain)
if __name__ == '__main__':
args = command_line_parser().parse_args()
string_munged = mung(args.string_to_mung)
if (args.link):
string_munged = '<a href="01">1</a>'.format(mung('mailto:'), string_munged)
print(string_munged)
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
The code is straightforward and reads well, but:
- string concatenation is not very efficient, especially in a loop. You'd be better off using
str.join
on an iterable; - encoding the
mailto:
part yourself impairs readability and maintenance, if only you had a function to do it for you. Oh wait... - The comment at the beginning would be better as a module docstrings, you would then be able to use it as the
argparse
description using__doc__
; - You should avoid interleaving code and function definition, and protect top level code using an
if __name__ == '__main__':
guard.
Proposed improvements:
#!/usr/bin/python
"""Convert a string into its HTML entities"""
import argparse
def command_line_parser():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('string_to_mung', help='String to convert')
parser.add_argument('-l', '--link', action='store_true',
help='Embed the munged string into a mailto: link')
return parser
def mung(plain):
return ''.join('&#;'.format(ord(c)) for c in plain)
if __name__ == '__main__':
args = command_line_parser().parse_args()
string_munged = mung(args.string_to_mung)
if (args.link):
string_munged = '<a href="01">1</a>'.format(mung('mailto:'), string_munged)
print(string_munged)
add a comment |Â
up vote
5
down vote
The code is straightforward and reads well, but:
- string concatenation is not very efficient, especially in a loop. You'd be better off using
str.join
on an iterable; - encoding the
mailto:
part yourself impairs readability and maintenance, if only you had a function to do it for you. Oh wait... - The comment at the beginning would be better as a module docstrings, you would then be able to use it as the
argparse
description using__doc__
; - You should avoid interleaving code and function definition, and protect top level code using an
if __name__ == '__main__':
guard.
Proposed improvements:
#!/usr/bin/python
"""Convert a string into its HTML entities"""
import argparse
def command_line_parser():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('string_to_mung', help='String to convert')
parser.add_argument('-l', '--link', action='store_true',
help='Embed the munged string into a mailto: link')
return parser
def mung(plain):
return ''.join('&#;'.format(ord(c)) for c in plain)
if __name__ == '__main__':
args = command_line_parser().parse_args()
string_munged = mung(args.string_to_mung)
if (args.link):
string_munged = '<a href="01">1</a>'.format(mung('mailto:'), string_munged)
print(string_munged)
add a comment |Â
up vote
5
down vote
up vote
5
down vote
The code is straightforward and reads well, but:
- string concatenation is not very efficient, especially in a loop. You'd be better off using
str.join
on an iterable; - encoding the
mailto:
part yourself impairs readability and maintenance, if only you had a function to do it for you. Oh wait... - The comment at the beginning would be better as a module docstrings, you would then be able to use it as the
argparse
description using__doc__
; - You should avoid interleaving code and function definition, and protect top level code using an
if __name__ == '__main__':
guard.
Proposed improvements:
#!/usr/bin/python
"""Convert a string into its HTML entities"""
import argparse
def command_line_parser():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('string_to_mung', help='String to convert')
parser.add_argument('-l', '--link', action='store_true',
help='Embed the munged string into a mailto: link')
return parser
def mung(plain):
return ''.join('&#;'.format(ord(c)) for c in plain)
if __name__ == '__main__':
args = command_line_parser().parse_args()
string_munged = mung(args.string_to_mung)
if (args.link):
string_munged = '<a href="01">1</a>'.format(mung('mailto:'), string_munged)
print(string_munged)
The code is straightforward and reads well, but:
- string concatenation is not very efficient, especially in a loop. You'd be better off using
str.join
on an iterable; - encoding the
mailto:
part yourself impairs readability and maintenance, if only you had a function to do it for you. Oh wait... - The comment at the beginning would be better as a module docstrings, you would then be able to use it as the
argparse
description using__doc__
; - You should avoid interleaving code and function definition, and protect top level code using an
if __name__ == '__main__':
guard.
Proposed improvements:
#!/usr/bin/python
"""Convert a string into its HTML entities"""
import argparse
def command_line_parser():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('string_to_mung', help='String to convert')
parser.add_argument('-l', '--link', action='store_true',
help='Embed the munged string into a mailto: link')
return parser
def mung(plain):
return ''.join('&#;'.format(ord(c)) for c in plain)
if __name__ == '__main__':
args = command_line_parser().parse_args()
string_munged = mung(args.string_to_mung)
if (args.link):
string_munged = '<a href="01">1</a>'.format(mung('mailto:'), string_munged)
print(string_munged)
answered 3 hours ago
Mathias Ettinger
22.3k32976
22.3k32976
add a comment |Â
add a comment |Â
dr01 is a new contributor. Be nice, and check out our Code of Conduct.
dr01 is a new contributor. Be nice, and check out our Code of Conduct.
dr01 is a new contributor. Be nice, and check out our Code of Conduct.
dr01 is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f205103%2fpython-program-that-obfuscates-an-email-address%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password