Skip to content

Commit

Permalink
chore(docs): include imports in snippets (#1027)
Browse files Browse the repository at this point in the history
* chore(docs): added imports into snippets

* simplify print regions
  • Loading branch information
daniel-sanche authored Oct 25, 2024
1 parent a9500b8 commit 18650e7
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 55 deletions.
37 changes: 24 additions & 13 deletions samples/snippets/deletes/deletes_snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@
# limitations under the License.


from google.cloud import bigtable

# Write your code here.


# [START bigtable_delete_from_column]
def delete_from_column(project_id, instance_id, table_id):
client = bigtable.Client(project=project_id, admin=True)
from google.cloud.bigtable import Client

client = Client(project=project_id, admin=True)
instance = client.instance(instance_id)
table = instance.table(table_id)
row = table.row("phone#4c410523#20190501")
Expand All @@ -33,7 +30,9 @@ def delete_from_column(project_id, instance_id, table_id):

# [START bigtable_delete_from_column_family]
def delete_from_column_family(project_id, instance_id, table_id):
client = bigtable.Client(project=project_id, admin=True)
from google.cloud.bigtable import Client

client = Client(project=project_id, admin=True)
instance = client.instance(instance_id)
table = instance.table(table_id)
row = table.row("phone#4c410523#20190501")
Expand All @@ -46,7 +45,9 @@ def delete_from_column_family(project_id, instance_id, table_id):

# [START bigtable_delete_from_row]
def delete_from_row(project_id, instance_id, table_id):
client = bigtable.Client(project=project_id, admin=True)
from google.cloud.bigtable import Client

client = Client(project=project_id, admin=True)
instance = client.instance(instance_id)
table = instance.table(table_id)
row = table.row("phone#4c410523#20190501")
Expand All @@ -58,7 +59,9 @@ def delete_from_row(project_id, instance_id, table_id):

# [START bigtable_streaming_and_batching]
def streaming_and_batching(project_id, instance_id, table_id):
client = bigtable.Client(project=project_id, admin=True)
from google.cloud.bigtable import Client

client = Client(project=project_id, admin=True)
instance = client.instance(instance_id)
table = instance.table(table_id)
batcher = table.mutations_batcher(flush_count=2)
Expand All @@ -74,7 +77,9 @@ def streaming_and_batching(project_id, instance_id, table_id):

# [START bigtable_check_and_mutate]
def check_and_mutate(project_id, instance_id, table_id):
client = bigtable.Client(project=project_id, admin=True)
from google.cloud.bigtable import Client

client = Client(project=project_id, admin=True)
instance = client.instance(instance_id)
table = instance.table(table_id)
row = table.row("phone#4c410523#20190501")
Expand All @@ -88,7 +93,9 @@ def check_and_mutate(project_id, instance_id, table_id):

# [START bigtable_drop_row_range]
def drop_row_range(project_id, instance_id, table_id):
client = bigtable.Client(project=project_id, admin=True)
from google.cloud.bigtable import Client

client = Client(project=project_id, admin=True)
instance = client.instance(instance_id)
table = instance.table(table_id)
row_key_prefix = "phone#4c410523"
Expand All @@ -99,7 +106,9 @@ def drop_row_range(project_id, instance_id, table_id):

# [START bigtable_delete_column_family]
def delete_column_family(project_id, instance_id, table_id):
client = bigtable.Client(project=project_id, admin=True)
from google.cloud.bigtable import Client

client = Client(project=project_id, admin=True)
instance = client.instance(instance_id)
table = instance.table(table_id)
column_family_id = "stats_summary"
Expand All @@ -111,7 +120,9 @@ def delete_column_family(project_id, instance_id, table_id):

# [START bigtable_delete_table]
def delete_table(project_id, instance_id, table_id):
client = bigtable.Client(project=project_id, admin=True)
from google.cloud.bigtable import Client

client = Client(project=project_id, admin=True)
instance = client.instance(instance_id)
table = instance.table(table_id)
table.delete()
Expand Down
34 changes: 19 additions & 15 deletions samples/snippets/deletes/deletes_snippets_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,11 @@
# limitations under the License.


from google.cloud.bigtable.data import (
BigtableDataClientAsync,
DeleteRangeFromColumn,
DeleteAllFromFamily,
DeleteAllFromRow,
RowMutationEntry,
row_filters,
ReadRowsQuery,
)


# Write your code here.


# [START bigtable_delete_from_column_asyncio]
async def delete_from_column(project_id, instance_id, table_id):
from google.cloud.bigtable.data import BigtableDataClientAsync
from google.cloud.bigtable.data import DeleteRangeFromColumn

client = BigtableDataClientAsync(project=project_id)
table = client.get_table(instance_id, table_id)

Expand All @@ -46,6 +35,9 @@ async def delete_from_column(project_id, instance_id, table_id):

# [START bigtable_delete_from_column_family_asyncio]
async def delete_from_column_family(project_id, instance_id, table_id):
from google.cloud.bigtable.data import BigtableDataClientAsync
from google.cloud.bigtable.data import DeleteAllFromFamily

client = BigtableDataClientAsync(project=project_id)
table = client.get_table(instance_id, table_id)

Expand All @@ -60,6 +52,9 @@ async def delete_from_column_family(project_id, instance_id, table_id):

# [START bigtable_delete_from_row_asyncio]
async def delete_from_row(project_id, instance_id, table_id):
from google.cloud.bigtable.data import BigtableDataClientAsync
from google.cloud.bigtable.data import DeleteAllFromRow

client = BigtableDataClientAsync(project=project_id)
table = client.get_table(instance_id, table_id)

Expand All @@ -73,6 +68,11 @@ async def delete_from_row(project_id, instance_id, table_id):

# [START bigtable_streaming_and_batching_asyncio]
async def streaming_and_batching(project_id, instance_id, table_id):
from google.cloud.bigtable.data import BigtableDataClientAsync
from google.cloud.bigtable.data import DeleteRangeFromColumn
from google.cloud.bigtable.data import RowMutationEntry
from google.cloud.bigtable.data import ReadRowsQuery

client = BigtableDataClientAsync(project=project_id)
table = client.get_table(instance_id, table_id)

Expand All @@ -95,12 +95,16 @@ async def streaming_and_batching(project_id, instance_id, table_id):

# [START bigtable_check_and_mutate_asyncio]
async def check_and_mutate(project_id, instance_id, table_id):
from google.cloud.bigtable.data import BigtableDataClientAsync
from google.cloud.bigtable.data import DeleteRangeFromColumn
from google.cloud.bigtable.data.row_filters import LiteralValueFilter

client = BigtableDataClientAsync(project=project_id)
table = client.get_table(instance_id, table_id)

await table.check_and_mutate_row(
"phone#4c410523#20190501",
predicate=row_filters.LiteralValueFilter("PQ2A.190405.003"),
predicate=LiteralValueFilter("PQ2A.190405.003"),
true_case_mutations=DeleteRangeFromColumn(
family="cell_plan", qualifier=b"data_plan_01gb"
),
Expand Down
67 changes: 56 additions & 11 deletions samples/snippets/filters/filter_snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# [START bigtable_filters_print]
import datetime

from google.cloud import bigtable
import google.cloud.bigtable.row_filters as row_filters

# Write your code here.
# [START_EXCLUDE]


# [START bigtable_filters_limit_row_sample]
def filter_limit_row_sample(project_id, instance_id, table_id):
from google.cloud import bigtable
from google.cloud.bigtable import row_filters

client = bigtable.Client(project=project_id, admin=True)
instance = client.instance(instance_id)
table = instance.table(table_id)
Expand All @@ -37,6 +31,9 @@ def filter_limit_row_sample(project_id, instance_id, table_id):
# [END bigtable_filters_limit_row_sample]
# [START bigtable_filters_limit_row_regex]
def filter_limit_row_regex(project_id, instance_id, table_id):
from google.cloud import bigtable
from google.cloud.bigtable import row_filters

client = bigtable.Client(project=project_id, admin=True)
instance = client.instance(instance_id)
table = instance.table(table_id)
Expand All @@ -51,6 +48,9 @@ def filter_limit_row_regex(project_id, instance_id, table_id):
# [END bigtable_filters_limit_row_regex]
# [START bigtable_filters_limit_cells_per_col]
def filter_limit_cells_per_col(project_id, instance_id, table_id):
from google.cloud import bigtable
from google.cloud.bigtable import row_filters

client = bigtable.Client(project=project_id, admin=True)
instance = client.instance(instance_id)
table = instance.table(table_id)
Expand All @@ -63,6 +63,9 @@ def filter_limit_cells_per_col(project_id, instance_id, table_id):
# [END bigtable_filters_limit_cells_per_col]
# [START bigtable_filters_limit_cells_per_row]
def filter_limit_cells_per_row(project_id, instance_id, table_id):
from google.cloud import bigtable
from google.cloud.bigtable import row_filters

client = bigtable.Client(project=project_id, admin=True)
instance = client.instance(instance_id)
table = instance.table(table_id)
Expand All @@ -75,6 +78,9 @@ def filter_limit_cells_per_row(project_id, instance_id, table_id):
# [END bigtable_filters_limit_cells_per_row]
# [START bigtable_filters_limit_cells_per_row_offset]
def filter_limit_cells_per_row_offset(project_id, instance_id, table_id):
from google.cloud import bigtable
from google.cloud.bigtable import row_filters

client = bigtable.Client(project=project_id, admin=True)
instance = client.instance(instance_id)
table = instance.table(table_id)
Expand All @@ -87,6 +93,9 @@ def filter_limit_cells_per_row_offset(project_id, instance_id, table_id):
# [END bigtable_filters_limit_cells_per_row_offset]
# [START bigtable_filters_limit_col_family_regex]
def filter_limit_col_family_regex(project_id, instance_id, table_id):
from google.cloud import bigtable
from google.cloud.bigtable import row_filters

client = bigtable.Client(project=project_id, admin=True)
instance = client.instance(instance_id)
table = instance.table(table_id)
Expand All @@ -101,6 +110,9 @@ def filter_limit_col_family_regex(project_id, instance_id, table_id):
# [END bigtable_filters_limit_col_family_regex]
# [START bigtable_filters_limit_col_qualifier_regex]
def filter_limit_col_qualifier_regex(project_id, instance_id, table_id):
from google.cloud import bigtable
from google.cloud.bigtable import row_filters

client = bigtable.Client(project=project_id, admin=True)
instance = client.instance(instance_id)
table = instance.table(table_id)
Expand All @@ -115,6 +127,9 @@ def filter_limit_col_qualifier_regex(project_id, instance_id, table_id):
# [END bigtable_filters_limit_col_qualifier_regex]
# [START bigtable_filters_limit_col_range]
def filter_limit_col_range(project_id, instance_id, table_id):
from google.cloud import bigtable
from google.cloud.bigtable import row_filters

client = bigtable.Client(project=project_id, admin=True)
instance = client.instance(instance_id)
table = instance.table(table_id)
Expand All @@ -131,6 +146,9 @@ def filter_limit_col_range(project_id, instance_id, table_id):
# [END bigtable_filters_limit_col_range]
# [START bigtable_filters_limit_value_range]
def filter_limit_value_range(project_id, instance_id, table_id):
from google.cloud import bigtable
from google.cloud.bigtable import row_filters

client = bigtable.Client(project=project_id, admin=True)
instance = client.instance(instance_id)
table = instance.table(table_id)
Expand All @@ -148,6 +166,9 @@ def filter_limit_value_range(project_id, instance_id, table_id):


def filter_limit_value_regex(project_id, instance_id, table_id):
from google.cloud import bigtable
from google.cloud.bigtable import row_filters

client = bigtable.Client(project=project_id, admin=True)
instance = client.instance(instance_id)
table = instance.table(table_id)
Expand All @@ -162,6 +183,10 @@ def filter_limit_value_regex(project_id, instance_id, table_id):
# [END bigtable_filters_limit_value_regex]
# [START bigtable_filters_limit_timestamp_range]
def filter_limit_timestamp_range(project_id, instance_id, table_id):
from google.cloud import bigtable
from google.cloud.bigtable import row_filters
import datetime

client = bigtable.Client(project=project_id, admin=True)
instance = client.instance(instance_id)
table = instance.table(table_id)
Expand All @@ -178,6 +203,9 @@ def filter_limit_timestamp_range(project_id, instance_id, table_id):
# [END bigtable_filters_limit_timestamp_range]
# [START bigtable_filters_limit_block_all]
def filter_limit_block_all(project_id, instance_id, table_id):
from google.cloud import bigtable
from google.cloud.bigtable import row_filters

client = bigtable.Client(project=project_id, admin=True)
instance = client.instance(instance_id)
table = instance.table(table_id)
Expand All @@ -190,6 +218,9 @@ def filter_limit_block_all(project_id, instance_id, table_id):
# [END bigtable_filters_limit_block_all]
# [START bigtable_filters_limit_pass_all]
def filter_limit_pass_all(project_id, instance_id, table_id):
from google.cloud import bigtable
from google.cloud.bigtable import row_filters

client = bigtable.Client(project=project_id, admin=True)
instance = client.instance(instance_id)
table = instance.table(table_id)
Expand All @@ -202,6 +233,9 @@ def filter_limit_pass_all(project_id, instance_id, table_id):
# [END bigtable_filters_limit_pass_all]
# [START bigtable_filters_modify_strip_value]
def filter_modify_strip_value(project_id, instance_id, table_id):
from google.cloud import bigtable
from google.cloud.bigtable import row_filters

client = bigtable.Client(project=project_id, admin=True)
instance = client.instance(instance_id)
table = instance.table(table_id)
Expand All @@ -214,6 +248,9 @@ def filter_modify_strip_value(project_id, instance_id, table_id):
# [END bigtable_filters_modify_strip_value]
# [START bigtable_filters_modify_apply_label]
def filter_modify_apply_label(project_id, instance_id, table_id):
from google.cloud import bigtable
from google.cloud.bigtable import row_filters

client = bigtable.Client(project=project_id, admin=True)
instance = client.instance(instance_id)
table = instance.table(table_id)
Expand All @@ -226,6 +263,9 @@ def filter_modify_apply_label(project_id, instance_id, table_id):
# [END bigtable_filters_modify_apply_label]
# [START bigtable_filters_composing_chain]
def filter_composing_chain(project_id, instance_id, table_id):
from google.cloud import bigtable
from google.cloud.bigtable import row_filters

client = bigtable.Client(project=project_id, admin=True)
instance = client.instance(instance_id)
table = instance.table(table_id)
Expand All @@ -245,6 +285,9 @@ def filter_composing_chain(project_id, instance_id, table_id):
# [END bigtable_filters_composing_chain]
# [START bigtable_filters_composing_interleave]
def filter_composing_interleave(project_id, instance_id, table_id):
from google.cloud import bigtable
from google.cloud.bigtable import row_filters

client = bigtable.Client(project=project_id, admin=True)
instance = client.instance(instance_id)
table = instance.table(table_id)
Expand All @@ -264,6 +307,9 @@ def filter_composing_interleave(project_id, instance_id, table_id):
# [END bigtable_filters_composing_interleave]
# [START bigtable_filters_composing_condition]
def filter_composing_condition(project_id, instance_id, table_id):
from google.cloud import bigtable
from google.cloud.bigtable import row_filters

client = bigtable.Client(project=project_id, admin=True)
instance = client.instance(instance_id)
table = instance.table(table_id)
Expand All @@ -285,9 +331,8 @@ def filter_composing_condition(project_id, instance_id, table_id):


# [END bigtable_filters_composing_condition]
# [END_EXCLUDE]


# [START bigtable_filters_print]
def print_row(row):
print("Reading data for {}:".format(row.row_key.decode("utf-8")))
for cf, cols in sorted(row.cells.items()):
Expand Down
8 changes: 3 additions & 5 deletions samples/snippets/filters/filter_snippets_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from google.cloud._helpers import _datetime_from_microseconds
from google.cloud.bigtable.data import Row


# [START bigtable_filters_limit_row_sample_asyncio]
async def filter_limit_row_sample(project_id, instance_id, table_id):
Expand Down Expand Up @@ -368,10 +365,11 @@ async def filter_composing_condition(project_id, instance_id, table_id):


# [END bigtable_filters_composing_condition_asyncio]
# [END_EXCLUDE]


def print_row(row: Row):
def print_row(row):
from google.cloud._helpers import _datetime_from_microseconds

print("Reading data for {}:".format(row.row_key.decode("utf-8")))
last_family = None
for cell in row.cells:
Expand Down
Loading

0 comments on commit 18650e7

Please sign in to comment.