Skip to content

Commit

Permalink
add file open modes based on python version
Browse files Browse the repository at this point in the history
  • Loading branch information
barrust committed May 13, 2019
1 parent fa96024 commit a7e3855
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
4 changes: 2 additions & 2 deletions spellchecker/spellchecker.py
Expand Up @@ -324,8 +324,8 @@ def tokenize(self, text):
str: The next `word` in the tokenized string
Note:
This is the same as the `spellchecker.split_words()` """
for x in self._tokenizer(text):
yield x.lower()
for word in self._tokenizer(text):
yield word.lower()

def keys(self):
""" Iterator over the key of the dictionary
Expand Down
9 changes: 6 additions & 3 deletions spellchecker/utils.py
Expand Up @@ -8,9 +8,12 @@
import io # python 2 text file encoding support

OPEN = io.open # hijack this
READMODE = 'rb'
WRITEMODE = 'wb'
else:
OPEN = open

READMODE = 'rt'
WRITEMODE = 'wt'

@contextlib.contextmanager
def load_file(filename, encoding):
Expand All @@ -24,7 +27,7 @@ def load_file(filename, encoding):
str: The string data from the file read
"""
try:
with gzip.open(filename, mode="rt") as fobj:
with gzip.open(filename, mode=READMODE) as fobj:
yield fobj.read()
except (OSError, IOError):
with OPEN(filename, mode="r", encoding=encoding) as fobj:
Expand All @@ -42,7 +45,7 @@ def write_file(filepath, encoding, gzipped, data):
data (str): The data to be written out
"""
if gzipped:
with gzip.open(filepath, "wt") as fobj:
with gzip.open(filepath, WRITEMODE) as fobj:
fobj.write(data)
else:
with OPEN(filepath, "w", encoding=encoding) as fobj:
Expand Down

0 comments on commit a7e3855

Please sign in to comment.