Python Forum
does anyon want to write an untabify command?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
does anyon want to write an untabify command?
#1
i know Linux has a command called "expand" intended to remove tabs. but it doesn't work right. when you run it with a file name, it just outputs the file, without tabs, to stdout, instead of replacing the file (and leaving a backup). and i have no idea what is on Windows or Mac OSX. a pip search comes up empty. does someone want to do this or does everyone want me to do it?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
How about sed?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
why sed?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
Because it's made just for this.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
sed is made for editing. i see nothing in its man page about tabs.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
(Sep-06-2019, 08:21 PM)Skaperen Wrote: sed is made for editing. i see nothing in its man page about tabs.

https://linuxconfig.org/replace-all-tab-...ith-spaces
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
a tab does not mean a fixed amount of spacing from where it is, instead, it means whatever amount of spacing to get to the fixed position of the next tab stop. i know of no way that "sed" can do this. the "expand" command does this correctly, but cannot update a file in place.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
(Sep-06-2019, 10:35 PM)Skaperen Wrote: a tab does not mean a fixed amount of spacing from where it is, instead, it means whatever amount of spacing to get to the fixed position of the next tab stop.
Yes it's more diabolical,and a little more difficult to write.
Why do you not try yourself?

As i started look into this,i did write some code that can do both tab to space or even space.
For in place change of file in-place,and i did use Click to make it a command line tool(no decorator question now) Wink
If you want to use some code can take functions out they work stand alone.
# tab.py
import click
import re
import in_place

@click.group()
def cli():
    """
    \b
    tab-space: Tab to space replace like Linux <expand> command
    Usage: tab-space --tabs 4 file_1.txt
    ---
    tab-even:  Tab to equal spacing between text content
    Usage: tab-even --space 4 file_1.txt
    """
    pass

def tab__space(text, tabs=8):
    result = ''
    for c in text:
        if c == '\t':
            result += ' '
            while len(result) % tabs != 0:
                result += ' '
        else:
            result += c
    return result

@cli.command()
@click.option('-t', '--tabs', default=8, help='Number of spaces')
@click.argument('file', type=click.Path(exists=True))
def tab_space(file, tabs=8):
    with in_place.InPlace(file) as f_out:
        for line in f_out:
            line = tab__space(line, tabs)
            f_out.write(line)

@cli.command()
@click.option('-s', '--space', default=4, help='Number of spaces')
@click.argument('file', type=click.Path(exists=True))
def tab_even(file, space=4):
    with in_place.InPlace(file) as f:
        text = f.read()
        result = re.sub(r'\s\s+|\t', ' '*space, text)
        click.echo(result)
        f.write(result)

if __name__ == '__main__':
    cli()
Usage:
Output:
λ tab --help Usage: tab [OPTIONS] COMMAND [ARGS]... tab-space: Tab to space replace like Linux <expand> command Usage: tab-space --tabs 4 file_1.txt --- tab-even: Tab to equal spacing between text content Usage: tab-even --space 4 file_1.txt Options: --help Show this message and exit. Commands: tab-even tab-space # tab dos not give even space between words λ tab tab-space --tabs 4 file_1.txt One Two Three Four Five six seven One Two Three Four Five six seven One Two Three Four Five six seven λ tab tab-even --space 4 file_1.txt One Two Three Four Five six seven One Two Three Four Five six seven One Two Three Four Five six seven
Reply
#9
i was expecting some existing function that just does it.

i like that in_place module. too bad i can't assume every python installation has it. and i wish they would quit naming things with CamelCase.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#10
(Sep-08-2019, 06:17 PM)Skaperen Wrote: i was expecting some existing function that just does it.
Yes with your none exciting effort,you should except that Dodgy

Skaperen Wrote:i like that in_place module. too bad i can't assume every python installation has it. and i wish they would quit naming things with CamelCase.
There is a option in standard library fileinput with inplace=True,but it not so nice to use,in-place module dos it better and are simpler to use.

As mention all code is there,here taken out.
import re
import in_place

def tab_space(text, tabs=8):
    result = ''
    for c in text:
        if c == '\t':
            result += ' '
            while len(result) % tabs != 0:
                result += ' '
        else:
            result += c
    return result

def tab_even(file, space=4):
    with in_place.InPlace(file) as f:
        text = f.read()
        result = re.sub(r'\s\s+|\t', ' '*space, text)
        f.write(result)

if __name__ == '__main__':
    #--- Tab space ---#
    file = 'file_1.txt'
    tabs = 4
    with in_place.InPlace(file) as f_out:
        for line in f_out:
            line = tab_space(line, tabs)
            f_out.write(line)

    #--- Even Space ---#
    #file = 'file_1.txt'
    #tab_even(file, space=8)
Test file file_1.txt.
Output:
One Two Three Four Five six seven One Two Three Four Five six seven One Two Three Four Five six seven
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  function to untabify Skaperen 3 1,352 Aug-14-2022, 12:49 AM
Last Post: Skaperen
  a command to write i just thought of Skaperen 0 1,319 Apr-12-2020, 03:18 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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