-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update: rename and return the object
- Loading branch information
Showing
2 changed files
with
1,219 additions
and
67 deletions.
There are no files selected for viewing
1,262 changes: 1,195 additions & 67 deletions
1,262
other/materials_designer/create_interface_with_min_strain_zsl.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |