Skip to content

Commit c70eec7

Browse files
chore(docs): include imports in snippets (#1027)
* chore(docs): added imports into snippets * simplify print regions
1 parent d4d990d commit c70eec7

File tree

5 files changed

+123
-55
lines changed

5 files changed

+123
-55
lines changed

packages/google-cloud-bigtable/samples/snippets/deletes/deletes_snippets.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,11 @@
1414
# limitations under the License.
1515

1616

17-
from google.cloud import bigtable
18-
19-
# Write your code here.
20-
21-
2217
# [START bigtable_delete_from_column]
2318
def delete_from_column(project_id, instance_id, table_id):
24-
client = bigtable.Client(project=project_id, admin=True)
19+
from google.cloud.bigtable import Client
20+
21+
client = Client(project=project_id, admin=True)
2522
instance = client.instance(instance_id)
2623
table = instance.table(table_id)
2724
row = table.row("phone#4c410523#20190501")
@@ -33,7 +30,9 @@ def delete_from_column(project_id, instance_id, table_id):
3330

3431
# [START bigtable_delete_from_column_family]
3532
def delete_from_column_family(project_id, instance_id, table_id):
36-
client = bigtable.Client(project=project_id, admin=True)
33+
from google.cloud.bigtable import Client
34+
35+
client = Client(project=project_id, admin=True)
3736
instance = client.instance(instance_id)
3837
table = instance.table(table_id)
3938
row = table.row("phone#4c410523#20190501")
@@ -46,7 +45,9 @@ def delete_from_column_family(project_id, instance_id, table_id):
4645

4746
# [START bigtable_delete_from_row]
4847
def delete_from_row(project_id, instance_id, table_id):
49-
client = bigtable.Client(project=project_id, admin=True)
48+
from google.cloud.bigtable import Client
49+
50+
client = Client(project=project_id, admin=True)
5051
instance = client.instance(instance_id)
5152
table = instance.table(table_id)
5253
row = table.row("phone#4c410523#20190501")
@@ -58,7 +59,9 @@ def delete_from_row(project_id, instance_id, table_id):
5859

5960
# [START bigtable_streaming_and_batching]
6061
def streaming_and_batching(project_id, instance_id, table_id):
61-
client = bigtable.Client(project=project_id, admin=True)
62+
from google.cloud.bigtable import Client
63+
64+
client = Client(project=project_id, admin=True)
6265
instance = client.instance(instance_id)
6366
table = instance.table(table_id)
6467
batcher = table.mutations_batcher(flush_count=2)
@@ -74,7 +77,9 @@ def streaming_and_batching(project_id, instance_id, table_id):
7477

7578
# [START bigtable_check_and_mutate]
7679
def check_and_mutate(project_id, instance_id, table_id):
77-
client = bigtable.Client(project=project_id, admin=True)
80+
from google.cloud.bigtable import Client
81+
82+
client = Client(project=project_id, admin=True)
7883
instance = client.instance(instance_id)
7984
table = instance.table(table_id)
8085
row = table.row("phone#4c410523#20190501")
@@ -88,7 +93,9 @@ def check_and_mutate(project_id, instance_id, table_id):
8893

8994
# [START bigtable_drop_row_range]
9095
def drop_row_range(project_id, instance_id, table_id):
91-
client = bigtable.Client(project=project_id, admin=True)
96+
from google.cloud.bigtable import Client
97+
98+
client = Client(project=project_id, admin=True)
9299
instance = client.instance(instance_id)
93100
table = instance.table(table_id)
94101
row_key_prefix = "phone#4c410523"
@@ -99,7 +106,9 @@ def drop_row_range(project_id, instance_id, table_id):
99106

100107
# [START bigtable_delete_column_family]
101108
def delete_column_family(project_id, instance_id, table_id):
102-
client = bigtable.Client(project=project_id, admin=True)
109+
from google.cloud.bigtable import Client
110+
111+
client = Client(project=project_id, admin=True)
103112
instance = client.instance(instance_id)
104113
table = instance.table(table_id)
105114
column_family_id = "stats_summary"
@@ -111,7 +120,9 @@ def delete_column_family(project_id, instance_id, table_id):
111120

112121
# [START bigtable_delete_table]
113122
def delete_table(project_id, instance_id, table_id):
114-
client = bigtable.Client(project=project_id, admin=True)
123+
from google.cloud.bigtable import Client
124+
125+
client = Client(project=project_id, admin=True)
115126
instance = client.instance(instance_id)
116127
table = instance.table(table_id)
117128
table.delete()

packages/google-cloud-bigtable/samples/snippets/deletes/deletes_snippets_async.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,11 @@
1414
# limitations under the License.
1515

1616

17-
from google.cloud.bigtable.data import (
18-
BigtableDataClientAsync,
19-
DeleteRangeFromColumn,
20-
DeleteAllFromFamily,
21-
DeleteAllFromRow,
22-
RowMutationEntry,
23-
row_filters,
24-
ReadRowsQuery,
25-
)
26-
27-
28-
# Write your code here.
29-
30-
3117
# [START bigtable_delete_from_column_asyncio]
3218
async def delete_from_column(project_id, instance_id, table_id):
19+
from google.cloud.bigtable.data import BigtableDataClientAsync
20+
from google.cloud.bigtable.data import DeleteRangeFromColumn
21+
3322
client = BigtableDataClientAsync(project=project_id)
3423
table = client.get_table(instance_id, table_id)
3524

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

4736
# [START bigtable_delete_from_column_family_asyncio]
4837
async def delete_from_column_family(project_id, instance_id, table_id):
38+
from google.cloud.bigtable.data import BigtableDataClientAsync
39+
from google.cloud.bigtable.data import DeleteAllFromFamily
40+
4941
client = BigtableDataClientAsync(project=project_id)
5042
table = client.get_table(instance_id, table_id)
5143

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

6153
# [START bigtable_delete_from_row_asyncio]
6254
async def delete_from_row(project_id, instance_id, table_id):
55+
from google.cloud.bigtable.data import BigtableDataClientAsync
56+
from google.cloud.bigtable.data import DeleteAllFromRow
57+
6358
client = BigtableDataClientAsync(project=project_id)
6459
table = client.get_table(instance_id, table_id)
6560

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

7469
# [START bigtable_streaming_and_batching_asyncio]
7570
async def streaming_and_batching(project_id, instance_id, table_id):
71+
from google.cloud.bigtable.data import BigtableDataClientAsync
72+
from google.cloud.bigtable.data import DeleteRangeFromColumn
73+
from google.cloud.bigtable.data import RowMutationEntry
74+
from google.cloud.bigtable.data import ReadRowsQuery
75+
7676
client = BigtableDataClientAsync(project=project_id)
7777
table = client.get_table(instance_id, table_id)
7878

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

9696
# [START bigtable_check_and_mutate_asyncio]
9797
async def check_and_mutate(project_id, instance_id, table_id):
98+
from google.cloud.bigtable.data import BigtableDataClientAsync
99+
from google.cloud.bigtable.data import DeleteRangeFromColumn
100+
from google.cloud.bigtable.data.row_filters import LiteralValueFilter
101+
98102
client = BigtableDataClientAsync(project=project_id)
99103
table = client.get_table(instance_id, table_id)
100104

101105
await table.check_and_mutate_row(
102106
"phone#4c410523#20190501",
103-
predicate=row_filters.LiteralValueFilter("PQ2A.190405.003"),
107+
predicate=LiteralValueFilter("PQ2A.190405.003"),
104108
true_case_mutations=DeleteRangeFromColumn(
105109
family="cell_plan", qualifier=b"data_plan_01gb"
106110
),

packages/google-cloud-bigtable/samples/snippets/filters/filter_snippets.py

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,12 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16-
# [START bigtable_filters_print]
17-
import datetime
18-
19-
from google.cloud import bigtable
20-
import google.cloud.bigtable.row_filters as row_filters
21-
22-
# Write your code here.
23-
# [START_EXCLUDE]
24-
2516

2617
# [START bigtable_filters_limit_row_sample]
2718
def filter_limit_row_sample(project_id, instance_id, table_id):
19+
from google.cloud import bigtable
20+
from google.cloud.bigtable import row_filters
21+
2822
client = bigtable.Client(project=project_id, admin=True)
2923
instance = client.instance(instance_id)
3024
table = instance.table(table_id)
@@ -37,6 +31,9 @@ def filter_limit_row_sample(project_id, instance_id, table_id):
3731
# [END bigtable_filters_limit_row_sample]
3832
# [START bigtable_filters_limit_row_regex]
3933
def filter_limit_row_regex(project_id, instance_id, table_id):
34+
from google.cloud import bigtable
35+
from google.cloud.bigtable import row_filters
36+
4037
client = bigtable.Client(project=project_id, admin=True)
4138
instance = client.instance(instance_id)
4239
table = instance.table(table_id)
@@ -51,6 +48,9 @@ def filter_limit_row_regex(project_id, instance_id, table_id):
5148
# [END bigtable_filters_limit_row_regex]
5249
# [START bigtable_filters_limit_cells_per_col]
5350
def filter_limit_cells_per_col(project_id, instance_id, table_id):
51+
from google.cloud import bigtable
52+
from google.cloud.bigtable import row_filters
53+
5454
client = bigtable.Client(project=project_id, admin=True)
5555
instance = client.instance(instance_id)
5656
table = instance.table(table_id)
@@ -63,6 +63,9 @@ def filter_limit_cells_per_col(project_id, instance_id, table_id):
6363
# [END bigtable_filters_limit_cells_per_col]
6464
# [START bigtable_filters_limit_cells_per_row]
6565
def filter_limit_cells_per_row(project_id, instance_id, table_id):
66+
from google.cloud import bigtable
67+
from google.cloud.bigtable import row_filters
68+
6669
client = bigtable.Client(project=project_id, admin=True)
6770
instance = client.instance(instance_id)
6871
table = instance.table(table_id)
@@ -75,6 +78,9 @@ def filter_limit_cells_per_row(project_id, instance_id, table_id):
7578
# [END bigtable_filters_limit_cells_per_row]
7679
# [START bigtable_filters_limit_cells_per_row_offset]
7780
def filter_limit_cells_per_row_offset(project_id, instance_id, table_id):
81+
from google.cloud import bigtable
82+
from google.cloud.bigtable import row_filters
83+
7884
client = bigtable.Client(project=project_id, admin=True)
7985
instance = client.instance(instance_id)
8086
table = instance.table(table_id)
@@ -87,6 +93,9 @@ def filter_limit_cells_per_row_offset(project_id, instance_id, table_id):
8793
# [END bigtable_filters_limit_cells_per_row_offset]
8894
# [START bigtable_filters_limit_col_family_regex]
8995
def filter_limit_col_family_regex(project_id, instance_id, table_id):
96+
from google.cloud import bigtable
97+
from google.cloud.bigtable import row_filters
98+
9099
client = bigtable.Client(project=project_id, admin=True)
91100
instance = client.instance(instance_id)
92101
table = instance.table(table_id)
@@ -101,6 +110,9 @@ def filter_limit_col_family_regex(project_id, instance_id, table_id):
101110
# [END bigtable_filters_limit_col_family_regex]
102111
# [START bigtable_filters_limit_col_qualifier_regex]
103112
def filter_limit_col_qualifier_regex(project_id, instance_id, table_id):
113+
from google.cloud import bigtable
114+
from google.cloud.bigtable import row_filters
115+
104116
client = bigtable.Client(project=project_id, admin=True)
105117
instance = client.instance(instance_id)
106118
table = instance.table(table_id)
@@ -115,6 +127,9 @@ def filter_limit_col_qualifier_regex(project_id, instance_id, table_id):
115127
# [END bigtable_filters_limit_col_qualifier_regex]
116128
# [START bigtable_filters_limit_col_range]
117129
def filter_limit_col_range(project_id, instance_id, table_id):
130+
from google.cloud import bigtable
131+
from google.cloud.bigtable import row_filters
132+
118133
client = bigtable.Client(project=project_id, admin=True)
119134
instance = client.instance(instance_id)
120135
table = instance.table(table_id)
@@ -131,6 +146,9 @@ def filter_limit_col_range(project_id, instance_id, table_id):
131146
# [END bigtable_filters_limit_col_range]
132147
# [START bigtable_filters_limit_value_range]
133148
def filter_limit_value_range(project_id, instance_id, table_id):
149+
from google.cloud import bigtable
150+
from google.cloud.bigtable import row_filters
151+
134152
client = bigtable.Client(project=project_id, admin=True)
135153
instance = client.instance(instance_id)
136154
table = instance.table(table_id)
@@ -148,6 +166,9 @@ def filter_limit_value_range(project_id, instance_id, table_id):
148166

149167

150168
def filter_limit_value_regex(project_id, instance_id, table_id):
169+
from google.cloud import bigtable
170+
from google.cloud.bigtable import row_filters
171+
151172
client = bigtable.Client(project=project_id, admin=True)
152173
instance = client.instance(instance_id)
153174
table = instance.table(table_id)
@@ -162,6 +183,10 @@ def filter_limit_value_regex(project_id, instance_id, table_id):
162183
# [END bigtable_filters_limit_value_regex]
163184
# [START bigtable_filters_limit_timestamp_range]
164185
def filter_limit_timestamp_range(project_id, instance_id, table_id):
186+
from google.cloud import bigtable
187+
from google.cloud.bigtable import row_filters
188+
import datetime
189+
165190
client = bigtable.Client(project=project_id, admin=True)
166191
instance = client.instance(instance_id)
167192
table = instance.table(table_id)
@@ -178,6 +203,9 @@ def filter_limit_timestamp_range(project_id, instance_id, table_id):
178203
# [END bigtable_filters_limit_timestamp_range]
179204
# [START bigtable_filters_limit_block_all]
180205
def filter_limit_block_all(project_id, instance_id, table_id):
206+
from google.cloud import bigtable
207+
from google.cloud.bigtable import row_filters
208+
181209
client = bigtable.Client(project=project_id, admin=True)
182210
instance = client.instance(instance_id)
183211
table = instance.table(table_id)
@@ -190,6 +218,9 @@ def filter_limit_block_all(project_id, instance_id, table_id):
190218
# [END bigtable_filters_limit_block_all]
191219
# [START bigtable_filters_limit_pass_all]
192220
def filter_limit_pass_all(project_id, instance_id, table_id):
221+
from google.cloud import bigtable
222+
from google.cloud.bigtable import row_filters
223+
193224
client = bigtable.Client(project=project_id, admin=True)
194225
instance = client.instance(instance_id)
195226
table = instance.table(table_id)
@@ -202,6 +233,9 @@ def filter_limit_pass_all(project_id, instance_id, table_id):
202233
# [END bigtable_filters_limit_pass_all]
203234
# [START bigtable_filters_modify_strip_value]
204235
def filter_modify_strip_value(project_id, instance_id, table_id):
236+
from google.cloud import bigtable
237+
from google.cloud.bigtable import row_filters
238+
205239
client = bigtable.Client(project=project_id, admin=True)
206240
instance = client.instance(instance_id)
207241
table = instance.table(table_id)
@@ -214,6 +248,9 @@ def filter_modify_strip_value(project_id, instance_id, table_id):
214248
# [END bigtable_filters_modify_strip_value]
215249
# [START bigtable_filters_modify_apply_label]
216250
def filter_modify_apply_label(project_id, instance_id, table_id):
251+
from google.cloud import bigtable
252+
from google.cloud.bigtable import row_filters
253+
217254
client = bigtable.Client(project=project_id, admin=True)
218255
instance = client.instance(instance_id)
219256
table = instance.table(table_id)
@@ -226,6 +263,9 @@ def filter_modify_apply_label(project_id, instance_id, table_id):
226263
# [END bigtable_filters_modify_apply_label]
227264
# [START bigtable_filters_composing_chain]
228265
def filter_composing_chain(project_id, instance_id, table_id):
266+
from google.cloud import bigtable
267+
from google.cloud.bigtable import row_filters
268+
229269
client = bigtable.Client(project=project_id, admin=True)
230270
instance = client.instance(instance_id)
231271
table = instance.table(table_id)
@@ -245,6 +285,9 @@ def filter_composing_chain(project_id, instance_id, table_id):
245285
# [END bigtable_filters_composing_chain]
246286
# [START bigtable_filters_composing_interleave]
247287
def filter_composing_interleave(project_id, instance_id, table_id):
288+
from google.cloud import bigtable
289+
from google.cloud.bigtable import row_filters
290+
248291
client = bigtable.Client(project=project_id, admin=True)
249292
instance = client.instance(instance_id)
250293
table = instance.table(table_id)
@@ -264,6 +307,9 @@ def filter_composing_interleave(project_id, instance_id, table_id):
264307
# [END bigtable_filters_composing_interleave]
265308
# [START bigtable_filters_composing_condition]
266309
def filter_composing_condition(project_id, instance_id, table_id):
310+
from google.cloud import bigtable
311+
from google.cloud.bigtable import row_filters
312+
267313
client = bigtable.Client(project=project_id, admin=True)
268314
instance = client.instance(instance_id)
269315
table = instance.table(table_id)
@@ -285,9 +331,8 @@ def filter_composing_condition(project_id, instance_id, table_id):
285331

286332

287333
# [END bigtable_filters_composing_condition]
288-
# [END_EXCLUDE]
289-
290334

335+
# [START bigtable_filters_print]
291336
def print_row(row):
292337
print("Reading data for {}:".format(row.row_key.decode("utf-8")))
293338
for cf, cols in sorted(row.cells.items()):

packages/google-cloud-bigtable/samples/snippets/filters/filter_snippets_async.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
# See the License for the specific language governing permissions and
1212
# limitations under the License.
1313

14-
from google.cloud._helpers import _datetime_from_microseconds
15-
from google.cloud.bigtable.data import Row
16-
1714

1815
# [START bigtable_filters_limit_row_sample_asyncio]
1916
async def filter_limit_row_sample(project_id, instance_id, table_id):
@@ -368,10 +365,11 @@ async def filter_composing_condition(project_id, instance_id, table_id):
368365

369366

370367
# [END bigtable_filters_composing_condition_asyncio]
371-
# [END_EXCLUDE]
372368

373369

374-
def print_row(row: Row):
370+
def print_row(row):
371+
from google.cloud._helpers import _datetime_from_microseconds
372+
375373
print("Reading data for {}:".format(row.row_key.decode("utf-8")))
376374
last_family = None
377375
for cell in row.cells:

0 commit comments

Comments
 (0)