<s> import time <EOL> import uuid <EOL> from datetime import datetime <EOL> from azure . common import ( <EOL> AzureHttpError , <EOL> AzureConflictHttpError , <EOL> AzureMissingResourceHttpError , <EOL> ) <EOL> from azure . storage import ( <EOL> Logging , <EOL> Metrics , <EOL> CorsRule , <EOL> ) <EOL> from azure . storage . table import ( <EOL> Entity , <EOL> TableBatch , <EOL> EdmType , <EOL> EntityProperty , <EOL> TablePayloadFormat , <EOL> ) <EOL> class TableSamples ( ) : <EOL> def __init__ ( self , account ) : <EOL> self . account = account <EOL> def run_all_samples ( self ) : <EOL> self . service = self . account . create_table_service ( ) <EOL> self . create_table ( ) <EOL> self . delete_table ( ) <EOL> self . exists ( ) <EOL> self . query_entities ( ) <EOL> self . batch ( ) <EOL> self . create_entity_class ( ) <EOL> self . create_entity_dict ( ) <EOL> self . insert_entity ( ) <EOL> self . get_entity ( ) <EOL> self . update_entity ( ) <EOL> self . merge_entity ( ) <EOL> self . insert_or_merge_entity ( ) <EOL> self . insert_or_replace_entity ( ) <EOL> self . delete_entity ( ) <EOL> self . list_tables ( ) <EOL> def _get_table_reference ( self , prefix = '<STR_LIT>' ) : <EOL> table_name = '<STR_LIT>' . format ( prefix , str ( uuid . uuid4 ( ) ) . replace ( '<STR_LIT:->' , '<STR_LIT>' ) ) <EOL> return table_name <EOL> def _create_table ( self , prefix = '<STR_LIT>' ) : <EOL> table_name = self . _get_table_reference ( prefix ) <EOL> self . service . create_table ( table_name ) <EOL> return table_name <EOL> def create_table ( self ) : <EOL> table_name1 = self . _get_table_reference ( ) <EOL> created = self . service . create_table ( table_name1 ) <EOL> table_name2 = self . _get_table_reference ( ) <EOL> created = self . service . create_table ( table_name2 ) <EOL> created = self . service . create_table ( table_name2 ) <EOL> try : <EOL> self . service . create_table ( table_name2 , fail_on_exist = True ) <EOL> except AzureConflictHttpError : <EOL> pass <EOL> self . service . delete_table ( table_name1 ) <EOL> self . service . delete_table ( table_name2 ) <EOL> def delete_table ( self ) : <EOL> table_name = self . _create_table ( ) <EOL> deleted = self . service . delete_table ( table_name ) <EOL> table_name = self . _get_table_reference ( ) <EOL> deleted = self . service . delete_table ( table_name ) <EOL> try : <EOL> self . service . delete_table ( table_name , fail_not_exist = True ) <EOL> except AzureMissingResourceHttpError : <EOL> pass <EOL> def exists ( self ) : <EOL> table_name = self . _get_table_reference ( ) <EOL> exists = self . service . exists ( table_name ) <EOL> self . service . create_table ( table_name ) <EOL> exists = self . service . exists ( table_name ) <EOL> self . service . delete_table ( table_name ) <EOL> def query_entities ( self ) : <EOL> table_name = self . _create_table ( ) <EOL> entities = [ ] <EOL> for i in range ( <NUM_LIT:1> , <NUM_LIT:5> ) : <EOL> entity = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' . format ( i ) , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : datetime ( <NUM_LIT> , <NUM_LIT:10> , i ) } <EOL> self . service . insert_entity ( table_name , entity ) <EOL> entities . append ( entity ) <EOL> queried_entities = list ( self . service . query_entities ( table_name ) ) <EOL> for entity in queried_entities : <EOL> print ( entity . RowKey ) <EOL> queried_entities = list ( self . service . query_entities ( table_name , num_results = <NUM_LIT:2> ) ) <EOL> for entity in queried_entities : <EOL> print ( entity . RowKey ) <EOL> filter = "<STR_LIT>" . format ( entities [ <NUM_LIT:1> ] [ '<STR_LIT>' ] ) <EOL> queried_entities = list ( self . service . query_entities ( table_name , filter = filter ) ) <EOL> for entity in queried_entities : <EOL> print ( entity . RowKey ) <EOL> queried_entities = list ( self . service . query_entities ( table_name , select = '<STR_LIT>' ) ) <EOL> for entity in queried_entities : <EOL> print ( entity . birthday ) <EOL> queried_entities [ <NUM_LIT:0> ] . get ( '<STR_LIT>' ) <EOL> queried_entities = list ( self . service . query_entities ( table_name , <EOL> accept = TablePayloadFormat . JSON_NO_METADATA ) ) <EOL> queried_entities [ <NUM_LIT:0> ] . birthday <EOL> queried_entities [ <NUM_LIT:0> ] . deceased <EOL> def resolver ( pk , rk , name , value , type ) : <EOL> if name == '<STR_LIT>' : <EOL> return EdmType . DATETIME <EOL> queried_entities = list ( self . service . query_entities ( table_name , <EOL> accept = TablePayloadFormat . JSON_NO_METADATA , <EOL> property_resolver = resolver ) ) <EOL> queried_entities [ <NUM_LIT:0> ] . birthday <EOL> queried_entities [ <NUM_LIT:0> ] . deceased <EOL> self . service . delete_table ( table_name ) <EOL> def batch ( self ) : <EOL> table_name = self . _create_table ( ) <EOL> entity = Entity ( ) <EOL> entity . PartitionKey = '<STR_LIT>' <EOL> entity . test = True <EOL> with self . service . batch ( table_name ) as batch : <EOL> for i in range ( <NUM_LIT:0> , <NUM_LIT:5> ) : <EOL> entity . RowKey = '<STR_LIT>' . format ( i ) <EOL> batch . insert_entity ( entity ) <EOL> batch = TableBatch ( ) <EOL> for i in range ( <NUM_LIT:0> , <NUM_LIT:5> ) : <EOL> entity . RowKey = '<STR_LIT>' . format ( i ) <EOL> batch . insert_entity ( entity ) <EOL> self . service . commit_batch ( table_name , batch ) <EOL> self . service . delete_table ( table_name ) <EOL> def create_entity_class ( self ) : <EOL> '''<STR_LIT>''' <EOL> entity = Entity ( ) <EOL> entity . PartitionKey = '<STR_LIT>' . format ( str ( uuid . uuid4 ( ) ) . replace ( '<STR_LIT:->' , '<STR_LIT>' ) ) <EOL> entity . RowKey = '<STR_LIT>' . format ( str ( uuid . uuid4 ( ) ) . replace ( '<STR_LIT:->' , '<STR_LIT>' ) ) <EOL> entity . age = <NUM_LIT> <EOL> entity . large = <NUM_LIT> <EOL> entity . sex = '<STR_LIT>' <EOL> entity . married = True <EOL> entity . ratio = <NUM_LIT> <EOL> entity . birthday = datetime ( <NUM_LIT> , <NUM_LIT:10> , <NUM_LIT:4> ) <EOL> entity . binary = EntityProperty ( EdmType . BINARY , b'<STR_LIT>' ) <EOL> entity . other = EntityProperty ( EdmType . INT32 , <NUM_LIT:20> ) <EOL> entity . clsid = EntityProperty ( EdmType . GUID , '<STR_LIT>' ) <EOL> return entity <EOL> def create_entity_dict ( self ) : <EOL> '''<STR_LIT>''' <EOL> entity = { } <EOL> entity [ '<STR_LIT>' ] = '<STR_LIT>' . format ( str ( uuid . uuid4 ( ) ) . replace ( '<STR_LIT:->' , '<STR_LIT>' ) ) <EOL> entity [ '<STR_LIT>' ] = '<STR_LIT>' . format ( str ( uuid . uuid4 ( ) ) . replace ( '<STR_LIT:->' , '<STR_LIT>' ) ) <EOL> entity [ '<STR_LIT>' ] = <NUM_LIT> <EOL> entity [ '<STR_LIT>' ] = <NUM_LIT> <EOL> entity [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> entity [ '<STR_LIT>' ] = True <EOL> entity [ '<STR_LIT>' ] = <NUM_LIT> <EOL> entity [ '<STR_LIT>' ] = datetime ( <NUM_LIT> , <NUM_LIT:10> , <NUM_LIT:4> ) <EOL> entity [ '<STR_LIT>' ] = EntityProperty ( EdmType . BINARY , b'<STR_LIT>' ) <EOL> entity [ '<STR_LIT>' ] = EntityProperty ( EdmType . INT32 , <NUM_LIT:20> ) <EOL> entity [ '<STR_LIT>' ] = EntityProperty ( EdmType . GUID , '<STR_LIT>' ) <EOL> return entity <EOL> def insert_entity ( self ) : <EOL> table_name = self . _create_table ( ) <EOL> entity = self . create_entity_dict ( ) <EOL> etag = self . service . insert_entity ( table_name , entity ) <EOL> entity = self . create_entity_class ( ) <EOL> etag = self . service . insert_entity ( table_name , entity ) <EOL> self . service . delete_table ( table_name ) <EOL> def get_entity ( self ) : <EOL> table_name = self . _create_table ( ) <EOL> insert_entity = self . create_entity_class ( ) <EOL> etag = self . service . insert_entity ( table_name , insert_entity ) <EOL> entity = self . service . get_entity ( table_name , insert_entity . PartitionKey , insert_entity . RowKey ) <EOL> entity . age <EOL> entity [ '<STR_LIT>' ] <EOL> entity . clsid . value <EOL> entity . clsid . type <EOL> entity = self . service . get_entity ( table_name , insert_entity . PartitionKey , insert_entity . RowKey , <EOL> select = '<STR_LIT>' ) <EOL> entity [ '<STR_LIT>' ] <EOL> entity . get ( '<STR_LIT>' ) <EOL> entity = self . service . get_entity ( table_name , insert_entity . PartitionKey , insert_entity . RowKey , <EOL> accept = TablePayloadFormat . JSON_NO_METADATA ) <EOL> entity . age <EOL> entity . clsid <EOL> entity . married <EOL> def resolver ( pk , rk , name , value , type ) : <EOL> if name == '<STR_LIT>' or name == '<STR_LIT>' : <EOL> return EdmType . INT64 <EOL> if name == '<STR_LIT>' : <EOL> return EdmType . DATETIME <EOL> if name == '<STR_LIT>' : <EOL> return EdmType . GUID <EOL> entity = self . service . get_entity ( table_name , insert_entity . PartitionKey , insert_entity . RowKey , <EOL> accept = TablePayloadFormat . JSON_NO_METADATA , <EOL> property_resolver = resolver ) <EOL> entity . age <EOL> entity . clsid . value <EOL> entity . clsid . type <EOL> entity . married <EOL> self . service . delete_table ( table_name ) <EOL> def update_entity ( self ) : <EOL> table_name = self . _create_table ( ) <EOL> entity = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : datetime ( <NUM_LIT> , <NUM_LIT:10> , <NUM_LIT:4> ) } <EOL> etag = self . service . insert_entity ( table_name , entity ) <EOL> entity = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True } <EOL> etag = self . service . update_entity ( table_name , entity ) <EOL> received_entity = self . service . get_entity ( table_name , entity [ '<STR_LIT>' ] , entity [ '<STR_LIT>' ] ) <EOL> received_entity . get ( '<STR_LIT>' ) <EOL> received_entity . get ( '<STR_LIT>' ) <EOL> entity = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : '<STR_LIT>' } <EOL> self . service . update_entity ( table_name , entity , if_match = etag ) <EOL> try : <EOL> self . service . update_entity ( table_name , entity , if_match = etag ) <EOL> except AzureHttpError : <EOL> pass <EOL> self . service . delete_table ( table_name ) <EOL> def merge_entity ( self ) : <EOL> table_name = self . _create_table ( ) <EOL> entity = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : datetime ( <NUM_LIT> , <NUM_LIT:10> , <NUM_LIT:4> ) } <EOL> etag = self . service . insert_entity ( table_name , entity ) <EOL> entity = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True } <EOL> etag = self . service . merge_entity ( table_name , entity ) <EOL> received_entity = self . service . get_entity ( table_name , entity [ '<STR_LIT>' ] , entity [ '<STR_LIT>' ] ) <EOL> received_entity . get ( '<STR_LIT>' ) <EOL> received_entity . get ( '<STR_LIT>' ) <EOL> entity = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : '<STR_LIT>' } <EOL> self . service . merge_entity ( table_name , entity , if_match = etag ) <EOL> try : <EOL> self . service . merge_entity ( table_name , entity , if_match = etag ) <EOL> except AzureHttpError : <EOL> pass <EOL> self . service . delete_table ( table_name ) <EOL> def insert_or_merge_entity ( self ) : <EOL> table_name = self . _create_table ( ) <EOL> entity = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : datetime ( <NUM_LIT> , <NUM_LIT:10> , <NUM_LIT:4> ) } <EOL> etag = self . service . insert_or_merge_entity ( table_name , entity ) <EOL> entity = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : '<STR_LIT>' } <EOL> etag = self . service . insert_or_merge_entity ( table_name , entity ) <EOL> received_entity = self . service . get_entity ( table_name , entity [ '<STR_LIT>' ] , entity [ '<STR_LIT>' ] ) <EOL> received_entity . get ( '<STR_LIT:id>' ) <EOL> received_entity . get ( '<STR_LIT>' ) <EOL> self . service . delete_table ( table_name ) <EOL> def insert_or_replace_entity ( self ) : <EOL> table_name = self . _create_table ( ) <EOL> entity = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : datetime ( <NUM_LIT> , <NUM_LIT:10> , <NUM_LIT:4> ) } <EOL> etag = self . service . insert_or_replace_entity ( table_name , entity ) <EOL> entity = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : '<STR_LIT>' } <EOL> etag = self . service . insert_or_replace_entity ( table_name , entity ) <EOL> received_entity = self . service . get_entity ( table_name , entity [ '<STR_LIT>' ] , entity [ '<STR_LIT>' ] ) <EOL> received_entity . get ( '<STR_LIT:id>' ) <EOL> received_entity . get ( '<STR_LIT>' ) <EOL> self . service . delete_table ( table_name ) <EOL> def delete_entity ( self ) : <EOL> table_name = self . _create_table ( ) <EOL> entity = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> etag = self . service . insert_entity ( table_name , entity ) <EOL> self . service . delete_entity ( table_name , entity [ '<STR_LIT>' ] , entity [ '<STR_LIT>' ] ) <EOL> entity = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : '<STR_LIT>' } <EOL> etag = self . service . insert_entity ( table_name , entity ) <EOL> self . service . update_entity ( table_name , entity , if_match = etag ) <EOL> try : <EOL> self . service . delete_entity ( table_name , entity [ '<STR_LIT>' ] , entity [ '<STR_LIT>' ] , if_match = etag ) <EOL> except AzureHttpError : <EOL> pass <EOL> self . service . delete_table ( table_name ) <EOL> def list_tables ( self ) : <EOL> table_name1 = self . _create_table ( '<STR_LIT>' ) <EOL> table_name2 = self . _create_table ( '<STR_LIT>' ) <EOL> tables = list ( self . service . list_tables ( num_results = <NUM_LIT:2> ) ) <EOL> for table in tables : <EOL> print ( table . name ) <EOL> self . service . delete_table ( table_name1 ) <EOL> self . service . delete_table ( table_name2 ) <EOL> def service_properties ( self ) : <EOL> self . service . set_table_service_properties ( logging = Logging ( delete = True ) , <EOL> hour_metrics = Metrics ( enabled = True , include_apis = True ) , <EOL> minute_metrics = Metrics ( enabled = True , include_apis = False ) , <EOL> cors = [ CorsRule ( allowed_origins = [ '<STR_LIT:*>' ] , allowed_methods = [ '<STR_LIT:GET>' ] ) ] ) <EOL> time . sleep ( <NUM_LIT:30> ) <EOL> props = self . service . get_table_service_properties ( ) <EOL> self . service . set_table_service_properties ( cors = [ ] ) <EOL> time . sleep ( <NUM_LIT:30> ) <EOL> props = self . service . get_table_service_properties ( ) </s>
