4 ms·
The Python code is unnecessarily verbose and doesn't actually work: • No need to involve Path objects; open("beef.txt") works. • The first argument for filter
by jwilk 4y ago
The Python code is unnecessarily verbose and doesn't actually work:
• No need to involve Path objects; open("beef.txt") works.
• The first argument for filter() should be the filter, not the iterable.
• The stream itself is iterable, so no need to call the readlines() method.
• The first argument for re.match() should be the pattern, not the string.
• You probably wanted re.search(), not re.match(). Or, since this is a fixed string, use the ‘in’ operator.
• ‘is not None’ is not necessary for checking if the match was successful.
• It's ‘reversed’, not ‘reverse’.
• All that sorting and reversing is unnecessary when you're going to put it in a set anyway.
- amelius 4y agoCould you post a rewritten version?
- alpaca128 4y agoSomething like this with open("beef.txt") as file: lines = [line for line in file if "lasagna" in line] print(set(lines))
- forgotpwd16 4y agoIn author's defense, code is apparently meant to exemplify the differences between shell and general programming languages mentioned in bullet points. So working doesn't matter and isn't even mentioned that is in Python.
- BoppreH 4y agoIt's also not performing the numeric sort ('-n'), which is arguably the hardest part because it's not a Python built-in. A bad example all around. Here's my attempt[1]: import re lines = list(set(line for line in open('beef.txt') if 'lasagna' in line)) lines.sort(key=lambda line: int(re.match('\d*', line)[0] or 0)) for line in lines: print(line, end='') Python is especially bad at this task for a number of reasons: - Iterating over the lines yields strings ending with newlines. If you naively loop and print the sorted lines, `print` will add a second newline, resulting in every other line being empty. Hence the `end=''`. - Python's str has many helper functions, but none for numeric sort, so you need an import, a regex, and a fallback value. - It's very imperative with a big mutation in the middle. Using `sorted(lines, key=...)` and `print(''.join(lines))` would be simpler and safer, but each would double the memory usage for large inputs. - Filters using list comprehensions ('x for x in y if z in x') are idiomatic and readable but very verbose. - The concepts are a lot more complicated. Here we have file handles, magic iterators, first-class-functions, sets, generators, lists, keyword arguments, regexes, overloaded indexes, etc. A beginner would blindly copy and run, and have no chance to fix any issues that appear. - Because Python has almost no static analysis (undefined names are runtime errors!), you'll have to find most errors by running your code. For a quick shell script this usually mean running over the real inputs, giving a horribly slow edit-compile-run loop and be potentially dangerous. [1]: https://gist.github.com/boppreh/3cf5489da92d85f11e152739320d147e https://gist.github.com/boppreh/3cf5489da92d85f11e152739320d...