Skip to content

Commit

Permalink
Remove unnecessary casts of Path objects to str
Browse files Browse the repository at this point in the history
More standard library functions accept Path objects as of Python 3.6.

Part of #219
  • Loading branch information
goodmami committed Jan 10, 2020
1 parent 7333804 commit c7b992d
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 6 deletions.
2 changes: 1 addition & 1 deletion delphin/codecs/dmrspenman.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def load(source):
a list of DMRS objects
"""
if not hasattr(source, 'read'):
source = str(Path(source).expanduser())
source = Path(source).expanduser()
graphs = penman.load(source)
xs = [from_triples(g.triples) for g in graphs]
return xs
Expand Down
2 changes: 1 addition & 1 deletion delphin/codecs/edspenman.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def load(source):
a list of EDS objects
"""
if not hasattr(source, 'read'):
source = str(Path(source).expanduser())
source = Path(source).expanduser()
graphs = penman.load(source)
xs = [from_triples(g.triples) for g in graphs]
return xs
Expand Down
7 changes: 3 additions & 4 deletions delphin/tsdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,9 +765,8 @@ def open(dir: util.PathLike,
... sentences.append(tsdb.split(line)[6])
"""
path = get_path(dir, name)
# open and gzip.open don't accept pathlib.Path objects until Python 3.6
if path.suffix.lower() == '.gz':
return gzopen(str(path), mode='rt', encoding=encoding)
return gzopen(path, mode='rt', encoding=encoding)
else:
return path.open(encoding=encoding)

Expand Down Expand Up @@ -843,7 +842,7 @@ def write(dir: util.PathLike,

with tempfile.NamedTemporaryFile(
mode='w+b', suffix='.tmp',
prefix=name, dir=str(dir)) as f_tmp:
prefix=name, dir=dir) as f_tmp:

for record in records:
f_tmp.write(
Expand All @@ -856,7 +855,7 @@ def write(dir: util.PathLike,
# now copy the temp file to the destination
f_tmp.seek(0)
if gzip:
with gzopen(str(dest), mode) as f_out:
with gzopen(dest, mode) as f_out:
shutil.copyfileobj(f_tmp, f_out)
else:
with dest.open(mode=mode) as f_out:
Expand Down

0 comments on commit c7b992d

Please sign in to comment.