Skip to content

Commit f8c2ad1

Browse files
[Tables] updating entities in samples to use more edmtypes (Azure#19803)
* updating entities in samples to use more edmtypes * updating samples * specifying unicode for py2.7 compat * specifying unicode on entities
1 parent 07d41c4 commit f8c2ad1

File tree

4 files changed

+99
-30
lines changed

4 files changed

+99
-30
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
import os
2424
import asyncio
25+
from datetime import datetime
26+
from uuid import uuid4
2527
from dotenv import find_dotenv, load_dotenv
2628

2729

@@ -37,7 +39,17 @@ def __init__(self):
3739
)
3840
self.table_name = "InsertDeleteAsync"
3941

40-
self.entity = {"PartitionKey": "color", "RowKey": "brand", "text": "Marker", "color": "Purple", "price": "5"}
42+
self.entity = {
43+
"PartitionKey": "color",
44+
"RowKey": "brand",
45+
"text": "Marker",
46+
"color": "Purple",
47+
"price": 4.99,
48+
"last_updated": datetime.today(),
49+
"product_id": uuid4(),
50+
"inventory_count": 42,
51+
"barcode": b"135aefg8oj0ld58"
52+
}
4153

4254
async def create_entity(self):
4355
from azure.data.tables.aio import TableClient

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

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from datetime import datetime, timedelta
2323
import os
2424
from time import sleep
25+
from uuid import uuid4
2526
import asyncio
2627
from dotenv import find_dotenv, load_dotenv
2728

@@ -50,11 +51,16 @@ async def create_and_get_entities(self):
5051

5152
my_entity = {
5253
"PartitionKey": "color",
53-
"RowKey": "crayola",
54+
"RowKey": "brand",
5455
"text": "Marker",
5556
"color": "Purple",
56-
"price": "5",
57+
"price": 4.99,
58+
"last_updated": datetime.today(),
59+
"product_id": uuid4(),
60+
"inventory_count": 42,
61+
"barcode": b"135aefg8oj0ld58"
5762
}
63+
5864
try:
5965
created_entity = await table.create_entity(entity=my_entity)
6066
print("Created entity: {}".format(created_entity))
@@ -80,8 +86,24 @@ async def list_all_entities(self):
8086
async with table:
8187
await table.create_table()
8288

83-
entity = {"PartitionKey": "color2", "RowKey": "sharpie", "text": "Marker", "color": "Purple", "price": "5"}
84-
entity1 = {"PartitionKey": "color2", "RowKey": "crayola", "text": "Marker", "color": "Red", "price": "3"}
89+
entity = {
90+
"PartitionKey": "color2",
91+
"RowKey": "sharpie",
92+
"text": "Marker",
93+
"color": "Purple",
94+
"price": 5.99,
95+
"inventory": 42,
96+
"product_id": uuid4(),
97+
}
98+
entity1 = {
99+
"PartitionKey": "color2",
100+
"RowKey": "crayola",
101+
"text": "Marker",
102+
"color": "Red",
103+
"price": 3.99,
104+
"inventory": 42,
105+
"product_id": uuid4(),
106+
}
85107

86108
try:
87109
# Create entities
@@ -109,8 +131,24 @@ async def update_entities(self):
109131
async with table:
110132
await table.create_table()
111133

112-
entity = {"PartitionKey": "color", "RowKey": "sharpie", "text": "Marker", "color": "Purple", "price": "5"}
113-
entity1 = {"PartitionKey": "color2", "RowKey": "crayola", "text": "Marker", "color": "Red", "price": "3"}
134+
entity = {
135+
"PartitionKey": "color2",
136+
"RowKey": "sharpie",
137+
"text": "Marker",
138+
"color": "Purple",
139+
"price": 5.99,
140+
"inventory": 42,
141+
"product_id": uuid4(),
142+
}
143+
entity1 = {
144+
"PartitionKey": "color2",
145+
"RowKey": "crayola",
146+
"text": "Marker",
147+
"color": "Red",
148+
"price": 3.99,
149+
"inventory": 42,
150+
"product_id": uuid4(),
151+
}
114152

115153
try:
116154
# Create entities

sdk/tables/azure-data-tables/samples/sample_insert_delete_entities.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
1) AZURE_STORAGE_CONNECTION_STRING - the connection string to your storage account
2121
"""
2222

23+
from datetime import datetime
2324
import os
25+
from uuid import uuid4
2426
from dotenv import find_dotenv, load_dotenv
2527

2628

@@ -32,7 +34,7 @@ def __init__(self):
3234
self.account_name = os.getenv("TABLES_STORAGE_ACCOUNT_NAME")
3335
self.endpoint = "{}.table.{}".format(self.account_name, self.endpoint_suffix)
3436
self.connection_string = (
35-
u"DefaultEndpointsProtocol=https;AccountName={};AccountKey={};EndpointSuffix={}".format(
37+
"DefaultEndpointsProtocol=https;AccountName={};AccountKey={};EndpointSuffix={}".format(
3638
self.account_name, self.access_key, self.endpoint_suffix
3739
)
3840
)
@@ -41,9 +43,13 @@ def __init__(self):
4143
self.entity = {
4244
u"PartitionKey": u"color",
4345
u"RowKey": u"brand",
44-
u"text": u"Marker",
45-
u"color": u"Purple",
46-
u"price": u"5",
46+
"text": "Marker",
47+
"color": "Purple",
48+
"price": 4.99,
49+
"last_updated": datetime.today(),
50+
"product_id": uuid4(),
51+
"inventory_count": 42,
52+
"barcode": b"135aefg8oj0ld58"
4753
}
4854

4955
def create_entity(self):

sdk/tables/azure-data-tables/samples/sample_update_upsert_merge_entities.py

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from datetime import datetime, timedelta
2424
from dotenv import find_dotenv, load_dotenv
2525
import os
26+
from uuid import uuid4
2627

2728

2829
class TableEntitySamples(object):
@@ -48,10 +49,14 @@ def create_and_get_entities(self):
4849

4950
my_entity = {
5051
u"PartitionKey": u"color",
51-
u"RowKey": u"crayola",
52-
u"text": u"Marker",
53-
u"color": u"Purple",
54-
u"price": u"5",
52+
u"RowKey": u"brand",
53+
"text": "Marker",
54+
"color": "Purple",
55+
"price": 4.99,
56+
"last_updated": datetime.today(),
57+
"product_id": uuid4(),
58+
"inventory_count": 42,
59+
"barcode": b"135aefg8oj0ld58"
5560
}
5661
try:
5762
# [START create_entity]
@@ -81,16 +86,20 @@ def list_all_entities(self):
8186
entity = {
8287
u"PartitionKey": u"color2",
8388
u"RowKey": u"sharpie",
84-
u"text": u"Marker",
85-
u"color": u"Purple",
86-
u"price": u"5",
89+
"text": "Marker",
90+
"color": "Purple",
91+
"price": 5.99,
92+
"inventory": 42,
93+
"product_id": uuid4(),
8794
}
8895
entity1 = {
8996
u"PartitionKey": u"color2",
9097
u"RowKey": u"crayola",
91-
u"text": u"Marker",
92-
u"color": u"Red",
93-
u"price": u"3",
98+
"text": "Marker",
99+
"color": "Red",
100+
"price": 3.99,
101+
"inventory": 42,
102+
"product_id": uuid4(),
94103
}
95104

96105
try:
@@ -122,16 +131,20 @@ def update_entities(self):
122131
entity = {
123132
u"PartitionKey": u"color2",
124133
u"RowKey": u"sharpie",
125-
u"text": u"Marker",
126-
u"color": u"Purple",
127-
u"price": u"5",
134+
"text": "Marker",
135+
"color": "Purple",
136+
"price": 5.99,
137+
"inventory": 42,
138+
"product_id": uuid4(),
128139
}
129140
entity1 = {
130141
u"PartitionKey": u"color2",
131142
u"RowKey": u"crayola",
132-
u"text": u"Marker",
133-
u"color": u"Red",
134-
u"price": u"3",
143+
"text": "Marker",
144+
"color": "Red",
145+
"price": 3.99,
146+
"inventory": 42,
147+
"product_id": uuid4(),
135148
}
136149

137150
try:
@@ -144,22 +157,22 @@ def update_entities(self):
144157
insert_entity = table.upsert_entity(mode=UpdateMode.REPLACE, entity=entity1)
145158
print("Inserted entity: {}".format(insert_entity))
146159

147-
created[u"text"] = u"NewMarker"
160+
created["text"] = "NewMarker"
148161
merged_entity = table.upsert_entity(mode=UpdateMode.MERGE, entity=entity)
149162
print("Merged entity: {}".format(merged_entity))
150163
# [END upsert_entity]
151164

152165
# [START update_entity]
153166
# Update the entity
154-
created[u"text"] = u"NewMarker"
167+
created["text"] = "NewMarker"
155168
table.update_entity(mode=UpdateMode.REPLACE, entity=created)
156169

157170
# Get the replaced entity
158171
replaced = table.get_entity(partition_key=created["PartitionKey"], row_key=created["RowKey"])
159172
print("Replaced entity: {}".format(replaced))
160173

161174
# Merge the entity
162-
replaced[u"color"] = u"Blue"
175+
replaced["color"] = "Blue"
163176
table.update_entity(mode=UpdateMode.MERGE, entity=replaced)
164177

165178
# Get the merged entity

0 commit comments

Comments
 (0)