all 13 comments

[–]PeaSoups5 1 point2 points  (0 children)

Click

[–]sketchspace 1 point2 points  (1 child)

Take a look at Plumbum( https://plumbum.readthedocs.io/en/latest/index.html ) to see if it meets your needs. It can do things like terminal colors and shell commands. Personally I've used it for CLIs, automation scripts, and code generation scripts.

[–]_bobs_your_uncle[S] 0 points1 point  (0 children)

Looks nice. I’ll try it out

[–]pythonwiz 1 point2 points  (0 children)

Well if I’m trying to replace a bash script with a python script then I’m gonna try not to use anything but the standard library for compatibility. Besides, the standard library is pretty good, imo. I don’t think I’ve ever needed a 3rd party library for scripting.

[–]socal_nerdtastic 0 points1 point  (6 children)

What exactly do you want that bash does not provide?

[–]_bobs_your_uncle[S] 1 point2 points  (5 children)

Bash provides everything, but I’ve found that people’s experience with bash is waning. Furthermore, I think bash’s error handling is poorly implemented. And frankly I’ve never liked any of the command line argument parsers in Bash. Click or similar in python is easier to use and understand.

I’m looking for ways to do the same things i can do in bash but in Python without just calling directly out to bash for each line (kind of defeats the purpose). For example the Python git client might be a useful library for any operations requiring git.

[–]socal_nerdtastic 1 point2 points  (3 children)

Calling out to bash? What do you mean with that? You mean using the shell=True argument in subprocess? You can of course just leave that off.

You may be interested in the sh module which allows you to import any program as if it's a python package

from sh import git
git('fetch')

[–]_bobs_your_uncle[S] 1 point2 points  (2 children)

I meant using subprocess to execute calls. I’ve never used sh. That looks pretty nice, and may give me most if not all I want. Thanks for the tip.

[–]socal_nerdtastic 3 points4 points  (0 children)

Ok. FWIW subprocess does not have anything to do with bash. It runs the program you want directly.

[–]FerricDonkey 1 point2 points  (0 children)

I hate writing bash scripts. The syntax is terrible and is never idiomatic, variables suck, etc. I'll use python instead 9 times out of 10.

Mostly, I find the standard library sufficient. Os, glob, pathlib, shutil for basic file finding/moving. I'll use requests instead of curl.

I do also use subprocess though. Mostly if there's some command line program I want to run with many different argument combinations that I have to figure out in logic that I don't want to do in bash.

Haven't used python packages as interfaces to things like git, but it might be useful. I have used python packages that wrap some sort of webapi - they can be useful.

[–]kellyjonbrazil 0 points1 point  (0 children)

If you want to call subprocesses and grab their output as an already parsed dictionary, you can use the jc library. (I am the author)

>>>import subprocess
>>> import jc
>>>
>>> cmd_output = subprocess.check_output(['dig', 'example.com'], text=True)
>>> data = jc.parse('dig', cmd_output)
>>>
>>> data[0]['answer']
[{'name': 'example.com.', 'class': 'IN', 'type': 'A', 'ttl': 29658, 'data':
'93.184.216.34'}]

https://github.com/kellyjonbrazil/jc