Skip to content

Commit

Permalink
address a self-review comment
Browse files Browse the repository at this point in the history
  • Loading branch information
gargnitingoogle committed Oct 15, 2024
1 parent bd1e733 commit 7c86625
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 41 deletions.
72 changes: 36 additions & 36 deletions perfmetrics/scripts/testing_on_gke/examples/utils/gsheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,14 @@ def append_data_to_gsheet(
) -> None:
"""Calls the API to append the given data at the end of the given worksheet in the given gsheet.
If the passed header matches the first row of the file, then the
header is not inserted again.
Args:
serviceAccountKeyFile: Path of a service-account key-file for authentication
read/write from/to the given gsheet. This can be a local filepath or a GCS
path starting with `gs:`
worksheet: string, name of the worksheet to be edited appended by a "!"
data: Dictionary of {'header': tuple, 'values': list(tuples)}, to be added
to the worksheet.
to the worksheet. If the passed header matches the first row of the
worksheet, then the header row is not inserted again.
gsheet_id: Unique ID to identify a gsheet.
Raises:
Expand All @@ -92,41 +91,41 @@ def _using_local_service_key_file(localServiceAccountKeyFile: str):
# Open a read-write gsheet client.
client = _get_sheets_service_client(localServiceAccountKeyFile)

def _read_from_range(cell_range: str):
"""Returns a list of list of values for the given range in the worksheet."""
gsheet_response = (
client.spreadsheets()
.values()
.get(spreadsheetId=gsheet_id, range=f'{worksheet}!{cell_range}')
.execute()
)
return gsheet_response['values'] if 'values' in gsheet_response else []

def _write_at_address(cell_address: str, data):
"""Writes a list of tuple of values at the given cell_cell_address in the worksheet."""
client.spreadsheets().values().update(
spreadsheetId=gsheet_id,
valueInputOption='USER_ENTERED',
body={'majorDimension': 'ROWS', 'values': data},
range=f'{worksheet}!{cell_address}',
).execute()

data_in_first_column = _read_from_range('A1:A')
num_rows = len(data_in_first_column)
data_in_first_row = _read_from_range('A1:1')
original_header = tuple(data_in_first_row[0]) if data_in_first_row else ()
new_header = data['header']

# Insert header in the file, if needed.
if not original_header or not original_header == new_header:
# Append header after last row.
_write_at_address(f'A{num_rows+1}', [new_header])
num_rows = num_rows + 1

# Append given values after the last row.
_write_at_address(f'A{num_rows+1}', data['values'])
def _read_from_range(cell_range: str):
"""Returns a list of list of values for the given range in the worksheet."""
gsheet_response = (
client.spreadsheets()
.values()
.get(spreadsheetId=gsheet_id, range=f'{worksheet}!{cell_range}')
.execute()
)
return gsheet_response['values'] if 'values' in gsheet_response else []

def _write_at_address(cell_address: str, data):
"""Writes a list of tuple of values at the given cell_cell_address in the worksheet."""
client.spreadsheets().values().update(
spreadsheetId=gsheet_id,
valueInputOption='USER_ENTERED',
body={'majorDimension': 'ROWS', 'values': data},
range=f'{worksheet}!{cell_address}',
).execute()

data_in_first_column = _read_from_range('A1:A')
num_rows = len(data_in_first_column)
data_in_first_row = _read_from_range('A1:1')
original_header = tuple(data_in_first_row[0]) if data_in_first_row else ()
new_header = data['header']

# Insert header in the file, if needed.
if not original_header or not original_header == new_header:
# Append header after last row.
_write_at_address(f'A{num_rows+1}', [new_header])
num_rows = num_rows + 1

# Append given values after the last row.
_write_at_address(f'A{num_rows+1}', data['values'])
num_rows = num_rows + 1

if serviceAccountKeyFile.startswith('gs://'):
localServiceAccountKeyFile = download_gcs_object_locally(
serviceAccountKeyFile
Expand All @@ -138,4 +137,5 @@ def _write_at_address(cell_address: str, data):


def url(gsheet_id: str) -> str:
"""Returns the url corresponding to the given google sheet ID."""
return f'https://docs.google.com/spreadsheets/d/{gsheet_id}'
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@

class GsheetTest(unittest.TestCase):

# @classmethod
# def setUpClass(self):
# self.project_id = 'gcs-fuse-test'

def test_append_data_to_gsheet(self):
_DEFAULT_GSHEET_ID = '1s9DCis6XZ_oHRIFTy0F8yVN93EGA2Koks_pzpCqAIS4'

Expand Down
2 changes: 1 addition & 1 deletion perfmetrics/scripts/testing_on_gke/examples/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ def run_command(command: str) -> int:
Returns exit-code which would be non-zero for error.
"""
result = subprocess.run(
[word for word in command.split(" ") if (word and not str.isspace(word))],
[word for word in command.split(" ") if (word and word.strip())],
capture_output=True,
text=True,
)
Expand Down

0 comments on commit 7c86625

Please sign in to comment.