3 ms·
The python code here is not very idiomatic. Better would be something like: import re with open('companies') as f: important_companies = set(f)
by jacobolus 16y ago
The python code here is not very idiomatic. Better would be something like:
import re
with open('companies') as f:
important_companies = set(f)
articles = ['article1.md', 'article2.md', 'article3.md']
http_regex = re.compile(r"https?:\/\/(\w+)?(\.(\w+))*\.(com|org|net)")
def parse_article(a):
with open(a, 'r') as f:
words = f.read().split()
urls = (http_regex.search(w) for w in words)
companies = (match.group(0).capitalize() for match in urls if match)
return (c for c in companies if c in important_companies)
[c for a in articles for c in parse_article(a)]
(Note, I didn’t test this; I might be slightly misunderstanding some of what the code is supposed to do; the basic idea should be clear though. I think the original Python code posted might be broken: a regexp match object shouldn’t be directly subscriptable, it’s not clear why he’s opening the file 'atp' for each article, and I’m really not sure what’s supposed to happen when the url regexp doesn’t match. One nice thing to notice: in Python a file object can be iterated over, and will spit out its lines; passing a file into list() or set() will turn its lines into the appropriate data structure. But if explicit splitting is ever necessary, the author should use foo.splitlines() rather than foo.split('\n'). Finally, compiling a regexp for each word in every file is bad form, and he should really be using "raw" strings for regexps. My guess is that the Python code in the original article was never actually tested.)
Or even better, use a generator:
import re
with open('companies') as f:
important_companies = set(f)
articles = ['article1.md', 'article2.md', 'article3.md']
http_regex = re.compile(r"https?:\/\/(\w+)?(\.(\w+))*\.(com|org|net)")
def parse_article(a):
with open(a, 'r') as f:
for word in f.read().split():
url = http_regex.search(word)
if url:
name = url.group(0).capitalize()
if name in important_companies:
yield name
[c for a in articles for c in parse_article(a)]
In my opinion, either of these, but especially the second, makes control flow much much clearer than either the ruby or python examples in the original article.
- djacobs 16y agoI'm not a fan of if statements (especially nested) or for loops (except for side effects), which is why I coded like I did — in the style of a Lisp. That said, thanks for the excellent code. I'm going to look it over. I'll go back tomorrow and double-check the regex. I'm definitely not an expert with those objects in Python.