Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I'm a huge fan of awk but the "Python vs awk" page this links to [1] shows python code that's almost deliberately atrocious.

Take this function the author wrote for converting a list of integers (or strings) into floats

    def ints2float(integerlist):
        for n in range(0,len(integerlist)):
            integerlist[n]=float(integerlist[n])
        return integerlist
Using `range(0,len(integerlist))` immediately betrays how the author doesn't understand python. The first arg in `range` is entirely redundant. Mutating the input list like this is also just bad design. If someone has used python for longer than a month, you'd write this with just `[float(i) for i in integerlist]`.

Further down in the function `format_captured` you see this attempt at obfuscation:

    freqs=ints2float(filter(None,captured[n].split(' '))[2:5])
Why bother with a `filter`? Who hurt you?

    freqs = ints2float(captured[n].split(' ')[2:5])
That said, the author's implementation in awk does look pretty clean. I'm just peeved that they straw-manned the other language.

[1] https://pmitev.github.io/to-awk-or-not/Python_vs_awk/



> python code that's almost deliberately atrocious

That code was so bad I felt I had to step in too, I used chatGPT to simplify it a bit but it also introduced some errors, so I found what appears to be an input file to test it on [1]. The only difference with the awk program is that it uses spaces while the original python program used tabs.

  #!/usr/bin/env python3
  import sys
  
  freq, fc, ir = [], [], []
  with open(sys.argv[1]) as f:
      for line in f.readlines():
          words = line.split()
          if "Frequencies" in line:
              freq.extend(words[2:])
          elif "Frc consts" in line:
              fc.extend(words[3:])
          elif "IR Inten" in line:
              ir.extend(words[3:])
  
  for i in range(len(freq)):
      print(f"{freq[i]}\t{fc[i]}\t{ir[i]}")
[1] https://dornshuld.chemistry.msstate.edu/comp-chem/first-gaus...


> I used chatGPT to simplify it a bit but it also introduced some errors

Lmaoooo


Agreed - this is pretty much the perfect use case for list comprehensions, which are one of the best features of Python. Normally "oh but there's a better way to do it in that language" isn't a particularly interesting observation, but here it completely turns the author's point on its head. I can't think of many more elegant ways to convert a list of ints to floats, in any language, than `[float(i) for i in integerlist]`.


I agree. But regarding

> I can't think of many more elegant ways to convert a list of ints to floats, in any language, than `[float(i) for i in integerlist]`.

I think something like `integerlist.map(float)` is at least a contender.


I prefer the python syntax generally, but throw in some typing with Typescript inferring the post-map type from the map function's return type, and I'd definitely go with `integerlist.map(float)`


> I can't think of many more elegant ways to convert a list of ints to floats, in any language, than `[float(i) for i in integerlist]`.

What about `float(integerlist)`


I don't think writing a function called `float` that apparently accepts arrays as an argument is very elegant at all


I wanted to point out that the Python code was written to be 2.7 compatible, and maybe the atrociousness was due to that, but then I looked up when list comprehensions were introduced - 2.0, with PEP202.


Looks like he copy pasted the python version from another forum post, and didn't look at it carefully. I'd suspect it can be made to look a lot cleaner (edit, yes, e.g. by just translating each of the main lines in the awk script to an if statement) I agree with the strawman comment.


    floats = [float(v) for v in integerList]
or less idiomatic

    floats = map(float, integerList)


Except that these are different -- the second returns an iterator (at least in python3). You'd need

   list(map(float, integerList)) 
for them to be equivalent.

(It doesn't matter in a lot of cases, but there are enough edges where it does. Json serialization for one)




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

Search: