Skip to content

Commit

Permalink
docs: add python sample runner
Browse files Browse the repository at this point in the history
  • Loading branch information
olavloite committed May 27, 2024
1 parent 4155833 commit 80a1a36
Show file tree
Hide file tree
Showing 17 changed files with 114 additions and 0 deletions.
5 changes: 5 additions & 0 deletions samples/snippets/python-snippets/samples/add_column.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@ def add_column(host: string, port: int, database: string):
"ADD COLUMN marketing_budget bigint")
print("Added marketing_budget column")
# [END spanner_add_column]

import sample_runner

sample_runner.parse_arguments()
add_column(sample_runner.host, sample_runner.port, sample_runner.database)
5 changes: 5 additions & 0 deletions samples/snippets/python-snippets/samples/create_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,8 @@ def create_connection(host: string, port: int, database: string):
cur.execute("select 'Hello world!' as hello")
print("Greeting from Cloud Spanner PostgreSQL:", cur.fetchone()[0])
# [END spanner_create_connection]

import sample_runner

sample_runner.parse_arguments()
create_connection(sample_runner.host, sample_runner.port, sample_runner.database)
5 changes: 5 additions & 0 deletions samples/snippets/python-snippets/samples/create_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,8 @@ def create_tables(host: string, port: int, database: string):
print("Created Singers & Albums tables in database: [{database}]"
.format(database=database))
# [END spanner_create_database]

import sample_runner

sample_runner.parse_arguments()
create_tables(sample_runner.host, sample_runner.port, sample_runner.database)
5 changes: 5 additions & 0 deletions samples/snippets/python-snippets/samples/data_boost.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,8 @@ def data_boost(host: string, port: int, database: string):
for singer in cur:
print(singer)
# [END spanner_data_boost]

import sample_runner

sample_runner.parse_arguments()
data_boost(sample_runner.host, sample_runner.port, sample_runner.database)
5 changes: 5 additions & 0 deletions samples/snippets/python-snippets/samples/ddl_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,8 @@ def ddl_batch(host: string, port: int, database: string):
")")
print("Added venues and concerts tables")
# [END spanner_ddl_batch]

import sample_runner

sample_runner.parse_arguments()
ddl_batch(sample_runner.host, sample_runner.port, sample_runner.database)
5 changes: 5 additions & 0 deletions samples/snippets/python-snippets/samples/partitioned_dml.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,8 @@ def execute_partitioned_dml(host: string, port: int, database: string):
print("Updated at least %d albums" % cur.rowcount)

# [END spanner_partitioned_dml]

import sample_runner

sample_runner.parse_arguments()
execute_partitioned_dml(sample_runner.host, sample_runner.port, sample_runner.database)
5 changes: 5 additions & 0 deletions samples/snippets/python-snippets/samples/query_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@ def query_data(host: string, port: int, database: string):
for album in cur:
print(album)
# [END spanner_query_data]

import sample_runner

sample_runner.parse_arguments()
query_data(sample_runner.host, sample_runner.port, sample_runner.database)
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,8 @@ def query_data_with_parameter(host: string, port: int, database: string):
for singer in cur:
print(singer)
# [END spanner_query_with_parameter]

import sample_runner

sample_runner.parse_arguments()
query_data_with_parameter(sample_runner.host, sample_runner.port, sample_runner.database)
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,8 @@ def read_only_transaction(host: string, port: int, database: string):
# rolling back or committing a read-only transaction.
conn.commit()
# [END spanner_read_only_transaction]

import sample_runner

sample_runner.parse_arguments()
read_only_transaction(sample_runner.host, sample_runner.port, sample_runner.database)
34 changes: 34 additions & 0 deletions samples/snippets/python-snippets/samples/sample_runner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright 2024 Google LLC All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys

global host, port, database

def parse_arguments():
global host, port, database
if len(sys.argv) < 2 or len(sys.argv) > 3:
print("Usage: python <sample-name>.py <database-name> [host:port]")
exit(1)

database = sys.argv[1]
host = "localhost"
port = 5432
if len(sys.argv) == 3:
host_and_port = sys.argv[2]
if not ":" in host_and_port:
print("Invalid host:port argument: " + host_and_port)
exit(1)
host = host_and_port.split(":", 1)[0]
port = int(host_and_port.split(":", 1)[1])
5 changes: 5 additions & 0 deletions samples/snippets/python-snippets/samples/statement_timeout.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,8 @@ def query_with_timeout(host: string, port: int, database: string):
except DatabaseError as exception:
print("Error occurred during query execution: %s" % exception)
# [END spanner_statement_timeout]

import sample_runner

sample_runner.parse_arguments()
query_with_timeout(sample_runner.host, sample_runner.port, sample_runner.database)
5 changes: 5 additions & 0 deletions samples/snippets/python-snippets/samples/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,8 @@ def tags(host: string, port: int, database: string):
conn.commit()
print("Reduced marketing budget")
# [END spanner_transaction_and_statement_tag]

import sample_runner

sample_runner.parse_arguments()
tags(sample_runner.host, sample_runner.port, sample_runner.database)
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,8 @@ def update_data_with_copy(host: string, port: int, database: string):
copy.write_row((2, 2, 500000))
print("Updated %d albums" % cur.rowcount)
# [END spanner_update_data]

import sample_runner

sample_runner.parse_arguments()
update_data_with_copy(sample_runner.host, sample_runner.port, sample_runner.database)
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,8 @@ def update_data_with_transaction(host: string, port: int, database: string):
conn.commit()
print("Transferred marketing budget from Album 2 to Album 1")
# [END spanner_dml_getting_started_update]

import sample_runner

sample_runner.parse_arguments()
update_data_with_transaction(sample_runner.host, sample_runner.port, sample_runner.database)
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,8 @@ def write_data_with_copy(host: string, port: int, database: string):
copy.write(data)
print("Copied %d albums" % cur.rowcount)
# [END spanner_copy_from_stdin]

import sample_runner

sample_runner.parse_arguments()
write_data_with_copy(sample_runner.host, sample_runner.port, sample_runner.database)
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,8 @@ def write_data_with_dml(host: string, port: int, database: string):
15, "Dylan", "Shaw",))
print("%d records inserted" % cur.rowcount)
# [END spanner_dml_getting_started_insert]

import sample_runner

sample_runner.parse_arguments()
write_data_with_dml(sample_runner.host, sample_runner.port, sample_runner.database)
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,8 @@ def write_data_with_dml_batch(host: string, port: int, database: string):
(18, "Maya", "Patel",), ])
print("%d records inserted" % cur.rowcount)
# [END spanner_dml_batch]

import sample_runner

sample_runner.parse_arguments()
write_data_with_dml_batch(sample_runner.host, sample_runner.port, sample_runner.database)

0 comments on commit 80a1a36

Please sign in to comment.