Skip to content

Commit

Permalink
sourcing changes made in feature-jdftx-inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
benrich37 committed Nov 21, 2024
1 parent 0719540 commit cfd9041
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
32 changes: 29 additions & 3 deletions src/pymatgen/io/jdftx/generic_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

import numpy as np

from pymatgen.io.jdftx._input_utils import flatten_list

__author__ = "Jacob Clary, Ben Rich"


Expand Down Expand Up @@ -833,7 +831,7 @@ def _make_str_for_dict(self, tag: str, value_list: list) -> str:
Returns:
str: The value converted to a string representation for dict representation.
"""
value = flatten_list(tag, value_list)
value = _flatten_list(tag, value_list)
self._check_for_mixed_nesting(tag, value)
return " ".join([str(x) for x in value])

Expand Down Expand Up @@ -1102,3 +1100,31 @@ def read(self, tag: str, value_str: str) -> dict:
f"Something is wrong in the JDFTXInfile formatting, some values were not processed: {value}"
)
return subdict


def _flatten_list(tag: str, list_of_lists: list[Any]) -> list[Any]:
"""Flatten list of lists into a single list, then stop.
Flatten list of lists into a single list, then stop.
Parameters
----------
tag : str
The tag to flatten the list of lists for.
list_of_lists : list[Any]
The list of lists to flatten.
Returns
-------
list[Any]
The flattened list.
"""
if not isinstance(list_of_lists, list):
raise TypeError(f"{tag}: You must provide a list to flatten_list()!")
flist = []
for v in list_of_lists:
if isinstance(v, list):
flist.extend(_flatten_list(tag, v))
else:
flist.append(v)
return flist
12 changes: 6 additions & 6 deletions tests/io/jdftx/test_input_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import pytest

from pymatgen.io.jdftx._input_utils import flatten_list
from pymatgen.io.jdftx.generic_tags import _flatten_list


def test_flatten_list():
assert flatten_list("", [1, 2, 3]) == [1, 2, 3]
assert flatten_list("", [1, [2, 3]]) == [1, 2, 3]
assert flatten_list("", [1, [2, [3]]]) == [1, 2, 3]
assert flatten_list("", [1, [2, [3, [4]]]]) == [1, 2, 3, 4]
assert _flatten_list("", [1, 2, 3]) == [1, 2, 3]
assert _flatten_list("", [1, [2, 3]]) == [1, 2, 3]
assert _flatten_list("", [1, [2, [3]]]) == [1, 2, 3]
assert _flatten_list("", [1, [2, [3, [4]]]]) == [1, 2, 3, 4]
with pytest.raises(TypeError):
flatten_list("", 1)
_flatten_list("", 1)

0 comments on commit cfd9041

Please sign in to comment.