Skip to content

Commit 4ce5f02

Browse files
committed
huge bug pix with purging articles, using special python semantics + fast list comprehension
1 parent cba8db5 commit 4ce5f02

3 files changed

Lines changed: 43 additions & 32 deletions

File tree

CONTRIBUTORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Thanks to the following contributors:
66
-------------------------------------
77
- Michael Hood - https://github.com/michaelhood
88
- Juliano Fischer - https://github.com/julianofischer
9+
- Alex Kessinger - https://github.com/voidfiles
910

1011
Newspaper relied on some code of a few other open source projects:
1112
------------------------------------------------------------------

newspaper/source.py

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class Feed(object):
3838
def __init__(self, url):
3939
self.url = encodeValue(url)
4040
self.rss = None
41-
# TODO self.dom = None ;; speed up Feedparser
41+
# TODO self.dom = None, speed up Feedparser
4242

4343

4444
class Source(object):
@@ -107,35 +107,21 @@ def build(self):
107107

108108
self.generate_articles()
109109

110-
def purge_articles(self, reason, in_articles=None):
110+
def purge_articles(self, reason, articles):
111111
"""
112112
Delete rejected articles, if there is an articles param, we
113113
purge from there, otherwise purge from our source instance.
114+
115+
Reference this excellent StackOverflow post for some of the wonky
116+
syntax below:
117+
http://stackoverflow.com/questions/1207406/remove-items-from-a-
118+
list-while-iterating-in-python
114119
"""
115-
# TODO Figure out why using the 'del' command on input list reference
116-
# isn't actually filtering the list?!
117-
# cur_articles = self.articles if in_articles is None else in_articles
118-
new_articles = []
119-
120-
for index, article in enumerate(in_articles):
121-
if reason == 'url' and not article.is_valid_url():
122-
#print 'deleting article', cur_articles[index].url
123-
#del cur_articles[index]
124-
#del in_articles[index]
125-
pass
126-
elif reason == 'url':
127-
new_articles.append(in_articles[index])
128-
129-
if reason == 'body' and not article.is_valid_body():
130-
#del cur_articles[index]
131-
pass
132-
elif reason == 'body':
133-
new_articles.append(in_articles[index])
134-
135-
if in_articles is not None: # if they give an input, output filtered
136-
return new_articles
137-
#else: # no input, we are playing with self.articles
138-
# self.articles = new_articles
120+
if reason == 'url':
121+
articles[:] = [a for a in articles if a.is_valid_url()]
122+
elif reason == 'body':
123+
articles[:] = [a for a in articles if a.is_valid_body()]
124+
return articles
139125

140126
@cache_disk(seconds=(86400*1), cache_folder=ANCHOR_DIRECTORY)
141127
def _get_category_urls(self, domain):
@@ -270,7 +256,7 @@ def feeds_to_articles(self):
270256
url=url,
271257
source_url=self.url,
272258
config=self.config
273-
# title=? # TODO: It **must** be fast
259+
# (pre) title=? # TODO: It **must** be fast
274260
)
275261
cur_articles.append(article)
276262

@@ -343,10 +329,7 @@ def generate_articles(self, limit=5000):
343329
"""
344330
articles = self._generate_articles()
345331
self.articles = articles[:limit]
346-
347-
# for a in self.articles:
348-
# print 'test examine url:', a.url
349-
# log.critical('total', len(articles), 'articles and cutoff was at', limit)
332+
# log.debug('total', len(articles), 'articles and cutoff was at', limit)
350333

351334
# @print_duration
352335
def download_articles(self, threads=1):
@@ -387,7 +370,7 @@ def download_articles(self, threads=1):
387370

388371
def parse_articles(self):
389372
"""
390-
Sync parse all articles, delete if too small.
373+
Parse all articles, delete if too small.
391374
"""
392375
for index, article in enumerate(self.articles):
393376
article.parse()

testclass.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
class Test(object):
3+
4+
def __init__(self):
5+
self.ll = range(11)
6+
7+
def _purge_list(self, ll=None):
8+
cur_ll = self.ll if not ll else ll
9+
10+
for index, e in enumerate(cur_ll):
11+
if e % 2 == 0:
12+
del cur_ll[index]
13+
return cur_ll
14+
15+
def purge_list(self):
16+
self.ll = self._purge_list()
17+
18+
19+
if __name__ == '__main__':
20+
t = Test()
21+
22+
print t.ll
23+
print 'purging list'
24+
25+
t.purge_list()
26+
27+
print t.ll

0 commit comments

Comments
 (0)