Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fixing indentation issues.
#1
Hi there.

I know this is an editor problem, not Python itself, but it's driving me nuts, it have have cropped up with other people and I'm interested in what they did.

I have built up a pretty big collection of python scripts now and I have a need to edit them in different locations. The code is centralised, but I maybe on a windows machine one day, a MacBook the then, or vnc'd to a unix box. I always prefer a decent editor with some intellisense, so where possible I use VS Code, if that's not available I'll use nano.

However, after making changes and saving scripts they may then not open correctly in other apps, indentation is messed up. It may be one line which I can delete back and put back in place, it may be some invisible character that I have to chase all the way up or down my code, or it may be a stupid indentation that I just cannot fix, usually a double indentation after a def that isn't correct, but the code won't run without it.

To compound the problem if I go back to the previous editor to see what's gone wrong the code is either worse or will give random incorrect runtime errors. Of course to the compiler the errors are valid, but they're not to the eye.

The problem is this makes bug fixing a complete nightmare. Imagine trying to fix a variable that isn't getting set in a 300 line script where the indentation is all incorrect.

I have in the past had to completely re-write scripts identically to the eye to get them working again. This is a ball ache.

I do realise with Python and it's fairly loose structure, no end if, no end sub, no end function, it' damn difficult to fix indentation errors if you're not intimate with the code. I WISH there was an option to force strict ends. But, is there an app out there anywhere that can fix these odd problems? It must be a character out of place, a tab that can't be seen, a space, some other cr/lf issue.

I know the bet practice would be - Only use one editor in one location, but this just isn't possible. And in this day and age it should be possible to hand your code to someone else and receive back changes without this crap.

Any other suggestions?

Many thanks.
Reply
#2
First, I would make sure all of my editors are set to the same indentation settings. Four spaces, tabs, whatever. Just pick one and make sure all of the editors are set the same.

Second, the way to fix this is to get an editor that shows you the invisible characters. Then go through the code looking for discrepancies. Use find as well. If you are using tabs, search for double spaces or return and space. If you are using spaces, search for tabs.

Third, debug this. Think up a bunch of short functions, each using a loop or a conditional. Then write them to the same file, using a different editor for each function. Then, go back to an editor that can show the invisible characters, and look for discrepancies.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
additional: many IDE's include auto formatting commands which work well, VSCode included
on windows: Shift Alt F
on Mac Shift Option F
On Linux: Ctrl Shift I
Reply
#4
Thanks folks, I will follow these steps and go through everything. I suspect it also has something to do with the python interpreters loaded into vs code, but I may be wrong.

Thanks again.
Reply
#5
There's definitely an extension issue.

On the MacBook half of my code is squiggly green with the warning "Spaces and tabs, expected spaces" although it's set to use tabs, not spaces. And on Windows with all the same editor settings the code is accepted as perfect.

I've been through the code with a nit comb VS Code with "Show all whitespace", notepad++ with "Show all characters", can't find a single thing wrong.

What's most confusing is looking at the code on the MacBook, why is half of my code ok and half of it not? There are NO differences in indents, tabs, spaces, coding style, etc. I've been over and over it. There's something around half way through where the extension is resetting a flag or something stupidly small.

Where do you post support requests for VS Code? I'm happy to work with them to get this hammered out.

Thanks again.
Reply
#6
It's recommended to use spaces instead of tabs so any editor could get it right. In any IDE editor, you can set the tabs to insert four spaces. So it is not needed to press the space button repeatedly but the tab.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
i had a similar problem when i first started python. It was a nightmare. I used tabs at first, and someone here got me to use spaces. Using spaces guarantees a level of consistency that tabs do not.

In Geany IDE, there are options for converting the current file to X type of space as shown in the image. In this way i dont have to search out for spaces or tabs, i just convert the file and done

Attached Files

Thumbnail(s)
   
Recommended Tutorials:
Reply
#8
Hah! Didn't know about it.
Anyway, one could write a python script to replace all tabs with four spaces. It's a pretty simple exercise.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#9
Tabs or spaces shouldn't matter. Any text editor that can't handle tabs doesn't deserve to be called a text editor.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#10
VS Code fix indentation with Right click --> Format Document.
I have formater set to use Black(An auto-formatter that really is good).
Look here how to setup.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  (python) Can i get some help fixing a English to Morse translator? Pls AlexPython 7 1,600 Sep-12-2022, 02:55 AM
Last Post: AlexPython
  Invalid syntax error - need help fixing calgk01 3 3,273 Feb-23-2021, 08:41 PM
Last Post: nilamo
  Fixing a code error Twoshawns 5 2,746 May-14-2020, 11:56 AM
Last Post: Twoshawns
  Help Fixing Code kianwalters05 5 3,954 May-12-2020, 12:13 PM
Last Post: kianwalters05
  Fixing "PermissionError: [Errno 13] Permission denied" puredata 17 72,459 Mar-09-2020, 03:20 PM
Last Post: syssy
  Fixing the code khanhcao 2 85,838 Feb-06-2020, 07:24 AM
Last Post: buran
  Fixing a problem with file.io ThickTac 2 2,622 Mar-13-2019, 10:13 PM
Last Post: ThickTac

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020