Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Python 3.12 compatibility #310

Merged
merged 1 commit into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading