Python Forum
Very strange error of indentation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Very strange error of indentation
#11
More details, here http://imgbox.com/adMgXtI5
Cheers
Reply
#12
If you have a Python script with tabs in source code, the script is broken.

I guess there are tools available to check for this problem.
But it makes fun, to write an own checker, which shows lines with tabs in your console.

from pathlib import Path
import sys
import click
import colorama


def tab_pos(line):
    return [idx for (idx, char) in enumerate(line) if char == '\t']


def scan(script):
    script = Path(script)
    with script.open() as fd:
        for line_no, line in enumerate(fd, 1):
            tabs = tab_pos(line)
            if tabs:
                yield line_no, line, tabs


def print_matches(results):
    RED = colorama.Back.RED
    RESET = colorama.Style.RESET_ALL
    for line_no, line, tabs in results:
        line = list(line.rstrip('\n'))
        for tab in tabs:
            line[tab] = RED + '<\t>' + RESET
        line = ''.join(line)
        print(f'{line_no:>4} -> {line}')


@click.command()
@click.argument('script')
def main(script):
    """
    This program detects tabs in source code and will print the result to console.
    """
    results = list(scan(script))
    if not results:
        print('File has no tabs')
        return 0
    else:
        print_matches(results)
        return 1

if __name__ == '__main__':
    sys.exit(main())
Output:
1 -> < >Hello World 2 -> This< >is< >a< >tab
You need click and colorama.
A tabulator is replaced with RED + '<\t>' + RESET
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#13
Hi Verb Conjugator!
If you get a glance to the source (uploaded into the opening post) http://reionization.org/wp-content/uploa...st2ra.html you may note that the mismatch is created by in [4] piece of code, undoubtely coming from Mathematica editor.
So, for future applications I'll be careful to take Python codes from such sources, which could conflict with my preferred editor (geany).
Thank you, and cheers
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Strange argument count error rowan_bradley 3 721 Aug-06-2023, 10:58 AM
Last Post: rowan_bradley
  Strange error ValueError: dimension mismatch Anldra12 0 1,971 Aug-17-2021, 07:54 AM
Last Post: Anldra12
  Strange syntax error with f-strings Askic 6 4,204 Oct-16-2020, 10:40 AM
Last Post: Askic
  Indentation Error With If Else Statement JoeDainton123 3 1,829 Aug-02-2020, 08:29 PM
Last Post: deanhystad
  Indentation error... lol ironfelix717 8 3,750 Apr-02-2020, 02:31 PM
Last Post: buran
  Getting error message for indentation Shafla 5 2,834 May-07-2019, 08:56 PM
Last Post: Yoriz
  Help please - indentation error bil007 1 2,045 Mar-24-2019, 11:41 AM
Last Post: Yoriz
  Indentation error in Python code ErnestTBass 5 3,571 Feb-28-2019, 04:26 PM
Last Post: samsonite
  Strange Pandoc Error AreebSooYasir 0 2,036 Jul-24-2018, 04:30 AM
Last Post: AreebSooYasir
  Class - Error Message (Indentation) tkj80 3 4,493 Jan-20-2017, 07:48 AM
Last Post: wavic

Forum Jump:

User Panel Messages

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