Python Forum
writing all new scripts in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
writing all new scripts in Python
#1
of those using Linux, how many of you, when you need to write a script to automate something, or make something work better, or just process some data, always do it in Python? (e.g. not a shell scripting language like bash)
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
I tend to write as many scripts as I can in python. However, it is not always possible. For example by calling a python script, one cannot change the current directory in the calling shell. So one day I wanted to have a python script to define a extended version of the 'cd' command. The command I wrote is called 'va' which is the french imperative mood for 'go'. So if I type in a shell
Output:
va spam
then the shell changes current directory to whatever directory I associate with the word 'spam'. One cannot do this with a python script, so I had to define 'va()' as a bash function defined in the file '~/.bashrc'. Here is its code
Output:
function va() { va_retour=$(python ~/bin/va.py $*) echo $va_retour if [[ ! -z "${va_retour// }" ]] then cd "$va_retour" fi }
When invoked, the function calls a python script with the command for example python ~/bin/va.py spam. This python script prints the name of the target directory on its stdout, then bash does the 'cd'.

It took me some research on the web to find out how to do this because I'm not very good at shell programming but I've been using this 'va' command for a few years now and it is very convenient.
Reply
#3
I use bash, awk and sed or just type commands separately, it all depends on the task, and how often I think I will use it.
Reply
#4
i am getting to the point where everything i do as a script in a file is always done in Python3 and no more in Bash. Bash functions, though, still have to be done in Bash until the day someone figures out how to merge Bash into Python (including interactive). i do have a ton of Bash functions, so i understand what you were doing with av(). i also do functions in place of aliases. there are some oldies i do need to rewrite. but i rarely come up with any use for Python inside the function. for directory aliases i use symlinks, like ulb -> /usr/local/bin and a few others like that. i think i have about 300 functions, over 600 scripts, and a couple dozen binaries from my C programming days. it is those binaries i want to rewrite into Python soon, so it's easier to use the new architectures in the cloud.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
and of course mainly python
Reply
#6
Skaperen Wrote:i think i have about 300 functions, over 600 scripts, and a couple dozen binaries from my C programming days.
It sounds like an enticing library, why not share some of these scripts on a website? It may contain utilities for all Linux users, and people could contribute to migrating the scripts to Python.
Reply
#7
sharing ends up with support questions. for now, i limit my sharing to stuff not customized for my environment and where someone seems to have a specific need.

but i should share some things soon, like a set of commands to manage background shell sessions done using screen. i should go through the code and clean it up, first. it still has lots of my diagnostic output going on. it lets each user have any number of named background sessions. the "bgi" command passes input to the named session. one of my sessions right now is playing music, so none of my terminal windows is tied up doing that. i run various other things there, like backups, C compiles, long searches. i still have a few minor bugs to fix, too, like refusing duplicate names (if you create 2 sessions of the same name you can no longer input to either). and the parsing of virtual screen size settings needs to be improved.

i have a command i originally wrote in C called "rls". i use it more than "ls". it uses my AVL tree code in C. having written a generator in Python to walk a file tree in the same order, i'm getting closer to redoing "rls" in Python. i will definitely share that after i try it until it is stable. one of the things i like about "rls" is a consistent output format that makes sorting easier. but i hope to better integrate some sorting into the new "rls" command (and the generator, too).
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
here's one of my simple commands. when you have output from a command that has columns that are not aligned vertically, this script command lines it up by getting the maximum width of each column then justifying each column of each row to that size, padded with spaces, left justified, except for decimal numbers right justified. it also takes data in arguments as a convenience for shell scripts.

http://ipal.net/python/align_columns.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Line up columns of input text."""
from sys import argv,stdin
argv.pop(0)
size = [] # indexed by col number
rows = [] # indexed by row number
for data in argv if argv else stdin:
    # in case an argument has 2 or more lines;
    for line in data.splitlines():
        cols = tuple(line.split())
        rows.append(cols)
        x = len(cols)
        y = len(size)
        if y<x:
            size[:] = size+(x-y)*[1]
        for n in range(x):
            size[n] = max(size[n],len(cols[n]))
for row in rows:
    new = []
    n = 0
    for col in row:
        if col.isdecimal():
            new.append(col.rjust(size[n]))
        else:
            new.append(col.ljust(size[n]))
        n += 1
    print(' '.join(new).rstrip())
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

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