Hacker News new | past | comments | ask | show | jobs | submit login

Python is my absolute favorite language but it's not suitable for the kinds of things you would use bash for.

This is the real code that a Ansible uses to run a shell command correctly and is 350 lines and is still a small subset of the features of a single line of bash. https://github.com/ansible/ansible/blob/a2776443017718f6bbd8...

The Python code to do what a single mv invocation does is 120 lines https://github.com/ansible/ansible/blob/a2776443017718f6bbd8...

People always focus on the footguns that exist in Bash the language but ignore how much systems programming is abstracted away from you in the shell environment.

In Bash you can enter a Linux namespace with a single nsenter invocation. If you want to do the same in Python you have use ctypes and call libc.clone manually.




How is that a remotely real comparison? The ansible mv function deals with preserving SELinux context, which mv doesn't do, and it automatically deals with a whole lot of common error conditions, whereas mv just fails. If you just want to replicate mv, Python has shutil.move, one line of code. Ansible is trying to do a lot more.

By the way, I don't know if this is the canonical implementation, but FreeBSD mv is 481 lines of C: https://github.com/freebsd/freebsd-src/blob/master/bin/mv/mv...


The code you are showing does a lot more than MV:

- it's portable accross OSes (and keep flags if the OS supports it, deal with encoding, etc)

- it ensures selinux context is saved if there is such a thing

- it has proper and rich error communication with the calling code

- it's includes documentation and comments

- it outputs json par parsing and storage

No to say "mv" is not awesome, because it is. There is much more boiler plate in python, and is why I'll often do subprocess.check_call(['mv', 'src', 'dst']) if my script is linux only.

But you are pushing it


i think its not fair comparison. mv command implementation in 'C' might have more lines of code. Maybe we should complain that there are no OOTB library functions in python to move the file.


I don't really care how many lines of code there are in an implementation. I care how many lines of code I actually have to write.

Python has shutil.move and os.rename but the Ansible example is to illustrate that there's a lot of code that needs to surround those calls to make them useful and they're not 1-1.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: