Code reformater?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Vincent Delporte

    Code reformater?

    Hello

    When I copy/paste Python code from the web, every so often,
    the TABs are wrong, which means that the code won't work and I have to
    manually reformat the code.

    Is there a code reformater that can parse the code to make it right?

    Thanks.
  • Ben Finney

    #2
    Re: Code reformater?

    Vincent Delporte <justask@acme.c omwrites:
    When I copy/paste Python code from the web, every so often, the TABs
    are wrong, which means that the code won't work and I have to
    manually reformat the code.
    >
    Is there a code reformater that can parse the code to make it right?
    Indentation is necessary for the syntax in Python. If that information
    is lost, it can't be reliably recreated.

    --
    \ "People come up to me and say, 'Emo, do people really come up |
    `\ to you?'" -- Emo Philips |
    _o__) |
    Ben Finney

    Comment

    • Mario Wehbrink

      #3
      Re: Code reformater?

      Vincent Delporte schrieb in comp.lang.pytho n:

      Hi
      When I copy/paste Python code from the web, every so often,
      the TABs are wrong, which means that the code won't work and I have to
      manually reformat the code.
      >
      Is there a code reformater that can parse the code to make it right?
      It may help to look at the settings of your editor. I think Vim (and
      surely emacs) can help you with that.

      (look at :help retab, paste, expandtab in (g)Vim)

      Mario


      --
      Mario Wehbrink

      Comment

      • Siggi

        #4
        Re: Code reformater?


        "Vincent Delporte" wrote:
        Hello
        >
        When I copy/paste Python code from the web, every so often,
        the TABs are wrong, which means that the code won't work and I have to
        manually reformat the code.
        >
        Is there a code reformater that can parse the code to make it right?
        >
        Thanks.
        Maybe my thread "help: code formatter, 08/01/2007 helps a little? Here are
        some of the answers:

        *
        Why don't you just write one? :)
        Seriously: Try.
        *
        Tools\scripts\r eindent.py in your Python distribution.
        *
        Why, yes, there is:

        *
        tabnanny ?


        siggi




        Comment

        • Gabriel Genellina

          #5
          Re: Code reformater?

          At Saturday 20/1/2007 14:37, Siggi wrote:
          When I copy/paste Python code from the web, every so often,
          the TABs are wrong, which means that the code won't work and I have to
          manually reformat the code.

          Is there a code reformater that can parse the code to make it right?

          Thanks.
          >
          >Maybe my thread "help: code formatter, 08/01/2007 helps a little? Here are
          >some of the answers:
          >
          >*
          >Why don't you just write one? :)
          >Seriously: Try.
          >*
          >Tools\scripts\ reindent.py in your Python distribution.
          >*
          >Why, yes, there is:

          >*
          >tabnanny ?
          As the indentation *is* significant in python, none of the above can
          help if you lose the indentation. Try to reconstruct this:

          def foo():
          if a>0:
          if b>0:
          print 1
          print 2
          else:
          return 3
          return 4

          The tools may help to make the indentation consistent (tabs/8
          spaces/4 spaces/2 spaces mixed) or look better, but not to make it right.


          --
          Gabriel Genellina
          Softlab SRL






          _______________ _______________ _______________ _____
          Preguntá. Respondé. Descubrí.
          Todo lo que querías saber, y lo que ni imaginabas,
          está en Yahoo! Respuestas (Beta).
          ¡Probalo ya!


          Comment

          • Steven D'Aprano

            #6
            Re: Code reformater?

            On Sat, 20 Jan 2007 23:51:24 -0300, Gabriel Genellina wrote:
            As the indentation *is* significant in python, none of the above can
            help if you lose the indentation. Try to reconstruct this:
            >
            def foo():
            if a>0:
            if b>0:
            print 1
            print 2
            else:
            return 3
            return 4
            >
            The tools may help to make the indentation consistent (tabs/8
            spaces/4 spaces/2 spaces mixed) or look better, but not to make it right.

            Sure -- but a heuristic that gets it right *sometimes* may still be
            useful, provided the user knows that the code may not be indented
            correctly.

            There are lots of legal Python blocks where the indentation CAN be
            reconstructed correctly, and only a relatively small proportion where it
            can't -- the tool could do its best, and warn the user when there are
            indents that can't be dealt with. Or even refuse the temptation to guess,
            but re-indent whatever parts of the code it is sure about.

            Still, it is better not to lose the indentation in the first place.



            --
            Steven.

            Comment

            • Gabriel Genellina

              #7
              Re: Code reformater?

              At Sunday 21/1/2007 00:15, Steven D'Aprano wrote:
              >On Sat, 20 Jan 2007 23:51:24 -0300, Gabriel Genellina wrote:
              >
              As the indentation *is* significant in python, none of the above can
              help if you lose the indentation. Try to reconstruct this:

              def foo():
              if a>0:
              if b>0:
              print 1
              print 2
              else:
              return 3
              return 4

              The tools may help to make the indentation consistent (tabs/8
              spaces/4 spaces/2 spaces mixed) or look better, but not to make it right.
              >
              >
              >Sure -- but a heuristic that gets it right *sometimes* may still be
              >useful, provided the user knows that the code may not be indented
              >correctly.
              >
              >There are lots of legal Python blocks where the indentation CAN be
              >reconstructe d correctly, and only a relatively small proportion where it
              >can't --
              I'd say absolutely the opposite. Try the example above. Or this one, simpler:

              def foo():
              if a>0:
              if b>0:
              print 1
              print 2

              Which level should be 'print 2' assigned to? There are 3 choices.
              What if there is a third print? There are 3, 2, or 1 possibilities,
              depending on where you put the previous one. The same for any other
              statement below.
              The problem is, after a colon, you know that a new block begins and
              you must indent, but you don't know when that block ends except in a
              few cases (an else clause, by example, and only if there was a single
              preceding if).
              >the tool could do its best, and warn the user when there are
              >indents that can't be dealt with. Or even refuse the temptation to guess,
              >but re-indent whatever parts of the code it is sure about.
              ....almost nothing, I'm afraid... :(
              >Still, it is better not to lose the indentation in the first place.
              Sure!


              --
              Gabriel Genellina
              Softlab SRL






              _______________ _______________ _______________ _____
              Preguntá. Respondé. Descubrí.
              Todo lo que querías saber, y lo que ni imaginabas,
              está en Yahoo! Respuestas (Beta).
              ¡Probalo ya!


              Comment

              • Vincent Delporte

                #8
                Re: Code reformater?

                On Sun, 21 Jan 2007 14:15:46 +1100, Steven D'Aprano
                <steve@REMOVE.T HIS.cybersource .com.auwrote:
                >Still, it is better not to lose the indentation in the first place.
                Thanks for the tips. But it does happen when copy/pasting code from
                either a web page or an e-mail that TABs are messed up, which is not a
                problem with other languages, but is a problem with Python. Too bad.

                Comment

                • Marc 'BlackJack' Rintsch

                  #9
                  Re: Code reformater?

                  In <urv7r2d98j2mpf cdqgmnber1ntes3 9mnks@4ax.com>, Vincent Delporte wrote:
                  On Sun, 21 Jan 2007 14:15:46 +1100, Steven D'Aprano
                  <steve@REMOVE.T HIS.cybersource .com.auwrote:
                  >>Still, it is better not to lose the indentation in the first place.
                  >
                  Thanks for the tips. But it does happen when copy/pasting code from
                  either a web page or an e-mail that TABs are messed up, which is not a
                  problem with other languages, but is a problem with Python. Too bad.
                  Well then don't use code from people using TABs. If it's indented by four
                  spaces per level, like suggested by the style guide, there's no problem
                  with TABs.

                  Ciao,
                  Marc 'BlackJack' Rintsch

                  Comment

                  Working...