Mercurial > hg > wordstream
comparison wordstream/api.py @ 10:bad7e66f4f24
py3
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Tue, 24 Nov 2020 10:33:22 -0800 |
parents | df84e61ae1e4 |
children | d2060c9bdb31 |
comparison
equal
deleted
inserted
replaced
9:ab37ae0e9cc0 | 10:bad7e66f4f24 |
---|---|
1 import urllib2 | 1 from urllib.request import urlopen |
2 from random import shuffle | 2 from random import shuffle |
3 | 3 |
4 class Corpus(dict): | 4 class Corpus(dict): |
5 | 5 |
6 def __init__(self, corpus=None): | 6 def __init__(self, corpus=None): |
24 stream.pop() | 24 stream.pop() |
25 | 25 |
26 def feed_stuff(self, *args): | 26 def feed_stuff(self, *args): |
27 for arg in args: | 27 for arg in args: |
28 if arg.startswith('https://') or arg.startswith('http://'): | 28 if arg.startswith('https://') or arg.startswith('http://'): |
29 text = urllib2.urlopen(arg) | 29 with urlopen(arg) as response: |
30 text = response.read() | |
30 else: | 31 else: |
31 text = file(arg).read() | 32 text = file(arg).read() |
32 self.feed_stream(text) | 33 self.feed_stream(text) |
33 | 34 |
34 def scramble(self): | 35 def scramble(self): |