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.
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)
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)`
> 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
> 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...
Take this function the author wrote for converting a list of integers (or strings) into floats
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:
Why bother with a `filter`? Who hurt you? 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/