Skip to content

Commit

Permalink
Merge pull request #310 from tovrstra/py3.12
Browse files Browse the repository at this point in the history
Fix Python 3.12 compatibility
  • Loading branch information
tovrstra authored May 7, 2024
2 parents 6b9c6e0 + b462485 commit 538d58d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ celerybeat-schedule
# dotenv
.env

# direnv
.envrc

# virtualenv
.venv
venv/
Expand Down
3 changes: 1 addition & 2 deletions iodata/formats/extxyz.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@
"""

from distutils.util import strtobool
import shlex
from typing import Iterator

import numpy as np

from ..docstrings import document_load_one, document_load_many
from ..periodic import sym2num, num2sym
from ..utils import angstrom, amu, LineIterator
from ..utils import angstrom, amu, LineIterator, strtobool

from .xyz import load_one as load_one_xyz

Expand Down
3 changes: 1 addition & 2 deletions iodata/formats/qchemlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@
"""

from typing import Tuple
from distutils.util import strtobool

import numpy as np

from ..docstrings import document_load_one
from ..orbitals import MolecularOrbitals
from ..periodic import sym2num
from ..utils import LineIterator, angstrom, kcalmol, calmol, kjmol
from ..utils import LineIterator, angstrom, kcalmol, calmol, kjmol, strtobool

__all__ = []

Expand Down
11 changes: 10 additions & 1 deletion iodata/test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,18 @@
# --
"""Unit tests for iodata.utils."""

import pytest

from ..utils import amu
from ..utils import amu, strtobool


def test_amu():
assert abs(amu * 1.008 - 1837.47) < 1e-1


def test_strtobool():
assert strtobool("T") is True
assert strtobool("false") is False
assert strtobool("y") is True
with pytest.raises(ValueError):
strtobool("whatever")
26 changes: 25 additions & 1 deletion iodata/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@


__all__ = ['LineIterator', 'Cube', 'set_four_index_element', 'volume',
'derive_naturals', 'check_dm']
'derive_naturals', 'check_dm', 'strtobool']


# The unit conversion factors below can be used as follows:
Expand Down Expand Up @@ -263,3 +263,27 @@ def check_dm(dm: np.ndarray, overlap: np.ndarray, eps: float = 1e-4, occ_max: fl
if occupations.max() > occ_max + eps:
raise ValueError('The density matrix has eigenvalues considerably larger than '
'max. error=%e' % (occupations.max() - 1))


STRTOBOOL = {
'y': True,
'yes': True,
't': True,
'true': True,
'on': True,
'1': True,
'n': False,
'no': False,
'f': False,
'false': False,
'off': False,
'0': False
}


def strtobool(value: str) -> bool:
"""Interpret string as a boolean."""
result = STRTOBOOL.get(value.lower())
if result is None:
raise ValueError(f"'{value}' cannot be converted to boolean")
return result

0 comments on commit 538d58d

Please sign in to comment.