1

I just started to use Python, and I know Python requires indentation, unlike any like language. But when I copy and paste a block of source code from other source, the code is mostly messy and I have to do the tab/backspace/home again and again for each line.

My current major setup are the Visual Studio Code with Python extension, and I use Emacs mainly for formatter and when I need to edit on Linux. https://www.emacswiki.org/emacs/ReformatBuffer

In old days, I can either use Visual Studio or Emacs to format the whole file in seconds.

Now with Python, you have to do it manually, which is a waste of time. I tried several IDEs (Spyder, etc) and packages (yapf). The idea beind IDE is that they help you when you type, but not trying to reindent everything.

For example, Spyder has a fix indentation function but it only fix tab/space. https://github.com/spyder-ide/spyder/issues/5565

While yapf only works when there is no indentation errors in the code.

My idea is that, unlike C/C++ and other language, there is a not an "end" in Python statement, so it is impossible for the tool to know whether the current line is the last of the block?

Because I used several languages and sometimes I just copy some from one to another and make some changes/replacements. For example (in IDL language)

    boundary = read_binary(boundary_file, data_type = 4, data_dims = [ncolumn, nrow])  
    ;;==========================================================================================================
    nan_index = WHERE( boundary EQ missing_value, nan_count)
    good_index = WHERE( boundary NE missing_value, good_count)
    IF nan_count EQ 0 THEN BEGIN
       RETURN
    ENDIF ELSE BEGIN
       boundary[ nan_index ] = 0
       boundary[ good_index ] = 1
    ENDELSE 
    :do something else
    sFilename_rock = 'something'

And I want to translate to python after copy/paste, after the replacement:

        def test
            import numpy as np
            sFilename_boundary = 'some_file'
        ifs = open(sFilename_boundary, 'rb')
    aBoundary = np.fromfile(ifs, '<f4')
    aBoundary.shape = (nrow, ncolumn)
    ifs.close()
        #==========================================================================================================
        nan_index = np.where(aBoundary == 1)
        nan_count = len(nan_index)

        if nan_count == 0 :
           pass
        else:
           boundary[ nan_index ] = 0
           boundary[ good_index ] = 1
    #something else
    sFilename_rock = 'something'

In this case, the whole code structure becomes nosense to me and I have to manually correct them one by one using yapf.

Please advise a solution, thanks.

Chang
  • 396
  • 4
  • 17
  • 1
    Have you tried [PyCharm](https://www.jetbrains.com/pycharm/)? That's what I use and it makes formatting a no-brainer. – A Magoon Feb 15 '18 at 17:02
  • Does your editor have settings for automatically converting tabs to spaces? If everything comes in as a space, your code will probably be much less messy. – Scott Mermelstein Feb 15 '18 at 17:05
  • I use [yapf](https://github.com/google/yapf). Here's an online demo https://yapf.now.sh/ You can integrate it into your workflow and run it when you save files in your editor or as a git precommit hook or something like that. – Håken Lid Feb 15 '18 at 17:07
  • Hey Chang, I second @A-Magoon. *PyCharm* is the answer to your question. It's free for us, academics too. See you around! – Atcold Feb 15 '18 at 17:35
  • I just tried. Like I said, IDE only works when there is no indentation errors. In other languages, you can throw any file into the IDE, and IDE can do its job as long as there is no missing syntax. But for Python, it just won't work that way. – Chang Feb 15 '18 at 17:48
  • Chang, *Python* relies on indentation for its syntax. If you screw it, then you're screwing the syntax. You have to copy the source correctly. I have no issue whatsoever when I'm copying and pasting within *PyCharm*. You should include a screen shot and some code examples, if you'd like us to better understand your issue. – Atcold Feb 15 '18 at 17:55
  • So, is the problem pasting IDL source into an editor that does not support it? Am I getting this right? – Atcold Feb 15 '18 at 19:26

1 Answers1

2

Pasting Python into Emacs using the system clipboard generally works fine. This sounds like you are using a paste method which basically simulates typing into Emacs (perhaps you are running Emacs remotely using some primitive VT220-type terminal emulation?) which causes Emacs to add indents where your pasted code already contains them.

You can disable the auto-indentation (maybe paste into the *scratch* buffer and then use Emacs' internal copy/paste into the Python buffer you are working in?) or run Emacs locally (maybe connect to the other system with Tramp if you need to edit files on a remote system).

tripleee
  • 175,061
  • 34
  • 275
  • 318