Document a small program that “mungs” an email address

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite
1












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 in argparse, what is the standard way to add some documentation for it, such as a manpage or some embedded help?


Thanks.



#!/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)









share|improve this question









New contributor




dr01 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.























    up vote
    2
    down vote

    favorite
    1












    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 in argparse, what is the standard way to add some documentation for it, such as a manpage or some embedded help?


    Thanks.



    #!/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)









    share|improve this question









    New contributor




    dr01 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      up vote
      2
      down vote

      favorite
      1









      up vote
      2
      down vote

      favorite
      1






      1





      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 in argparse, what is the standard way to add some documentation for it, such as a manpage or some embedded help?


      Thanks.



      #!/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)









      share|improve this question









      New contributor




      dr01 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      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 in argparse, what is the standard way to add some documentation for it, such as a manpage or some embedded help?


      Thanks.



      #!/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






      share|improve this question









      New contributor




      dr01 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      dr01 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 4 hours ago





















      New contributor




      dr01 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 4 hours ago









      dr01

      1113




      1113




      New contributor




      dr01 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      dr01 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      dr01 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote













          The code is straightforward and reads well, but:



          1. string concatenation is not very efficient, especially in a loop. You'd be better off using str.join on an iterable;

          2. encoding the mailto: part yourself impairs readability and maintenance, if only you had a function to do it for you. Oh wait...

          3. 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__;

          4. 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)





          share|improve this answer




















            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "196"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );






            dr01 is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f205103%2fdocument-a-small-program-that-mungs-an-email-address%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote













            The code is straightforward and reads well, but:



            1. string concatenation is not very efficient, especially in a loop. You'd be better off using str.join on an iterable;

            2. encoding the mailto: part yourself impairs readability and maintenance, if only you had a function to do it for you. Oh wait...

            3. 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__;

            4. 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)





            share|improve this answer
























              up vote
              2
              down vote













              The code is straightforward and reads well, but:



              1. string concatenation is not very efficient, especially in a loop. You'd be better off using str.join on an iterable;

              2. encoding the mailto: part yourself impairs readability and maintenance, if only you had a function to do it for you. Oh wait...

              3. 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__;

              4. 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)





              share|improve this answer






















                up vote
                2
                down vote










                up vote
                2
                down vote









                The code is straightforward and reads well, but:



                1. string concatenation is not very efficient, especially in a loop. You'd be better off using str.join on an iterable;

                2. encoding the mailto: part yourself impairs readability and maintenance, if only you had a function to do it for you. Oh wait...

                3. 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__;

                4. 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)





                share|improve this answer












                The code is straightforward and reads well, but:



                1. string concatenation is not very efficient, especially in a loop. You'd be better off using str.join on an iterable;

                2. encoding the mailto: part yourself impairs readability and maintenance, if only you had a function to do it for you. Oh wait...

                3. 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__;

                4. 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)






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 2 hours ago









                Mathias Ettinger

                22.3k32976




                22.3k32976




















                    dr01 is a new contributor. Be nice, and check out our Code of Conduct.









                     

                    draft saved


                    draft discarded


















                    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.













                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f205103%2fdocument-a-small-program-that-mungs-an-email-address%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    What does second last employer means? [closed]

                    Installing NextGIS Connect into QGIS 3?

                    One-line joke