News for hackers. Stuff that matters.
Re:Fascination of Awk by cpdean on Friday March 24, 2023 @07:15AM (#35287526)
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/

Re:Fascination of Awk by version_five on Friday March 24, 2023 @07:20AM (#35287572)
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.
Re:Fascination of Awk by randlet on Friday March 24, 2023 @09:45AM (#35288998)

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

    floats = map(float, integerList)
Re:Fascination of Awk by wiredfool on Friday March 24, 2023 @10:49AM (#35289760)
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)

Re:Fascination of Awk by sgarland on Friday March 24, 2023 @08:17AM (#35288029)
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.
Re:Fascination of Awk by NoboruWataya on Friday March 24, 2023 @12:13PM (#35291194)
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]`.
Re:Fascination of Awk by chillpenguin on Friday March 24, 2023 @12:57PM (#35291973)
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.

Re:Fascination of Awk by pcthrowaway on Friday March 24, 2023 @05:39PM (#35295970)
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)`
Re:Fascination of Awk by enriquto on Friday March 24, 2023 @02:31PM (#35293524)
> 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)`

Re:Fascination of Awk by nawgz on Friday March 24, 2023 @03:57PM (#35294598)
I don't think writing a function called `float` that apparently accepts arrays as an argument is very elegant at all
Re:Fascination of Awk by elesiuta on Friday March 24, 2023 @01:52PM (#35292963)
> 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...
Re:Fascination of Awk by andrepd on Monday March 27, 2023 @06:42AM (#35325005)
> I used chatGPT to simplify it a bit but it also introduced some errors

Lmaoooo