CLI tools hidden in the Python standard library (til.simonwillison.net)
from learnbyexample@programming.dev to python@programming.dev on 18 Feb 2024 04:00
https://programming.dev/post/10193753

#python

threaded - newest

floppingfish@kbin.social on 18 Feb 2024 10:28 next collapse

Nice work!

nickwitha_k@lemmy.sdf.org on 18 Feb 2024 11:21 next collapse

Easier than grepping, you can just look at the standard library docs: docs.python.org/3/library/

BatmanAoD@programming.dev on 19 Feb 2024 18:31 collapse

How is that easier? It doesn’t look like it provides a list of which modules have a __name__ == “__main__” block.

nickwitha_k@lemmy.sdf.org on 21 Feb 2024 18:16 collapse

No. But, it does provide a list of all stdlib libraries and those, like gzip, that are intended to be compatible with the CLI tend to have explicit documentation showing usage (ex. docs.python.org/3/library/gzip.html) and provides any other contextual info related to using the library.

Don’t get me wrong, grepping through the code is a great way of building skills needed as a professional. Really, I have to do this kind of thing multiple times every week at work. It is, however, also worth noting that Python only uses an “honor system” for public/private functions, methods, and classes so modules having an “if name == ‘main’” block doesn’t necessarily mean that they are appropriate to use as CLI tools. They might be but, without documentation to back it up, it’s an “at your own risk” situation.

NotAtWork@startrek.website on 18 Feb 2024 15:23 next collapse

CLITools can be hard to find, but when you do it’s worth the effort.

moonpiedumplings@programming.dev on 18 Feb 2024 16:59 next collapse

python -m http.server has been a very useful tool to me, to test if a server is accessible.

wasabi@feddit.de on 18 Feb 2024 17:11 next collapse

python -m http.server came in handy so many times!

ATLeagle@mastodon.online on 18 Feb 2024 17:32 collapse

@wasabi @learnbyexample one of my favorite super lazy ways to share a file

sugar_in_your_tea@sh.itjust.works on 18 Feb 2024 17:28 next collapse

Pro tip: you can ignore filenames with ripgrep using -g “!test/”. That should speed things up a bit.

namingthingsiseasy@programming.dev on 19 Feb 2024 00:21 collapse

I use json.tool a lot to format JSON directly in vim. Simply highlight the text you want to format and run :!python3 -m json.tool. There are probably plugins to do this too, but doing it this way is probably the simplest, unless vim has a built-in for it.

sugar_in_your_tea@sh.itjust.works on 19 Feb 2024 02:58 collapse

Could probably do the same with :! jq . It’s a bit shorter.

namingthingsiseasy@programming.dev on 19 Feb 2024 14:34 collapse

Yeah, I knew someone was going to say that. Usually it’s more likely that Python is installed than jq - especially on servers. But yes, that would definitely work too.

sugar_in_your_tea@sh.itjust.works on 19 Feb 2024 15:44 collapse

Sure, I’m just not sure when I’d ever run into that. Either I’m doing it a lot and installing jq is reasonable, or I’m not allowed on the server anyway and need to copy/paste from logs.

I used to use python -m json.tool a lot, but I haven’t needed to in many years.