6 ms·
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 wrot
by cpdean 3y ago
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/ https://pmitev.github.io/to-awk-or-not/Python_vs_awk/
- version_five 3y agoLooks 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.
- randlet 3y agofloats = [float(v) for v in integerList] or less idiomatic floats = map(float, integerList)
- wiredfool 3y agoExcept 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)
- sgarland 3y agoI 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.
- NoboruWataya 3y agoAgreed - 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]`.
- chillpenguin 3y agoI 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.
- pcthrowaway 3y agoI 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)`
- enriquto 3y ago> 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)`
- nawgz 3y agoI don't think writing a function called `float` that apparently accepts arrays as an argument is very elegant at all
- elesiuta 3y ago> 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-gaussian-job/#:~:text=the%20frequency%20computation.-,Frequency%20Data,-Lines%201097%2D1113 https://dornshuld.chemistry.msstate.edu/comp-chem/first-gaus...
- andrepd 3y ago> I used chatGPT to simplify it a bit but it also introduced some errors Lmaoooo