Skip to content

Commit

Permalink
try fix Lobsterin.from_file on 3.12
Browse files Browse the repository at this point in the history
  • Loading branch information
janosh committed Apr 27, 2024
1 parent 56c2e95 commit 0990f1f
Showing 1 changed file with 22 additions and 23 deletions.
45 changes: 22 additions & 23 deletions pymatgen/io/lobster/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,31 +566,30 @@ def from_file(cls, lobsterin: str) -> Self:
lobsterin_dict: dict[str, Any] = {}

for datum in data:
# Remove all comments
if not datum.startswith(("!", "#", "//")):
pattern = r"\b[^!#//]+" # exclude comments after commands
if matched_pattern := re.findall(pattern, datum):
raw_datum = matched_pattern[0].replace("\t", " ") # handle tab in between and end of command
key_word = raw_datum.strip().split(" ") # extract keyword
key = key_word[0].lower()
if len(key_word) > 1:
# check which type of keyword this is, handle accordingly
if key not in [datum2.lower() for datum2 in Lobsterin.LISTKEYWORDS]:
if key not in [datum2.lower() for datum2 in Lobsterin.FLOAT_KEYWORDS]:
if key not in lobsterin_dict:
lobsterin_dict[key] = " ".join(key_word[1:])
else:
raise ValueError(f"Same keyword {key} twice!")
elif key not in lobsterin_dict:
lobsterin_dict[key] = float(key_word[1])
else:
if datum.startswith(("!", "#", "//")):
continue # ignore comments
pattern = r"\b[^!#//]+" # exclude comments after commands
if matched_pattern := re.findall(pattern, datum):
raw_datum = matched_pattern[0].replace("\t", " ") # handle tab in between and end of command
key_word = raw_datum.strip().split(" ") # extract keyword
key = key_word[0].lower()
if len(key_word) > 1:
# check which type of keyword this is, handle accordingly
if key not in [datum2.lower() for datum2 in Lobsterin.LISTKEYWORDS]:
if key not in [datum2.lower() for datum2 in Lobsterin.FLOAT_KEYWORDS]:
if key in lobsterin_dict:
raise ValueError(f"Same keyword {key} twice!")
elif key not in lobsterin_dict:
lobsterin_dict[key] = [" ".join(key_word[1:])]
lobsterin_dict[key] = " ".join(key_word[1:])
elif key in lobsterin_dict:
raise ValueError(f"Same keyword {key} twice!")
else:
lobsterin_dict[key].append(" ".join(key_word[1:]))
elif len(key_word) > 0:
lobsterin_dict[key] = True
lobsterin_dict[key] = float("nan" if key_word[1].strip() == "None" else key_word[1])
elif key not in lobsterin_dict:
lobsterin_dict[key] = [" ".join(key_word[1:])]
else:
lobsterin_dict[key].append(" ".join(key_word[1:]))
elif len(key_word) > 0:
lobsterin_dict[key] = True

return cls(lobsterin_dict)

Expand Down

0 comments on commit 0990f1f

Please sign in to comment.