Mercurial > hg > wordstream
changeset 17:d2060c9bdb31
py3
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Tue, 24 Nov 2020 10:47:20 -0800 |
parents | 50b319ad87b8 |
children | 6718f2808a65 |
files | wordstream/api.py |
diffstat | 1 files changed, 8 insertions(+), 10 deletions(-) [+] |
line wrap: on
line diff
--- a/wordstream/api.py Tue Nov 24 10:44:13 2020 -0800 +++ b/wordstream/api.py Tue Nov 24 10:47:20 2020 -0800 @@ -29,30 +29,28 @@ with urlopen(arg) as response: text = response.read() else: - text = file(arg).read() + text = open(arg).read() self.feed_stream(text) def scramble(self): for i in self: shuffle(self[i]) - def save(self, filename): + def save(self, f): named = False - if isinstance(f, basestring): + if isinstance(f, str): named = True - f = file(f) + f = open(f) for key in sorted(self.keys()): - print >> f, "%s %s" % (key, ' '.join(self[key])) + f.write("%s %s\n" % (key, ' '.join(self[key]))) if named: f.close() - + def load(self, f): - if isinstance(f, basestring): - f = file(f) - + if isinstance(f, str): + f = open(f) @classmethod def restore(cls, filename): corpus = cls() corpus.load(filename) -