Skip to content

Commit

Permalink
update: rename and return the object
Browse files Browse the repository at this point in the history
  • Loading branch information
VsevolodX committed May 7, 2024
1 parent f1ebb87 commit b38fa97
Show file tree
Hide file tree
Showing 2 changed files with 1,219 additions and 67 deletions.
1,262 changes: 1,195 additions & 67 deletions other/materials_designer/create_interface_with_min_strain_zsl.ipynb

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions utils/io.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from typing import Any, List, Optional


def user_select_object_by_index(
objects: List[Any], object_name: str = "object", prompt_head: Optional[str] = None
) -> Any:
prompt_head = prompt_head or f"Select {object_name} by index:\n"
prompt_body = "\n".join(f"{i}: {t}" for i, t in enumerate(objects))

selected_index_str = input(prompt_head + prompt_body)
try:
selected_index = int(selected_index_str)
except ValueError:
print("Invalid input. Please enter a valid integer.")
return user_select_object_by_index(objects, object_name, prompt_head)

assert isinstance(selected_index, int)

if selected_index < 0 or selected_index >= len(objects):
print("Invalid index.")
return user_select_object_by_index(objects, object_name, prompt_head)

print(f"Selected {object_name}: ", objects[selected_index])
return objects[selected_index]

0 comments on commit b38fa97

Please sign in to comment.