Live data from Hacker News

Using Python for Scripting

hypirion.com

81–90 of 112 posts

Re: Using Python for Scripting

#81

If a script is simple - I use posix sh + awk, sed, etc. But if a script I write needs to use arrays, sets, hashtable or processes many files - I use Nim[0]. It's a compiled systems-programming language that feels like a scripting language: - Nim is easy to write and reads almost like a pseudocode. - Nim is very portable language, runs almost anywhere C can run (both compiler and programs). - `nim r script.nim` to com…

I might need to try it out. However, I haven't really found a use case yet where the speed of Python has been a major factor in my day job. It's usually fast enough and is a lot easier to optimize than many languages.

I actually sped up a script the other day that had been written in bash by 200x by moving it over to Python and rewriting the regexes so they could run on whole files all at once instead of line by line. Performance problems are most often from poorly written code in my experience, not a slow language.

Re: Using Python for Scripting

#82
post #36

Earlier quoted context omitted.

Ok tbh bash uses the system package manager to install command-line utilities, and using the system package manager for python packages HAS been tried, and the parallel use of system packaging and independent package managers is part of why python package distribution is such a mess.

Why using independent package managers alongside the system one? I think the introduction of non system packagers is what brought us the whole mess we are in. Most system packagers allows for custom repositories.

Because the system package repository doesn't package everything & isn't always up to date. And if you introduce other repos to fix this, then you have an ecosystem with multiple instances of the same package distributed at different versions in different repositories, and God help you if you need multiple versions of one package, or if you need a different version of Python entirely. Nix could do it, but not anything else.

No—system python is for the system's scripts, and user projects should have their dependencies sandboxed in a virtual environment. That's the only model that really works.

Re: Using Python for Scripting

#83

I like the message the article is trying to convey, Python is good alternative to complicated shell scripts in my opinion. I do wonder, let's say the scripting file is using lots of libraries, do you have to include some kind of requirements.txt file with it aswell when you want to share it with other people? In Ruby, there is inline bundler which makes sharing a single Ruby script very portable. https://bundler.io/g…

uv does that these days:

  # /// script
  # dependencies = [
  #   "requests

Re: Using Python for Scripting

#84
This from the site:

  /*
  Oh, you're looking at my CSS. Here be dragons.

  Let's bundle in a rant while you're here: I'd like to use variable width fonts
  instead of fixed size fonts. I enjoy 300 (i.e. light) and 600 (i.e. semibold)
  over regular and bold for Crimson Pro only. But Chrome doesn't like to show
  anything but 400 and 700 with variable width fonts, so I'll have to trick Chrome
  by declaring my 300 and 600 fonts as if they were 400 and 700 respectively.

  ... or well, I could use font-variation-settings, but that's a global override
  which means I have to specify it EVERYWHERE and that's just too much effort for
  poor me.
  */

Re: Using Python for Scripting

#85
post #3

How do you handle packages? I want scripts to a be a single file with a shebang, not a repo with a requirements.txt that I need to run in a venv. To me, this is the biggest blocker to using Python for any non-trivial scripting (which is precisely the kind where I wouldn't want to use bash), but I'd like to know how others deal with it. C# scripts let you reference packages in a comment at the top of the file, for exa…

Honestly, I just use a requirements.txt file. It's not as convenient as having a single file, it's true, but that is far outweighed by how much easier Python is to use than bash. So I'm giving up a 3/10 convenience but gaining a 10/10 convenience, which I think is worth it.

Re: Using Python for Scripting

#86
post #30

Earlier quoted context omitted.

> How do you handle packages? The same way you handle them with bash? Install them? What are we talking about here?

I think it is the fact that python packaging has been problematic for some time. setuptools/easy_install, pip/pipx, poetry, conda, uv all promise they will be the thing that "fixes" it

Eh. Python packaging is just fine. Set up a venv, install with pip, done. I think that the difficulty of installing Python packages is wildly overblown on this site.

Re: Using Python for Scripting

#87

The Python stdlib does not get enough credit. People complain about things like how its http client is dated and slow, but it’s pretty amazing that it’s just right there if you need it, no external dependencies needed. And it’s sitting right next to difflib, graphlib, pathlib, struct, glob, tkinter, and dozens of others. Sure, every one of these is limited individually, but those limitations are stable and well under…

I strongly agree! Sure, the stuff in the Python stdlib might not be the best available - but it's always there. I've been in environments where I couldn't install requests but urllib2 meant that I could still get the job done. I think that every language should have a batteries-included stdlib.

Re: Using Python for Scripting

#88
post #30

Earlier quoted context omitted.

I think it is the fact that python packaging has been problematic for some time. setuptools/easy_install, pip/pipx, poetry, conda, uv all promise they will be the thing that "fixes" it

Eh. Python packaging is just fine. Set up a venv, install with pip, done. I think that the difficulty of installing Python packages is wildly overblown on this site.

not “AI Bubble” overblown but close

Re: Using Python for Scripting

#89
Funny this shows up on HN now! Jean Niklas wrote his thesis about RRB trees, which is an evolution of the persistent vectors of clojure fame. I have spent many hours reading his thesis lately, because I just spent two months porting his c-rrb (https://github.com/hypirion/c-rrb) to c#, which was a fun endeavour. I wish I would have read hus thesis better since I spent two weeks debugging issues that arise from me not enforcing the leftwise dense invariant with regards to the tail.

Re: Using Python for Scripting

#90
post #75

Earlier quoted context omitted.

One thing I can recommend that makes scripting in python with external commands a lot easier is the `sh` module: https://pypi.org/project/sh/ Basically you can just `from sh import [command]` and then have an installed binary command available as function from sh import ifconfig print(ifconfig("eth0"))

Please don't use "sh" python library! By default (1) captures stdout and stderr of all processes and (2) create tty for processs stdout. Those are really bad defaults. The tty on stdout means many programs run in "interactive" rather then "batch" mode: programs which use pager get output truncated, auto-colors may get enabled and emit ESC controls into output streams (or not, depending on user's distro... fun!). And…

> They fixed it.. by disabling tty only for "git" command.

Wow... yes sounds like a library to avoid!

Post reply on HN