Skip to content

Commit d50c779

Browse files
added samples to pipeline, fixed up a few samples to actually work (Azure#15947)
* added samples to pipeline, fixed up a few samples to actually work * fixed up samples to pass, changed table names, inserted a few sleeps * fixed up unicode and python2 issues, added cleanup methods and unique table names * added unicode endings * fixing up samples to use with, and removing unnecessary comments * bug on a sample
1 parent 7cf106f commit d50c779

18 files changed

+636
-359
lines changed

sdk/tables/azure-data-tables/samples/async_samples/sample_authentication_async.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,23 @@
3030
from datetime import datetime, timedelta
3131
import os
3232
import asyncio
33+
from dotenv import find_dotenv, load_dotenv
34+
3335

3436
class TableAuthSamples(object):
35-
connection_string = os.getenv("AZURE_TABLES_CONNECTION_STRING")
36-
access_key = os.getenv("AZURE_TABLES_KEY")
37-
account_url = os.getenv("AZURE_TABLES_ACCOUNT_URL")
38-
account_name = os.getenv("AZURE_TABLES_ACCOUNT_NAME")
37+
38+
def __init__(self):
39+
load_dotenv(find_dotenv())
40+
# self.connection_string = os.getenv("AZURE_TABLES_CONNECTION_STRING")
41+
self.access_key = os.getenv("TABLES_PRIMARY_STORAGE_ACCOUNT_KEY")
42+
self.endpoint = os.getenv("TABLES_STORAGE_ENDPOINT_SUFFIX")
43+
self.account_name = os.getenv("TABLES_STORAGE_ACCOUNT_NAME")
44+
self.account_url = "{}.table.{}".format(self.account_name, self.endpoint)
45+
self.connection_string = "DefaultEndpointsProtocol=https;AccountName={};AccountKey={};EndpointSuffix={}".format(
46+
self.account_name,
47+
self.access_key,
48+
self.endpoint
49+
)
3950

4051
async def authentication_by_connection_string(self):
4152
# Instantiate a TableServiceClient using a connection string
@@ -62,6 +73,7 @@ async def authentication_by_shared_access_signature(self):
6273

6374
# Create a SAS token to use for authentication of a client
6475
from azure.data.tables import generate_account_sas, ResourceTypes, AccountSasPermissions
76+
print("Account name: {}".format(self.account_name))
6577
sas_token = generate_account_sas(
6678
self.account_name,
6779
self.access_key,
@@ -84,4 +96,5 @@ async def main():
8496

8597

8698
if __name__ == '__main__':
87-
asyncio.run(main())
99+
loop = asyncio.get_event_loop()
100+
loop.run_until_complete(main())

sdk/tables/azure-data-tables/samples/async_samples/sample_batching_async.py

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,65 +23,108 @@
2323

2424
from datetime import datetime, timedelta
2525
import os
26+
from time import sleep
2627
import asyncio
28+
from dotenv import find_dotenv, load_dotenv
2729

2830

2931
class CreateClients(object):
30-
connection_string = os.getenv("AZURE_TABLES_CONNECTION_STRING")
31-
my_table = os.getenv("AZURE_TABLES_NAME") or ""
3232

33-
async def sample_batching(self):
34-
# Instantiate a TableServiceClient using a connection string
35-
entity1 = {
33+
def __init__(self):
34+
load_dotenv(find_dotenv())
35+
# self.connection_string = os.getenv("AZURE_TABLES_CONNECTION_STRING")
36+
self.access_key = os.getenv("TABLES_PRIMARY_STORAGE_ACCOUNT_KEY")
37+
self.endpoint = os.getenv("TABLES_STORAGE_ENDPOINT_SUFFIX")
38+
self.account_name = os.getenv("TABLES_STORAGE_ACCOUNT_NAME")
39+
self.account_url = "{}.table.{}".format(self.account_name, self.endpoint)
40+
self.connection_string = "DefaultEndpointsProtocol=https;AccountName={};AccountKey={};EndpointSuffix={}".format(
41+
self.account_name,
42+
self.access_key,
43+
self.endpoint
44+
)
45+
self.table_name = "sampleBatchingAsync"
46+
47+
async def _create_entities(self):
48+
from azure.core.exceptions import ResourceExistsError
49+
self.entity1 = {
3650
'PartitionKey': 'pk001',
3751
'RowKey': 'rk001',
3852
'Value': 4,
3953
'day': "Monday",
4054
'float': 4.003
4155
}
42-
entity2 = {
56+
self.entity2 = {
4357
'PartitionKey': 'pk001',
4458
'RowKey': 'rk002',
4559
'Value': 4,
4660
'day': "Tuesday",
4761
'float': 4.003
4862
}
49-
entity3 = {
63+
self.entity3 = {
5064
'PartitionKey': 'pk001',
5165
'RowKey': 'rk003',
5266
'Value': 4,
5367
'day': "Wednesday",
5468
'float': 4.003
5569
}
56-
entity4 = {
70+
self.entity4 = {
5771
'PartitionKey': 'pk001',
5872
'RowKey': 'rk004',
5973
'Value': 4,
6074
'day': "Thursday",
6175
'float': 4.003
6276
}
6377

78+
entities = [self.entity2, self.entity3, self.entity4]
79+
80+
for entity in entities:
81+
try:
82+
await self.table_client.create_entity(entity)
83+
except ResourceExistsError:
84+
print("entity already exists")
85+
pass
86+
87+
async def sample_batching(self):
88+
# Instantiate a TableServiceClient using a connection string
89+
90+
6491
# [START batching]
6592
from azure.data.tables.aio import TableClient
66-
from azure.data.tables import UpdateMode
67-
table_client = TableClient.from_connection_string(conn_str=self.connection_string, table_name="tableName")
68-
69-
batch = table_client.create_batch()
70-
batch.create_entity(entity1)
71-
batch.delete_entity(entity2)
72-
batch.upsert_entity(entity3)
73-
batch.update_entity(entity4, mode=UpdateMode.REPLACE)
93+
from azure.data.tables import UpdateMode, BatchErrorException
94+
from azure.core.exceptions import ResourceExistsError
95+
self.table_client = TableClient.from_connection_string(
96+
conn_str=self.connection_string, table_name=self.table_name)
97+
7498
try:
75-
await table_client.send_batch(batch)
99+
await self.table_client.create_table()
100+
print("Created table")
101+
except ResourceExistsError:
102+
print("Table already exists")
103+
104+
await self._create_entities()
105+
106+
batch = self.table_client.create_batch()
107+
batch.create_entity(self.entity1)
108+
batch.delete_entity(partition_key=self.entity2['PartitionKey'], row_key=self.entity2['RowKey'])
109+
batch.upsert_entity(self.entity3)
110+
batch.update_entity(self.entity4, mode=UpdateMode.REPLACE)
111+
try:
112+
await self.table_client.send_batch(batch)
76113
except BatchErrorException as e:
77114
print("There was an error with the batch operation")
78115
print("Error: {}".format(e))
79116
# [END batching]
80117

118+
async def clean_up(self):
119+
await self.table_client.delete_table()
120+
await self.table_client.__aexit__()
121+
81122

82123
async def main():
83124
sample = CreateClients()
84125
await sample.sample_batching()
126+
await sample.clean_up()
85127

86128
if __name__ == '__main__':
87-
asyncio.run(main())
129+
loop = asyncio.get_event_loop()
130+
loop.run_until_complete(main())

sdk/tables/azure-data-tables/samples/async_samples/sample_create_client_async.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,23 @@
3030
from datetime import datetime, timedelta
3131
import os
3232
import asyncio
33+
from dotenv import find_dotenv, load_dotenv
3334

3435

3536
class CreateClients(object):
36-
connection_string = os.getenv("AZURE_TABLES_CONNECTION_STRING")
37-
access_key = os.getenv("AZURE_TABLES_KEY")
38-
account_url = os.getenv("AZURE_TABLES_ACCOUNT_URL")
39-
account_name = os.getenv("AZURE_TABLES_ACCOUNT_NAME")
40-
my_table = os.getenv("AZURE_TABLES_NAME") or ""
37+
38+
def __init__(self):
39+
load_dotenv(find_dotenv())
40+
# self.connection_string = os.getenv("AZURE_TABLES_CONNECTION_STRING")
41+
self.access_key = os.getenv("TABLES_PRIMARY_STORAGE_ACCOUNT_KEY")
42+
self.endpoint = os.getenv("TABLES_STORAGE_ENDPOINT_SUFFIX")
43+
self.account_name = os.getenv("TABLES_STORAGE_ACCOUNT_NAME")
44+
self.account_url = "{}.table.{}".format(self.account_name, self.endpoint)
45+
self.connection_string = "DefaultEndpointsProtocol=https;AccountName={};AccountKey={};EndpointSuffix={}".format(
46+
self.account_name,
47+
self.access_key,
48+
self.endpoint
49+
)
4150

4251
async def create_table_client(self):
4352
# Instantiate a TableServiceClient using a connection string
@@ -54,4 +63,5 @@ async def main():
5463

5564

5665
if __name__ == '__main__':
57-
asyncio.run(main())
66+
loop = asyncio.get_event_loop()
67+
loop.run_until_complete(main())

sdk/tables/azure-data-tables/samples/async_samples/sample_create_delete_table_async.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,26 @@
2424
"""
2525

2626
import os
27-
import logging
27+
from time import sleep
2828
import asyncio
29+
from dotenv import find_dotenv, load_dotenv
2930

30-
_LOGGER = logging.getLogger(__name__)
3131

3232
class CreateDeleteTable(object):
33-
connection_string = os.getenv("AZURE_TABLES_CONNECTION_STRING")
34-
access_key = os.getenv("AZURE_TABLES_KEY")
35-
account_url = os.getenv("AZURE_TABLES_ACCOUNT_URL")
36-
account_name = os.getenv("AZURE_TABLES_ACCOUNT_NAME")
37-
table_name = "OfficeSupplies"
33+
34+
def __init__(self):
35+
load_dotenv(find_dotenv())
36+
# self.connection_string = os.getenv("AZURE_TABLES_CONNECTION_STRING")
37+
self.access_key = os.getenv("TABLES_PRIMARY_STORAGE_ACCOUNT_KEY")
38+
self.endpoint = os.getenv("TABLES_STORAGE_ENDPOINT_SUFFIX")
39+
self.account_name = os.getenv("TABLES_STORAGE_ACCOUNT_NAME")
40+
self.account_url = "{}.table.{}".format(self.account_name, self.endpoint)
41+
self.connection_string = "DefaultEndpointsProtocol=https;AccountName={};AccountKey={};EndpointSuffix={}".format(
42+
self.account_name,
43+
self.access_key,
44+
self.endpoint
45+
)
46+
self.table_name = "CreateDeleteTable"
3847

3948

4049
async def create_table(self):
@@ -55,7 +64,7 @@ async def create_if_not_exists(self):
5564

5665
# [START create_if_not_exists]
5766
async with TableServiceClient.from_connection_string(self.connection_string) as table_service_client:
58-
table_item = TableServiceClient.create_table_if_not_exists(table_name=self.table_name)
67+
table_item = await table_service_client.create_table_if_not_exists(table_name=self.table_name)
5968
print("Table name: {}".format(table_item.table_name))
6069
# [END create_if_not_exists]
6170

@@ -73,7 +82,7 @@ async def delete_table(self):
7382
# [END delete_table]
7483

7584
async def create_from_table_client(self):
76-
from azure.data.table import TableClient
85+
from azure.data.tables.aio import TableClient
7786

7887
# [START create_from_table_client]
7988
async with TableClient.from_connection_string(conn_str=self.connection_string, table_name=self.table_name) as table_client:
@@ -85,7 +94,8 @@ async def create_from_table_client(self):
8594
# [END create_from_table_client]
8695

8796
async def delete_from_table_client(self):
88-
from azure.data.table import TableClient
97+
from azure.data.tables.aio import TableClient
98+
from azure.core.exceptions import ResourceNotFoundError
8999

90100
# [START delete_from_table_client]
91101
async with TableClient.from_connection_string(conn_str=self.connection_string, table_name=self.table_name) as table_client:
@@ -100,8 +110,11 @@ async def delete_from_table_client(self):
100110
async def main():
101111
sample = CreateDeleteTable()
102112
await sample.create_table()
113+
await sample.create_if_not_exists()
103114
await sample.delete_table()
115+
await sample.delete_from_table_client()
104116

105117

106118
if __name__ == '__main__':
107-
asyncio.run(main())
119+
loop = asyncio.get_event_loop()
120+
loop.run_until_complete(main())

sdk/tables/azure-data-tables/samples/async_samples/sample_insert_delete_entities_async.py

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,33 @@
2121
"""
2222

2323
import os
24+
from time import sleep
2425
import asyncio
26+
from dotenv import find_dotenv, load_dotenv
2527

2628
class InsertDeleteEntity(object):
27-
connection_string = os.getenv("AZURE_TABLES_CONNECTION_STRING")
28-
access_key = os.getenv("AZURE_TABLES_KEY")
29-
account_url = os.getenv("AZURE_TABLES_ACCOUNT_URL")
30-
account_name = os.getenv("AZURE_TABLES_ACCOUNT_NAME")
31-
table_name = "OfficeSupplies"
32-
33-
entity = {
34-
'PartitionKey': 'color',
35-
'RowKey': 'brand',
36-
'text': 'Marker',
37-
'color': 'Purple',
38-
'price': '5'
39-
}
29+
30+
def __init__(self):
31+
load_dotenv(find_dotenv())
32+
# self.connection_string = os.getenv("AZURE_TABLES_CONNECTION_STRING")
33+
self.access_key = os.getenv("TABLES_PRIMARY_STORAGE_ACCOUNT_KEY")
34+
self.endpoint = os.getenv("TABLES_STORAGE_ENDPOINT_SUFFIX")
35+
self.account_name = os.getenv("TABLES_STORAGE_ACCOUNT_NAME")
36+
self.account_url = "{}.table.{}".format(self.account_name, self.endpoint)
37+
self.connection_string = "DefaultEndpointsProtocol=https;AccountName={};AccountKey={};EndpointSuffix={}".format(
38+
self.account_name,
39+
self.access_key,
40+
self.endpoint
41+
)
42+
self.table_name = "InsertDeleteAsync"
43+
44+
self.entity = {
45+
'PartitionKey': 'color',
46+
'RowKey': 'brand',
47+
'text': 'Marker',
48+
'color': 'Purple',
49+
'price': '5'
50+
}
4051

4152
async def create_entity(self):
4253
from azure.data.tables.aio import TableClient
@@ -83,12 +94,24 @@ async def delete_entity(self):
8394
print("Entity does not exists")
8495
# [END delete_entity]
8596

97+
async def clean_up(self):
98+
from azure.data.tables.aio import TableServiceClient
99+
tsc = TableServiceClient.from_connection_string(self.connection_string)
100+
async with tsc:
101+
async for table in tsc.list_tables():
102+
await tsc.delete_table(table.table_name)
103+
104+
print("Cleaned up")
105+
86106

87107
async def main():
88108
ide = InsertDeleteEntity()
89109
await ide.create_entity()
90110
await ide.delete_entity()
111+
await ide.clean_up()
91112

92113

93114
if __name__ == '__main__':
94-
asyncio.run(main())
115+
loop = asyncio.get_event_loop()
116+
loop.run_until_complete(main())
117+

0 commit comments

Comments
 (0)