Hacker News new | ask | show | jobs
by robax 2870 days ago
I'd love this for python as I'm not a big fan of Ruby. Does such a thing exist?
4 comments

Not well tested... just whipped this up.

The example to sort df -h by the percent:

  df -h | p 'sorted({}[1:], key=lambda x: int(x.split()[-2][:-1]))'
I saved this as ~/bin/p

  #!/usr/bin/env python3
  
  import sys
  
  single_line = sys.argv[1] == '-l'
  code = ' '.join(sys.argv[2 if single_line else 1:])
  if single_line:
      for line in sys.stdin:
          print(eval(code.format('line.strip()')))
  else:
    print('\n'.join(eval(code.format('sys.stdin.read().splitlines()'))))
Several people have tried to tackle this in Python. My attempt was python-oneliner[1]. There's also a list of similar projects in the readme.

[1] https://github.com/gvalkov/python-oneliner

I made myself a little utility that's kinda like that: https://gist.github.com/dschep/4358be665537463b9271f782e77ff...
Out of interest, why are you not a fan of Ruby?
Not the parent but speaking myself, I feel the need to pick one or the other.

I'm not saying I hate Ruby. I just haven't take the time (nor had a reason to, frankly) become a fan the way I have with Python. Being similar languages in terms of both being dynamic, malleable, mostly single threaded and REPL-able they both seem to occupy the same spot in my toolbox.

Not to knock Ruby-the-language, but the one hitch is that Python's ecosystem is quite a bit vaster into fields I don't interact with (biology, physics, etc.) but also with many that I do (numpy/sympy, cli utilities and scripting, opencv, curl bindings). Where I'm at, it's most everyone's secondary or tertiary language.

Nothing against Ruby, it's just circumstance. If anything I might pick it up soon if only to access JRuby/TruffleRuby worlds which are much ahead of Jython/GraalPython.

Oh, except for function calls without (). And I'm not huge on DSLs. They seem icky but I grew to love significant whitespace in Python so maybe I'll grow to love those too.

Right tool for the job; don't be a fan boy.
As someone who prefers Python to Ruby, Ruby is better for this job because there are so many more methods on Array/Enumerable than on Python list (or Python's global functions.) And them all being methods makes life simpler too.