Files
model_garden/.cloud-build/UpdateNotebookVariables.py
T
Morgan DuandGitHub 64a778c5e7 Copy Cloudbuild and GithubActions setup from ai-platform-samples for notebook testing (#1)
* copy github actions

* copy Cloud Build

* add Dockerfile for cloud build

* add notebook test_folders.txt to .cloud-build and update the file path
2021-07-20 10:37:18 -07:00

66 lines
1.8 KiB
Python

import re
"""
This script is used to update variables in the notebook via regex
It requires variables to be defined in particular format
For example, if your variable was PROJECT_ID, use:
PROJECT_ID = "[your_project_here]"
Single-quotes also work:
PROJECT_ID = '[your_project_here]'
Variables in conditionals can also be replaced:
PROJECT_ID == "[your_project_here]"
"""
def get_updated_value(content: str, variable_name: str, variable_value: str) -> str:
return re.sub(
rf"({variable_name}.*?=.*?[\",\'])\[.+?\]([\",\'].*?)",
rf"\1{variable_value}\2",
content,
flags=re.M,
)
def test_update_value():
new_content = get_updated_value(
content='asdf\nPROJECT_ID = "[your-project-id]" #@param {type:"string"} \nasdf',
variable_name="PROJECT_ID",
variable_value="sample-project",
)
assert (
new_content
== 'asdf\nPROJECT_ID = "sample-project" #@param {type:"string"} \nasdf'
)
def test_update_value_single_quotes():
new_content = get_updated_value(
content="PROJECT_ID = '[your-project-id]'",
variable_name="PROJECT_ID",
variable_value="sample-project",
)
assert new_content == "PROJECT_ID = 'sample-project'"
def test_update_value_avoidance():
new_content = get_updated_value(
content="PROJECT_ID = shell_output[0] ",
variable_name="PROJECT_ID",
variable_value="sample-project",
)
assert new_content == "PROJECT_ID = shell_output[0] "
def test_region():
new_content = get_updated_value(
content='REGION = "[your-region]" # @param {type:"string"}',
variable_name="REGION",
variable_value="us-central1",
)
assert new_content == 'REGION = "us-central1" # @param {type:"string"}'