Skip to content

Commit

Permalink
Raise more helpful error when reading a blank file
Browse files Browse the repository at this point in the history
  • Loading branch information
theavey committed Dec 13, 2018
1 parent 013432e commit 4ce75ff
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 2 deletions.
6 changes: 4 additions & 2 deletions paratemp/geometries.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
__all__ = ['rotation_matrix', 'Vector', 'XYZ', 'COM']



def rotation_matrix(axis, theta):
"""
Return the rotation matrix associated with counterclockwise rotation about
Expand Down Expand Up @@ -101,9 +100,12 @@ def __init__(self, f_name):
self.file = f_name
with open(f_name, 'r') as f_file:
f_lines = f_file.readlines()
if len(f_lines) < 2:
raise TypeError('The given file {} appears '
'to be empty'.format(f_name))
self._header = f_lines[0:2]
if 'Energy' in self._header[1]:
energy_match = re.search('(?:Energy:\s+)(-\d+\.\d+)',
energy_match = re.search(r'(?:Energy:\s+)(-\d+\.\d+)',
self._header[1])
self._energy = float(energy_match.group(1))
else:
Expand Down
Empty file added tests/test-data/empty.txt
Empty file.
Empty file added tests/test-data/empty_line.txt
Empty file.
4 changes: 4 additions & 0 deletions tests/test_geometries.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ def test_bad_lines(self):
XYZ('tests/test-data/stil-3htmf-bad.xyz')
with pytest.raises(ValueError):
XYZ('tests/test-data/stil-3htmf-bad-2.xyz')
with pytest.raises(TypeError):
XYZ('tests/test-data/empty.txt')
with pytest.raises(TypeError):
XYZ('tests/test-data/empty_line.txt')


class TestXYZNewline(object):
Expand Down

0 comments on commit 4ce75ff

Please sign in to comment.