Mar-22-2019, 09:32 AM
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.
A tabulator is replaced with RED + '<\t>' + RESET
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!
All humans together. We don't need politicians!