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

Speed up Summarize test #19

Merged
merged 2 commits into from
Oct 21, 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
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
.env
.venv
ibm_granite_community.egg-info/
ibm_granite_community/__pycache__/
build/
.DS_Store
.DS_Store
__pycache__
13 changes: 9 additions & 4 deletions ibm_granite_community/notebook_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def is_colab():
return False

# Function to get the API key
def get_env_var(var_name):
def get_env_var(var_name, default_value=None):
env_var = None

if os.environ.get(var_name) is not None:
Expand Down Expand Up @@ -39,7 +39,12 @@ def get_env_var(var_name):
print(f"{var_name} not found in .env file.")

if not env_var:
# If neither Colab nor .env file, prompt the user for the API key
# If we can't find a value in the env, use the default value if provided.
if default_value is not None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a lot of code that could just be handled in one line:

env_var = os.environ.get(var_name, default_value)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fayvor Since this is blocking tests passing, let's get this in, and come back to @rawkintrevo's point

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rawkintrevo I'm not sure what you are suggesting, sorry. That line doesn't have the same outcome.

env_var = default_value

if not env_var:
# If neither Colab nor .env file nor default, prompt the user for the API key
from getpass import getpass
env_var = getpass(f"Please enter your {var_name}: ")

Expand All @@ -51,6 +56,6 @@ def get_env_var(var_name):

return env_var

def set_env_var(var_name):
def set_env_var(var_name, default_value=None):
if os.environ.get(var_name) is None:
os.environ[var_name] = get_env_var(var_name)
os.environ[var_name] = get_env_var(var_name, default_value=default_value)
10 changes: 9 additions & 1 deletion test/test_notebook_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,12 @@ def test_env_var_dotenv(monkeypatch):
def set_api_key():
os.environ["TEST_API_KEY"] = "xyz123"
monkeypatch.setattr(dotenv, "load_dotenv", set_api_key)
assert get_env_var("TEST_API_KEY") == "xyz123"
assert get_env_var("TEST_API_KEY") == "xyz123"

# Test fallback to default environment variable
def test_env_var_default():
assert get_env_var("FAVORITE_COLOR", "blue") == "blue"

def test_set_env_var_default():
set_env_var("FAVORITE_COLOR", "blue")
assert get_env_var("FAVORITE_COLOR") == "blue"
Loading