Skip to content

Commit

Permalink
Make sure return format is valid. Otherwise raise Valueerror. #1614
Browse files Browse the repository at this point in the history
  • Loading branch information
shyuep committed Oct 1, 2019
1 parent ce672ac commit 4fffd3e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
31 changes: 12 additions & 19 deletions pre-commit
Expand Up @@ -8,23 +8,16 @@
# To enable this hook, rename this file to "pre-commit".

status=0
files=$(git diff --diff-filter=d --cached --name-only | grep -E 'pymatgen.*\.(py)$' | sed '/test_/d')
files=$(git diff --diff-filter=d --cached --name-only | grep -E 'monty.*\.(py)$' | sed '/test_/d')
files=`echo $files | tr '\n' ' '`
echo "Check code styles for $files"
pycodestyle pymatgen/dao.py $files
if [ $? -ne 0 ]; then
echo "Bad code style detected by pycodestyle. Fix before commit."
exit 1
fi
echo "Flake8 for $files"
flake8 pymatgen/dao.py $files
if [ $? -ne 0 ]; then
echo "Bad code style detected by flake8. Fix before commit."
exit 1
fi
echo "Check doc styles for $files"
pydocstyle --match='(?!test_).*\.py' pymatgen/dao.py $files
if [ $? -ne 0 ]; then
echo "Bad doc style detected. Fix before commit."
exit 1
fi
if [ "$files" != " " ]; then
for cmd in pycodestyle mypy flake8 pydocstyle
do
echo "Running $cmd $files..."
$cmd $files
if [ $? -ne 0 ]; then
echo "$cmd failed. Fix before commit."
exit 1
fi
done
fi
4 changes: 3 additions & 1 deletion pymatgen/core/structure.py
Expand Up @@ -1871,13 +1871,15 @@ def to(self, fmt=None, filename=None, **kwargs):
from pymatgen.io.prismatic import Prismatic
s = Prismatic(self).to_string()
return s
else:
elif fmt == "yaml" or fnmatch(fname, "*.yaml*") or fnmatch(fname, "*.yml*"):
import ruamel.yaml as yaml
if filename:
with zopen(filename, "wt") as f:
yaml.safe_dump(self.as_dict(), f)
return None
return yaml.safe_dump(self.as_dict())
else:
raise ValueError("Invalid format.")

if filename:
writer.write_file(filename)
Expand Down
3 changes: 3 additions & 0 deletions pymatgen/core/tests/test_structure.py
Expand Up @@ -518,6 +518,9 @@ def test_to_from_file_string(self):
s = Structure.from_file("Si_testing.yaml")
self.assertEqual(s, self.struct)

self.assertRaises(ValueError, self.struct.to, filename="whatever")
self.assertRaises(ValueError, self.struct.to, fmt="badformat")

# Test Path support.
s = Structure.from_file(Path("Si_testing.yaml"))
self.assertEqual(s, self.struct)
Expand Down

0 comments on commit 4fffd3e

Please sign in to comment.