def account_sas(self):
        table_name = self._create_table()
        entity = {
            'PartitionKey': 'test',
            'RowKey': 'test1',
            'text': 'hello world',
            }
        self.service.insert_entity(table_name, entity)

        # Access to all entities in all the tables
        # Expires in an hour
        token = self.service.generate_account_shared_access_signature(
            ResourceTypes.OBJECT,
            AccountPermissions.READ,
            datetime.utcnow() + timedelta(hours=1),
        )

        # Create a service and use the SAS
        sas_service = TableService(
            account_name=self.account.account_name,
            sas_token=token,
        )

        entities = list(sas_service.query_entities(table_name))
        for entity in entities:
            print(entity.text) # hello world

        self.service.delete_table(table_name)
    def sas_with_signed_identifiers(self):
        table_name = self._create_table()
        entity = {
            'PartitionKey': 'test',
            'RowKey': 'test1',
            'text': 'hello world',
            }
        self.service.insert_entity(table_name, entity)

        # Set access policy on table
        access_policy = AccessPolicy(permission=TablePermissions.QUERY,
                                     expiry=datetime.utcnow() + timedelta(hours=1))
        identifiers = {'id': access_policy}
        acl = self.service.set_table_acl(table_name, identifiers)

        # Wait 30 seconds for acl to propagate
        time.sleep(30)

        # Indicates to use the access policy set on the table
        token = self.service.generate_table_shared_access_signature(
            table_name,
            id='id'
        )

        # Create a service and use the SAS
        sas_service = TableService(
            account_name=self.account.account_name,
            sas_token=token,
        )

        entities = list(sas_service.query_entities(table_name))
        for entity in entities:
            print(entity.text) # hello world

        self.service.delete_table(table_name)
    def test_sas_upper_case_table_name(self):
        # SAS URL is calculated from storage key, so this test runs live only
        if TestMode.need_recording_file(self.test_mode):
            return

        # Arrange
        entity = self._insert_random_entity()

        # Table names are case insensitive, so simply upper case our existing table name to test
        token = self.ts.generate_table_shared_access_signature(
            self.table_name.upper(),
            TablePermissions.QUERY,
            datetime.utcnow() + timedelta(hours=1),
            datetime.utcnow() - timedelta(minutes=1),
        )

        # Act
        service = TableService(
            account_name=self.settings.STORAGE_ACCOUNT_NAME,
            sas_token=token,
        )
        self._set_service_options(service, self.settings)
        entities = list(service.query_entities(self.table_name, 
                                               filter="PartitionKey eq '{}'".format(entity['PartitionKey'])))

        # Assert
        self.assertEqual(len(entities), 1)
        self._assert_default_entity(entities[0])
    def test_account_sas(self):
        # SAS URL is calculated from storage key, so this test runs live only
        if TestMode.need_recording_file(self.test_mode):
            return

        # Arrange
        table_name = self._create_table()
        entity = {"PartitionKey": "test", "RowKey": "test1", "text": "hello"}
        self.ts.insert_entity(table_name, entity)

        entity["RowKey"] = "test2"
        self.ts.insert_entity(table_name, entity)

        token = self.ts.generate_account_shared_access_signature(
            ResourceTypes.OBJECT,
            AccountPermissions.READ,
            datetime.utcnow() + timedelta(hours=1),
            datetime.utcnow() - timedelta(minutes=1),
        )

        # Act
        service = TableService(account_name=self.settings.STORAGE_ACCOUNT_NAME, sas_token=token)
        self._set_service_options(service, self.settings)
        entities = list(service.query_entities(table_name))

        # Assert
        self.assertEqual(len(entities), 2)
        self.assertEqual(entities[0].text, "hello")
        self.assertEqual(entities[1].text, "hello")
    def table_sas(self):
        table_name = self._create_table()
        entity = {
            'PartitionKey': 'test',
            'RowKey': 'test1',
            'text': 'hello world',
            }
        self.service.insert_entity(table_name, entity)

        # Access only to the entities in the given table
        # Query permissions to access entities
        # Expires in an hour
        token = self.service.generate_table_shared_access_signature(
            table_name,
            TablePermissions.QUERY,
            datetime.utcnow() + timedelta(hours=1),
        )

        # Create a service and use the SAS
        sas_service = TableService(
            account_name=self.account.account_name,
            sas_token=token,
        )

        entities = sas_service.query_entities(table_name)
        for entity in entities:
            print(entity.text) # hello world

        self.service.delete_table(table_name)
def get_last_run_id():
    table_service = TableService(account_name=STORAGE_ACCOUNT_NAME,
                                 account_key=STORAGE_ACCOUNT_KEY)
    databricks_cluster_details_entries = table_service.query_entities(
        'databricks', filter="PartitionKey eq 'pdm'")
    databricks_cluster_details = list(databricks_cluster_details_entries)
    if databricks_cluster_details:
        return databricks_cluster_details[0]['run_id']
    return None
def get_keywords():
    # get table service reference
    account_name = getenv('STORAGE_ACCOUNT')
    account_key = getenv('STORAGE_KEY')
    keyword_table = getenv('KEYWORD_TABLE_NAME')
    site_name = getenv('SITE_NAME')

    table_service = TableService(account_name=account_name,
                                 account_key=account_key)

    filter_str = "PartitionKey eq '%s'" % (site_name)
    # query all keyword entities
    keywords = table_service.query_entities(keyword_table, filter=filter_str)

    # TODO: automate the language detection
    # separate each keyword by language
    deKeywords = {}
    urKeywords = {}
    arKeywords = {}
    enKeywords = {}
    idKeywords = {}
    for keyword in keywords:
        canonicalKeyword = keyword.name.encode('UTF-8').lower()
        if hasattr(keyword, 'name_de'):
            # pre-compile regex for each keyword
            deKeywordRegex = create_keyword_regex(
                keyword.name_de.encode('UTF-8'))
            deKeywords[canonicalKeyword] = deKeywordRegex
        if hasattr(keyword, 'name_ur'):
            # pre-compile regex for each keyword
            urKeywordRegex = create_keyword_regex(
                keyword.name_ur.encode('UTF-8'))
            urKeywords[canonicalKeyword] = urKeywordRegex
        if hasattr(keyword, 'name_ar'):
            # pre-compile regex for each keyword
            arKeywordRegex = create_keyword_regex(
                keyword.name_ar.encode('UTF-8'))
            arKeywords[canonicalKeyword] = arKeywordRegex
        if hasattr(keyword, 'name_id'):
            # pre-compile regex for each keyword
            idKeywordRegex = create_keyword_regex(
                keyword.name_id.encode('UTF-8'))
            idKeywords[canonicalKeyword] = idKeywordRegex
        if hasattr(keyword, 'name_en'):
            # pre-compile regex for each keyword
            enKeywordRegex = create_keyword_regex(
                keyword.name_en.encode('UTF-8'))
            enKeywords[canonicalKeyword] = enKeywordRegex
    return {
        'de': deKeywords,
        'en': enKeywords,
        'id': idKeywords,
        'ur': urKeywords,
        'ar': arKeywords
    }
class SummaryTable:
    def __init__(self, account_name, account_key, table_name="summary"):
        """Initialiaze a table to store summary data. Values must be provided
        for 'account_name' and 'account_key' which are values
        associated with the Azure Storage account. 'table_name' is
        optional and is the name of the table used (and created if
        necessary) in the storage account.

        """
        self.log = Log()

        self.account_name = account_name
        self.account_key = account_key
        self.table_name = table_name

        self.createAzureTable()

    def createAzureTable(self):
        """
        Create an Azure Table in which to store the summary results.
        """
        self.table_service = TableService(self.account_name, self.account_key)
        self.table_service.create_table(self.table_name)

    def deleteTable(self, name):
        """
        Delete a table in which summary results have been stored.
        """
        self.table_service.delete_table(name, False)

    def writeCount(self, count_type, count):
        entry = {'PartitionKey': "count", "RowKey": count_type, 'total_count' : count}
        self.table_service.insert_entity(self.table_name, entry)

    def updateCount(self, count_type, count):
        entry = {'total_count' : count}
        self.table_service.update_entity(self.table_name, "count", count_type, entry)

    def getCount(self, event_type):
        """
        Get the total number of events of a given type.
        """

        count = 0
        entries = self.table_service.query_entities(self.table_name, "PartitionKey eq 'count' and RowKey eq '" + event_type + "'")

        if len(entries) == 0:
            self.writeCount(event_type, 0)
        elif len(entries) > 1:
            raise Exception('We have more than one summary entry for ' + event_type) 
        else:
            count = entries[0].total_count

        return count
Example #9
0
def get_video_ids_by_term(search_term):
    table_service = TableService(account_name=storage_acc_name,
                                 account_key=storage_acc_key)
    vid_ids = table_service.query_entities(table_name='CorpusInvertedIndex',
                                           filter='PartitionKey eq \'' +
                                           search_term + '\'',
                                           select='RowKey')
    if not vid_ids.items or len(vid_ids.items) == 0:
        return []
    video_ids = {record['RowKey'] for record in vid_ids.items}
    return video_ids
Example #10
0
def test_add_mileage(empty_tables: TableService):
    db = DBHelper(empty_tables)
    db.add_mileage_record("123", 124302)
    test_moment_key = db.get_new_key("123")
    entities = empty_tables.query_entities(
        Tables.MILEAGE,
        filter=
        f"PartitionKey eq '{Categories.MILEAGE}' and RowKey gt '{test_moment_key}' and RowKey lt '123:20000000000'"
    )
    result = [e for e in entities]
    assert len(result) == 1
    assert result[0]['mileage'] == 124302.0
Example #11
0
def test_add_gas_spending(empty_tables: TableService):
    db = DBHelper(empty_tables)
    db.add_gas_record("123", 30, CCY.BYN)
    test_moment_key = db.get_new_key("123")
    entities = empty_tables.query_entities(
        Tables.SPENDINGS,
        filter=
        f"PartitionKey eq '{Categories.GAS}' and RowKey gt '{test_moment_key}' and RowKey lt '123:20000000000'"
    )
    result = [e for e in entities]
    assert len(result) == 1
    assert db.get_float_value(result[0]['amount']) == 30.0
def get_keyword_filters():
    # get table service reference
    account_name = getenv('STORAGE_ACCOUNT')
    account_key = getenv('STORAGE_KEY')
    filter_table = getenv('FILTER_TABLE_NAME')
    table_service = TableService(account_name=account_name,
                                 account_key=account_key)
    # query all entities
    rows = table_service.query_entities(filter_table)
    # create a list of conjunct regexes
    return [[
        create_keyword_regex(term) for term in json.loads(row.filteredTerms)
    ] for row in rows]
Example #13
0
def test_add_car_goods_float(empty_tables: TableService):
    db = DBHelper(empty_tables)
    db.add_car_goods_record(123, 10.05, CCY.BYN, "огнетушитель")
    test_moment_key = db.get_new_key("123")
    entities = empty_tables.query_entities(
        Tables.SPENDINGS,
        filter=
        f"PartitionKey eq '{Categories.CAR_GOODS}' and RowKey gt '{test_moment_key}' and RowKey lt '123:20000000000'"
    )
    result = [e for e in entities]
    assert len(result) == 1
    assert result[0]['amount'] == 10.05
    assert result[0]['ccy'] == CCY.BYN
Example #14
0
def get_table_details(creds, resource_group_name, account_name, table_name, next_partition_key=None, next_row_key=None):
    keys = _get_storage_account_keys(creds, resource_group_name, account_name)
    table_service = TableService(account_name, keys.key1)

    model = StorageAccountTableDetails()
    model.table_name = table_name
    model.entities = table_service.query_entities(
        table_name,
        top=3, # small to demonstrate continuations
        next_partition_key=next_partition_key,
        next_row_key=next_row_key,
    )
    model.custom_fields = _get_entities_custom_fields(model.entities)

    return model
def connection(tasks, query, types):
    table_service = TableService(account_name=acc_name, account_key=acc_key)
    table_service.create_table('customer')
    if types == "insert":
        print(tasks)
        table_service.insert_entity('customer', tasks)
        return "added Successfully!!!!"
    elif types == "reterive":
        #print(query)
        tasks = table_service.query_entities('customer', filter=query)
        #print(tasks)
        df = pd.DataFrame(df_con(tasks))
        data1 = df.to_json(orient='records')
        #print(data1)
        return data1
Example #16
0
def get_table_details(creds, resource_group_name, account_name, table_name, next_partition_key=None, next_row_key=None):
    keys = _get_storage_account_keys(creds, resource_group_name, account_name)
    table_service = TableService(account_name, keys.key1)

    model = StorageAccountTableDetails()
    model.table_name = table_name
    model.entities = table_service.query_entities(
        table_name,
        top=3,  # small to demonstrate continuations
        next_partition_key=next_partition_key,
        next_row_key=next_row_key,
    )
    model.custom_fields = _get_entities_custom_fields(model.entities)

    return model
Example #17
0
class Repository(object):
    """Azure Table storage repository for UOTD."""

    def __init__(self, settings):
        """Initialise UOTD repository with the given settings dict.

        Required settings:
        STORAGE_NAME -- the Azure Storage account name
        STORAGE_KEY -- an access key for the Storage account
        STORAGE_TABLE_UOTD -- the name of the table
        """
        self.service = TableService(settings["STORAGE_NAME"],
                                    settings["STORAGE_KEY"])
        self.uotd_table = settings["STORAGE_TABLE_UOTD"]
        self.service.create_table(self.uotd_table)
        self.partition_key_format = "%Y%m"
        self.row_key_format = "%d"

    def get_uotd(self):
        """Get the UUID for the current day.

        If the UUID does not yet exist then it will be created.
        """
        partition_key = date.today().strftime(self.partition_key_format)
        row_key = date.today().strftime(self.row_key_format)

        try:
            uotd_entity = self.service.get_entity(self.uotd_table,
                                                  partition_key,
                                                  row_key)
            uuid = uotd_entity.uuid
        except AzureMissingResourceHttpError:
            uuid = str(uuid4())
            uotd_entity = {
                "PartitionKey": partition_key,
                "RowKey": row_key,
                "uuid": uuid
            }
            self.service.insert_entity(self.uotd_table, uotd_entity)

        return uuid

    def get_uuids(self, partition_key):
        """Get all the UUIDs in a given partition."""
        filter = "PartitionKey eq '{0}'".format(partition_key)
        entities = self.service.query_entities(self.uotd_table, filter=filter)
        return entities
Example #18
0
def get_data(block_id, sas_url):

    table_service = TableService(azure_storage_account_name, azure_storage_account_key)

    tasks = table_service.query_entities('Parking1hour1BlockAverage', 
                                         filter="PartitionKey eq '%d' and RowKey gt '2019-01-01 00:00:00'" % (1001), 
                                         select='RowKey,PercentOccupied')
    pct_occupieds = []
    hours = []
    for task in tasks:
        pct_occupieds.append(task.PercentOccupied)
        hours.append(datetime.strptime(task.RowKey, "%Y-%m-%d %H:%M:%S").hour)

    time_chunks_per_day = np.max(hours) - np.min(hours) + 1


    run_arima( np.array(pct_occupieds), time_chunks_per_day )
Example #19
0
def main(req: func.HttpRequest) -> func.HttpResponse:

    try:
        logging.info("Trigger started")

        ret = {}

        if "code" not in req.params:
            logging.info("Invalid code")

            ret["message"] = "The parameter code is no present in the request."
            ret["status"] = False

            return func.HttpResponse(json.dumps(ret), headers=headers)
        else:
            code = req.params.get('code')

            logging.info("Processing " + str(code) + "...")

            table_service = TableService(account_name=ACCOUNT_NAME,
                                         account_key=ACCOUNT_KEY)
            records = table_service.query_entities(
                TABLE_NAME_TRACKING,
                filter="PartitionKey eq 'tracking-analysis' and RowKey eq '" +
                code + "'")

            if len(records.items) == 0:
                ret["message"] = "Meeting coding not found"
                ret["status"] = False
                logging.info("Code not found.")
            else:
                ret["message"] = "Code has been found"
                ret["status"] = True
                logging.info("Code has been found.")

            return func.HttpResponse(json.dumps(ret), headers=headers)

    except Exception as error:
        logging.error(error)
        return func.HttpResponse(error, status_code=400, headers=headers)
Example #20
0
def busca_heroe(nombre):
    account_name = os.environ['AZURE_STORAGE_ACCOUNT']
    account_key = os.environ['AZURE_STORAGE_ACCESS_KEY']
    table_service = TableService(account_name, account_key)

    filter = "PartitionKey eq " + "\'" + nombre + "\'"

    consulta = table_service.query_entities('heroes', filter)

    salida = {}

    for heroe in consulta:
        for key in heroe.__dict__.keys():
            salida[key] = str(heroe.__dict__[key])

    salida['Nombre'] = salida['PartitionKey']
    salida['Grupo'] = salida['RowKey']
    del salida['etag']
    del salida['PartitionKey']
    del salida['RowKey']
    del salida['Timestamp']

    return salida
    def test_account_sas(self):
        # SAS URL is calculated from storage key, so this test runs live only
        if TestMode.need_recording_file(self.test_mode):
            return

        # Arrange
        table_name = self._create_table()
        entity = {
            'PartitionKey': 'test',
            'RowKey': 'test1',
            'text': 'hello',
        }
        self.ts.insert_entity(table_name, entity)

        entity['RowKey'] = 'test2'
        self.ts.insert_entity(table_name, entity)

        token = self.ts.generate_account_shared_access_signature(
            ResourceTypes.OBJECT,
            AccountPermissions.READ,
            datetime.utcnow() + timedelta(hours=1),
            datetime.utcnow() - timedelta(minutes=1),
        )

        # Act
        service = TableService(
            account_name=self.settings.STORAGE_ACCOUNT_NAME,
            sas_token=token,
        )
        self._set_test_proxy(service, self.settings)
        entities = list(service.query_entities(table_name))

        # Assert
        self.assertEqual(len(entities), 2)
        self.assertEqual(entities[0].text, 'hello')
        self.assertEqual(entities[1].text, 'hello')
table_service.insert_entity('itemstable', coffee)
print('Created entry for CoffeeBean...\n')
time.sleep(1)

###
# Use the Azure Storage Storage SDK for Python to query for entities in our Table
###
print(
    'With some data in our Azure Storage Table, we can query the data.\nLet\'s see what the pizza menu looks like.'
)
raw_input('Press Enter to continue...')

# In this query, you define the partition key to search within, and then which properties to retrieve
# Structuring queries like this improves performance as your application scales up and keeps the queries efficient
items = table_service.query_entities('itemstable',
                                     filter="PartitionKey eq 'Cargallery'",
                                     select='make,model,year,color,price')
for item in items:
    print('Make: ' + item.make)
    print('Model: ' + item.model)
    print('Year: ' + item.year)
    print('Color: ' + item.color)
    print('Price: ' + str(item.price) + '\n')

items = table_service.query_entities('itemstable',
                                     filter="PartitionKey eq 'Coffeemenu'",
                                     select='brand,flavor,cupsize,price')
for item in items:
    print('Brand: ' + item.brand)
    print('Flavor: ' + item.flavor)
    print('Cupsize: ' + item.cupsize)
Example #23
0
class TableStorageHandlerTest(_TestCase):

    def _divide_key(self, key):
        divided = []
        hostname = gethostname()
        if key.find(hostname) >= 0:
            preceding, hostname, remaining = key.rpartition(hostname)
            preceding = preceding[:-1] if preceding.endswith('-') else preceding
            divided.extend(preceding.split('-'))
            divided.append(hostname)
            remaining = remaining[1:] if remaining.startswith('-') else remaining
            divided.extend(remaining.split('-'))
        else:
            divided.extend(key.split('-'))
        return iter(divided)

    def _get_formatter_name(self, handler_name, formatter_type):
        name = _get_handler_config_value(handler_name, formatter_type)
        if name:
            if name.startswith('cfg://formatters.'):
                name = name.split('.')[1]
        return name

    def _get_partition_key_formatter_name(self, handler_name):
        return self._get_formatter_name(handler_name, 'partition_key_formatter')

    def _get_row_key_formatter_name(self, handler_name):
        return self._get_formatter_name(handler_name, 'row_key_formatter')

    def setUp(self):
        self.service = TableService(account_name=ACCOUNT_NAME,
                                    account_key=ACCOUNT_KEY,
                                    is_emulated=_EMULATED)
        # ensure that there's no entity in the table before each test
        tables = set()
        for cfg in LOGGING['handlers'].values():
            if 'table' in cfg:
                tables.add(cfg['table'])
        for table in self.service.list_tables():
            if table.name in tables:
                for entity in self.service.query_entities(table.name):
                    self.service.delete_entity(table.name,
                                               entity.PartitionKey,
                                               entity.RowKey)

    def test_logging(self):
        # get the logger for the test
        logger_name = 'table'
        logger = logging.getLogger(logger_name)
        handler_name = _get_handler_name(logger_name)

        # perform logging
        log_text = 'logging test'
        logging_started = datetime.now()
        logger.info(log_text)
        logging_finished = datetime.now()

        # confirm that the entity has correct log text
        table = _get_handler_config_value(handler_name, 'table')
        entities = iter(self.service.query_entities(table))
        entity = next(entities)
        self.assertEqual(entity.message, 'INFO %s' % log_text)

        # confirm that the entity has the default partitiok key
        fmt = '%Y%m%d%H%M'
        try:
            self.assertEqual(entity.PartitionKey, logging_started.strftime(fmt))
        except AssertionError:
            if logging_started == logging_finished:
                raise
            self.assertEqual(entity.PartitionKey, logging_finished.strftime(fmt))

        # confirm that the entity has the default row key
        divided = self._divide_key(entity.RowKey)
        timestamp = next(divided)
        fmt = '%Y%m%d%H%M%S'
        self.assertGreaterEqual(timestamp[:-3], logging_started.strftime(fmt))
        self.assertLessEqual(timestamp[:-3], logging_finished.strftime(fmt))
        self.assertRegex(timestamp[-3:], '^[0-9]{3}$')
        self.assertEqual(next(divided), gethostname())
        self.assertEqual(int(next(divided)), os.getpid())
        self.assertEqual(next(divided), '00')
        with self.assertRaises(StopIteration):
            next(divided)

        # confirm that there's no more entity in the table
        with self.assertRaises(StopIteration):
            next(entities)

    @unittest.skipIf(_EMULATED, "Azure Storage Emulator doesn't support batch operation.")
    def test_batch(self):
        # get the logger for the test
        logger_name = 'batch'
        logger = logging.getLogger(logger_name)
        handler_name = _get_handler_name(logger_name)

        # perform logging and execute  the first batch
        batch_size = _get_handler_config_value(handler_name, 'batch_size')
        log_text = 'batch logging test'
        for i in range(batch_size + int(batch_size/2)):
            logger.info('%s#%02d' % (log_text, i))

        # confirm that only batch_size entities are committed at this point
        table = _get_handler_config_value(handler_name, 'table')
        entities = list(iter(self.service.query_entities(table)))
        self.assertEqual(len(entities), batch_size)
        rowno_found = set()
        seq_found = set()
        for entity in entities:
            # partition key
            self.assertEqual(entity.PartitionKey, 'batch-%s' % gethostname())
            # row key
            rowno = entity.RowKey.split('-')[-1]
            self.assertLess(int(rowno), batch_size)
            self.assertNotIn(rowno, rowno_found)
            rowno_found.add(rowno)
            # message
            message, seq = entity.message.split('#')
            self.assertEqual(message, 'INFO %s' % log_text)
            self.assertLess(int(seq), batch_size)
            self.assertNotIn(seq, seq_found)
            seq_found.add(seq)

        # remove currently created entities before the next batch
        for entity in entities:
            self.service.delete_entity(table,
                                       entity.PartitionKey,
                                       entity.RowKey)

        # perform logging again and execute the next batch
        for j in range(i+1, int(batch_size/2)+i+1):
            logger.info('%s#%02d' % (log_text, j))

        # confirm that the remaining entities are committed in the next batch
        entities = list(iter(self.service.query_entities(table)))
        self.assertEqual(len(entities), batch_size)
        rowno_found.clear()
        for entity in entities:
            # partition key
            self.assertEqual(entity.PartitionKey, 'batch-%s' % gethostname())
            # row key
            rowno = entity.RowKey.split('-')[-1]
            self.assertLess(int(rowno), batch_size)
            self.assertNotIn(rowno, rowno_found)
            rowno_found.add(rowno)
            # message
            message, seq = entity.message.split('#')
            self.assertEqual(message, 'INFO %s' % log_text)
            self.assertGreaterEqual(int(seq), batch_size)
            self.assertLess(int(seq), batch_size*2)
            self.assertNotIn(seq, seq_found)
            seq_found.add(seq)

    def test_extra_properties(self):
        # get the logger for the test
        logger_name = 'extra_properties'
        logger = logging.getLogger(logger_name)
        handler_name = _get_handler_name(logger_name)
        
        # perform logging
        log_text = 'extra properties test'
        logger.info(log_text)

        # confirm that the entity has correct log text
        table = _get_handler_config_value(handler_name, 'table')
        entities = iter(self.service.query_entities(table))
        entity = next(entities)
        self.assertEqual(entity.message, 'INFO %s' % log_text)

        # confirm that the extra properties have correct values
        entity = next(iter(self.service.query_entities(table)))
        self.assertEqual(entity.hostname, gethostname())
        self.assertEqual(entity.levelname, 'INFO')
        self.assertEqual(int(entity.levelno), logging.INFO)
        self.assertEqual(entity.module, os.path.basename(__file__).rpartition('.')[0])
        self.assertEqual(entity.name, logger_name)
        self.assertEqual(int(entity.process), os.getpid())
        self.assertEqual(int(entity.thread), current_thread().ident)

        # confirm that there's no more entity in the table
        with self.assertRaises(StopIteration):
            next(entities)

    def test_custom_key_formatters(self):
        # get the logger for the test
        logger_name = 'custom_keys'
        logger = logging.getLogger(logger_name)
        handler_name = _get_handler_name(logger_name)

        # perform logging
        log_text = 'custom key formatters test'
        logging_started = datetime.now()
        logger.info(log_text)
        logging_finished = datetime.now()

        # confirm that the entity correct log text
        table = _get_handler_config_value(handler_name, 'table')
        entities = iter(self.service.query_entities(table))
        entity = next(entities)
        self.assertEqual(entity.message, 'INFO %s' % log_text)

        # confirm that the entity has a custom partitiok key
        divided = self._divide_key(entity.PartitionKey)
        self.assertEqual(next(divided), 'mycustompartitionkey')
        self.assertEqual(next(divided), gethostname())
        formatter_name = self._get_partition_key_formatter_name(handler_name)
        fmt = _get_formatter_config_value(formatter_name, 'datefmt')
        asctime = next(divided)
        try:
            self.assertEqual(asctime, logging_started.strftime(fmt))
        except AssertionError:
            if logging_started == logging_finished:
                raise
            self.assertEqual(asctime, logging_finished.strftime(fmt))
        with self.assertRaises(StopIteration):
            next(divided)

        # confirm that the entity has a custom row key
        divided = self._divide_key(entity.RowKey)
        self.assertEqual(next(divided), 'mycustomrowkey')
        self.assertEqual(next(divided), gethostname())
        formatter_name = self._get_row_key_formatter_name(handler_name)
        fmt = _get_formatter_config_value(formatter_name, 'datefmt')
        asctime = next(divided)
        try:
            self.assertEqual(asctime, logging_started.strftime(fmt))
        except AssertionError:
            if logging_started == logging_finished:
                raise
            self.assertEqual(asctime, logging_finished.strftime(fmt))
        with self.assertRaises(StopIteration):
            next(divided)

        # confirm that there's no more entity in the table
        with self.assertRaises(StopIteration):
            next(entities)
Example #24
0
class StorageTableContext():
    """Initializes the repository with the specified settings dict.
        Required settings in config dict are:
        - AZURE_STORAGE_NAME
        - STORAGE_KEY
    """
    
    _models = []
    _encryptproperties = False
    _encrypted_properties = []
    _tableservice = None
    _storage_key = ''
    _storage_name = ''

    def __init__(self, **kwargs):

        self._storage_name = kwargs.get('AZURE_STORAGE_NAME', '')
        self._storage_key = kwargs.get('AZURE_STORAGE_KEY', '')

        """ service init """
        self._models = []
        if self._storage_key != '' and self._storage_name != '':
            self._tableservice = TableService(account_name = self._storage_name, account_key = self._storage_key, protocol='https')

        """ encrypt queue service """
        if kwargs.get('AZURE_REQUIRE_ENCRYPTION', False):

            # Create the KEK used for encryption.
            # KeyWrapper is the provided sample implementation, but the user may use their own object as long as it implements the interface above.
            kek = KeyWrapper(kwargs.get('AZURE_KEY_IDENTIFIER', 'otrrentapi'), kwargs.get('SECRET_KEY', 'super-duper-secret')) # Key identifier

            # Create the key resolver used for decryption.
            # KeyResolver is the provided sample implementation, but the user may use whatever implementation they choose so long as the function set on the service object behaves appropriately.
            key_resolver = KeyResolver()
            key_resolver.put_key(kek)

            # Set the require Encryption, KEK and key resolver on the service object.
            self._encryptproperties = True
            self._tableservice.key_encryption_key = kek
            self._tableservice.key_resolver_funcion = key_resolver.resolve_key
            self._tableservice.encryption_resolver_function = self.__encryptionresolver__


        pass

    def __createtable__(self, tablename) -> bool:
        if (not self._tableservice is None):
            try:
                self._tableservice.create_table(tablename)
                return True
            except AzureException as e:
                log.error('failed to create {} with error {}'.format(tablename, e))
                return False
        else:
            return True
        pass

    # Define the encryption resolver_function.
    def __encryptionresolver__(self, pk, rk, property_name):
        if property_name in self._encrypted_properties:
            return True
            #log.debug('encrypt field {}'.format(property_name))
        
        #log.debug('dont encrypt field {}'.format(property_name))
        return False

    def register_model(self, storagemodel:object):
        modelname = storagemodel.__class__.__name__     
        if isinstance(storagemodel, StorageTableModel):
            if (not modelname in self._models):
                self.__createtable__(storagemodel._tablename)
                self._models.append(modelname)

                """ set properties to be encrypted client side """
                if self._encryptproperties:
                    self._encrypted_properties += storagemodel._encryptedproperties

                log.info('model {} registered successfully. Models are {!s}. Encrypted fields are {!s} '.format(modelname, self._models, self._encrypted_properties))      
        pass

    def table_isempty(self, tablename, PartitionKey='', RowKey = '') -> bool:
        if  (not self._tableservice is None):

            filter = "PartitionKey eq '{}'".format(PartitionKey) if PartitionKey != '' else ''
            if filter == '':
                filter = "RowKey eq '{}'".format(RowKey) if RowKey != '' else ''
            else:
                filter = filter + ("and RowKey eq '{}'".format(RowKey) if RowKey != '' else '')
            try:
                entities = list(self._tableservice.query_entities(tablename, filter = filter, select='PartitionKey', num_results=1))
                if len(entities) == 1: 
                    return False
                else:
                    return True

            except AzureMissingResourceHttpError as e:
                log.debug('failed to query {} with error {}'.format(tablename, e))
                return True

        else:
            return True
        pass

    def exists(self, storagemodel) -> bool:
        exists = False
        if isinstance(storagemodel, StorageTableModel):
            modelname = storagemodel.__class__.__name__
            if (modelname in self._models):
                if storagemodel._exists is None:
                    try:
                        entity = self._tableservice.get_entity(storagemodel._tablename, storagemodel.PartitionKey, storagemodel.RowKey)
                        storagemodel._exists = True
                        exists = True
            
                    except AzureMissingResourceHttpError:
                        storagemodel._exists = False
                else:
                    exists = storagemodel._exists
            else:
                log.debug('please register model {} first'.format(modelname))
                        
        return exists       

    def get(self, storagemodel) -> StorageTableModel:
        """ load entity data from storage to vars in self """

        if isinstance(storagemodel, StorageTableModel):
            modelname = storagemodel.__class__.__name__
            if (modelname in self._models):
                try:
                    entity = self._tableservice.get_entity(storagemodel._tablename, storagemodel.PartitionKey, storagemodel.RowKey)
                    storagemodel._exists = True
        
                    """ sync with entity values """
                    for key, default in vars(storagemodel).items():
                        if not key.startswith('_') and key not in ['','PartitionKey','RowKey']:
                            value = getattr(entity, key, None)
                            if not value is None:
                                setattr(storagemodel, key, value)
             
                except AzureMissingResourceHttpError as e:
                    log.debug('can not get table entity:  Table {}, PartitionKey {}, RowKey {} because {!s}'.format(storagemodel._tablename, storagemodel.PartitionKey, storagemodel.RowKey, e))
                    storagemodel._exists = False

                except Exception as e:
                    log.debug('can not get table entity:  Table {}, PartitionKey {}, RowKey {} because {!s}'.format(storagemodel._tablename, storagemodel.PartitionKey, storagemodel.RowKey, e))
                    storagemodel._exists = False

            else:
                log.debug('please register model {} first to {!s}'.format(modelname, self._models))

            return storagemodel

        else:
            return None

    def insert(self, storagemodel) -> StorageTableModel:
        """ insert model into storage """
        if isinstance(storagemodel, StorageTableModel):
            modelname = storagemodel.__class__.__name__
            if (modelname in self._models):
                try:            
                    self._tableservice.insert_or_replace_entity(storagemodel._tablename, storagemodel.entity())
                    storagemodel._exists = True

                except AzureMissingResourceHttpError as e:
                    log.debug('can not insert or replace table entity:  Table {}, PartitionKey {}, RowKey {} because {!s}'.format(storagemodel._tablename, storagemodel.PartitionKey, storagemodel.RowKey, e))
            else:
                log.debug('please register model {} first'.format(modelname))

            return storagemodel
        else:
            return None

    def merge(self, storagemodel) -> StorageTableModel:
        """ try to merge entry """
        if isinstance(storagemodel, StorageTableModel):
            modelname = storagemodel.__class__.__name__
            if (modelname in self._models):
                try:            
                    self._tableservice.insert_or_merge_entity(storagemodel._tablename, storagemodel.entity())
                    storagemodel._exists = True

                except AzureMissingResourceHttpError as e:
                    log.debug('can not insert or merge table entity:  Table {}, PartitionKey {}, RowKey {} because {!s}'.format(storagemodel._tablename, storagemodel.PartitionKey, storagemodel.RowKey, e))
            else:
                log.debug('please register model {} first'.format(modelname))

            return storagemodel
        else:
            return None
    
    def delete(self,storagemodel):
        """ delete existing Entity """
        if isinstance(storagemodel, StorageTableModel):
            modelname = storagemodel.__class__.__name__
            if (modelname in self._models):
                try:
                    self._tableservice.delete_entity(storagemodel._tablename, storagemodel.PartitionKey, storagemodel.RowKey)
                    storagemodel._exists = False

                except AzureMissingResourceHttpError as e:
                    log.debug('can not delete table entity:  Table {}, PartitionKey {}, RowKey {} because {!s}'.format(storagemodel._tablename, storagemodel.PartitionKey, storagemodel.RowKey, e))

            else:
                log.debug('please register model {} first'.format(modelname))

            return storagemodel
        else:
            return None


    def __changeprimarykeys__(self, PartitionKey = '', RowKey = ''):
        """ Change Entity Primary Keys into new instance:

            - PartitionKey and/or
            - RowKey
        """

        PartitionKey = PartitionKey if PartitionKey != '' else self._PartitionKey
        RowKey = RowKey if RowKey != '' else self._RowKey

        """ change Primary Keys if different to existing ones """
        if (PartitionKey != self._PartitionKey) or (RowKey != self._RowKey):
            return True, PartitionKey, RowKey
        else:
            return False, PartitionKey, RowKey
        pass
            
    def moveto(self, PartitionKey = '', RowKey = ''):
        """ Change Entity Primary Keys and move in Storage:

            - PartitionKey and/or
            - RowKey
        """
        changed, PartitionKey, RowKey = self.__changeprimarykeys__(PartitionKey, RowKey)

        if changed:

            """ sync self """
            new = self.copyto(PartitionKey, RowKey)
            new.save()

            """ delete Entity if exists in Storage """
            self.delete()

    def copyto(self, PartitionKey = '', RowKey = '') -> object:
        """ Change Entity Primary Keys and copy to new Instance:

            - PartitionKey and/or
            - RowKey
        """
        changed, PartitionKey, RowKey = self.__changeprimarykeys__(PartitionKey, RowKey)

        self.load()
        new = self
        new._PartitionKey = PartitionKey
        new._RowKey = RowKey
        new.load()

        return new

    def query(self, storagecollection) -> StorageTableCollection:
        if isinstance(storagecollection, StorageTableCollection):
            try:
                storagecollection.extend(self._tableservice.query_entities(storagecollection._tablename,storagecollection._filter))

            except AzureMissingResourceHttpError as e:
                log.debug('can not query table {} with filters {} because {!s}'.format(storagecollection._tablename, storagecollection._filter, e))            

            return storagecollection
        else:
            return None
Example #25
0
j=1
while True:
    # wait time in sec's
    time.sleep(90)
    #import table from storage as csv file

    import azure
    from azure.storage.table import TableService, Entity
    from azure.storage.table.models import EntityProperty
    import csv

    key ='******************************d/Xg8j1ITmmGg=='
    table_service = TableService(account_name='intelligentappstorage01', account_key=key)

    
    rows = table_service.query_entities('transcriptiontable') 
        
            

    with open('chunks'+meetingID+'.csv', 'w') as f:  
        w = None

        for a_row in rows:
            if w == None:
                w = csv.writer(f, lineterminator='\n')
                w.writerow(a_row.keys())
            row = []
            for key, value in a_row.items():
                actual_value = value
                if isinstance(value, EntityProperty):
                    actual_value = value.value
clothing.sku = 'BLK203143'
clothing.item = 'jeans'
clothing.cost = 55.99
table_service.insert_entity('itemstable', clothing)
print('Created entry for Jeans...\n')
time.sleep(1)

###
# Use the Azure Storage Storage SDK for Python to query for entities in our Table
###
print('With some data in our Azure Storage Table, we can query the data.\nLet\'s see what the pizza menu looks like.')
raw_input('Press Enter to continue...')

# In this query, you define the partition key to search within, and then which properties to retrieve
# Structuring queries like this improves performance as your application scales up and keeps the queries efficient
items = table_service.query_entities('itemstable', filter="PartitionKey eq 'pizzamenu'", select='description,cost')
for item in items:
    print('Name: ' + item.description)
    print('Cost: ' + str(item.cost) + '\n')

items = table_service.query_entities('itemstable', filter="PartitionKey eq 'clothingstore'", select='description,price')
for item in items:
    print('Name: ' + item.description)
    print('Price: ' + str(item.price) + '\n')

time.sleep(1)


###
# This was a quick demo to see Tables in action.
# Although the actual cost is minimal (fractions of a cent per month) for the three entities we created, it's good to clean up resources when you're done
Example #27
0
table_service.insert_entity('itemstable', coffee)
print('Created entry for a StarBucks Vanilla...\n')
time.sleep(1)



###
# Use the Azure Storage Storage SDK for Python to query for entities in our Table
###
print('With some data in our Azure Storage Table, we can query the data.\nLet\'s see what the Car Info looks like.')
raw_input('Press Enter to continue...')

# In this query, you define the partition key to search within, and then which properties to retrieve
# Structuring queries like this improves performance as your application scales up and keeps the queries efficient
items = table_service.query_entities('itemstable', filter="PartitionKey eq 'carinfo'", select='make,model')
for item in items:
    print('Make: ' + item.make)
    print('Model: ' + str(item.model) + '\n')

items = table_service.query_entities('itemstable', filter="PartitionKey eq 'coffeeinv'", select='brand,flavor')
for item in items:
    print('Brand: ' + item.brand)
    print('Flavor: ' + str(item.flavor) + '\n')

time.sleep(1)


###
# This was a quick demo to see Tables in action.
# Although the actual cost is minimal (fractions of a cent per month) for the three entities we created, it's good to clean up resources when you're done
class Repository(object):
    """Azure Table Storage repository."""
    def __init__(self, settings):
        """Initializes the repository with the specified settings dict.
        Required settings are:
         - STORAGE_NAME
         - STORAGE_KEY
         - STORAGE_TABLE_POLL
         - STORAGE_TABLE_CHOICE
        """
        self.name = 'Azure Table Storage'
        self.storage_name = settings['STORAGE_NAME']
        self.storage_key = settings['STORAGE_KEY']
        self.poll_table = settings['STORAGE_TABLE_POLL']
        self.choice_table = settings['STORAGE_TABLE_CHOICE']

        self.svc = TableService(self.storage_name, self.storage_key)
        self.svc.create_table(self.poll_table)
        self.svc.create_table(self.choice_table)

    def get_polls(self):
        """Returns all the polls from the repository."""
        poll_entities = self.svc.query_entities(self.poll_table)
        polls = [_poll_from_entity(entity) for entity in poll_entities]
        return polls

    def get_poll(self, poll_key):
        """Returns a poll from the repository."""
        try:
            partition, row = _key_to_partition_and_row(poll_key)
            poll_entity = self.svc.get_entity(self.poll_table, partition, row)
            choice_entities = self.svc.query_entities(
                self.choice_table,
                "PollPartitionKey eq '{0}' and PollRowKey eq '{1}'" \
                    .format(partition, row)
            )

            poll = _poll_from_entity(poll_entity)
            poll.choices = [_choice_from_entity(choice_entity)
                            for choice_entity in choice_entities]
            return poll
        except AzureMissingResourceHttpError:
            raise PollNotFound()

    def increment_vote(self, poll_key, choice_key):
        """Increment the choice vote count for the specified poll."""
        try:
            partition, row = _key_to_partition_and_row(choice_key)
            entity = self.svc.get_entity(self.choice_table, partition, row)
            entity.Votes += 1
            self.svc.update_entity(self.choice_table, entity)
        except AzureMissingResourceHttpError:
            raise PollNotFound()

    def add_sample_polls(self):
        """Adds a set of polls from data stored in a samples.json file."""
        poll_partition = '2014'
        poll_row = 0
        choice_partition = '2014'
        choice_row = 0

        for sample_poll in _load_samples_json():
            poll_entity = {
                'PartitionKey': poll_partition,
                'RowKey': str(poll_row),
                'Text': sample_poll['text'],
            }
            self.svc.insert_entity(self.poll_table, poll_entity)

            for sample_choice in sample_poll['choices']:
                choice_entity = {
                    'PartitionKey': choice_partition,
                    'RowKey': str(choice_row),
                    'Text': sample_choice,
                    'Votes': 0,
                    'PollPartitionKey': poll_partition,
                    'PollRowKey': str(poll_row),
                }
                self.svc.insert_entity(self.choice_table, choice_entity)
                choice_row += 1

            poll_row += 1
table_service.insert_entity('itemstable', coffee)
print('Created entry for Costacoffee...\n')
time.sleep(1)

###
# Use the Azure Storage Storage SDK for Python to query for entities in our Table
###
print(
    'With some data in our Azure Storage Table, we can query the data.\nLet\'s see what the car menu looks like.'
)
raw_input('Press Enter to continue...')

# In this query, you define the partition key to search within, and then which properties to retrieve
# Structuring queries like this improves performance as your application scales up and keeps the queries efficient
items = table_service.query_entities('itemstable',
                                     filter="PartitionKey eq 'pizzamenu'",
                                     select='description,cost')
for item in items:
    print('Name: ' + item.description)
    print('Cost: ' + str(item.cost) + '\n')

items = table_service.query_entities('itemstable',
                                     filter="PartitionKey eq 'cardealer'",
                                     select='make,cost')
for item in items:
    print('Name: ' + item.make)
    print('Price: ' + str(item.cost) + '\n')

items = table_service.query_entities('itemstable',
                                     filter="PartitionKey eq 'clothingstore'",
                                     select='item,cost')
Example #30
0
coffee.sizeofcup = 'Grande'
coffee.price = 5.50
table_service.insert_entity('itemstable', coffee)
print('Created entry for a cafe Latte...\n')
time.sleep(1)


###
# Use the Azure Storage Storage SDK for Python to query for entities in our Table
###
print('With some data in our Azure Storage Table, we can query the data.\nLet\'s see what the car delaership looks like.')
raw_input('Press Enter to continue...')

# In this query, you define the partition key to search within, and then which properties to retrieve
# Structuring queries like this improves performance as your application scales up and keeps the queries efficient
items = table_service.query_entities('itemstable', filter="PartitionKey eq 'Cardealership'", select='make,model,year,color,price')
for item in items:
    print('Make: ' + item.make)
    print('Model: ' + item.model)
    print('Year: ' + item.year)
    print('Color: ' + item.color)
    print('Price: ' + str(item.price) + '\n')

items = table_service.query_entities('itemstable', filter="PartitionKey eq 'coffeeshop'", select='brand,flavor,sizeofcup,price')
for item in items:
    print('Brand: ' + item.brand)
    print('flavor: ' + item.flavor)
    print('sizeofcup: ' + item.sizeofcup)
    print('PricePerCup: ' + str(item.price) + '\n')

time.sleep(1)
Example #31
0
table_service.update_entity('tasktable', 'tasksSeattle', '1', task)
#
task = {'description' : 'Take out the garbage again', 'priority' : 250}
table_service.insert_or_replace_entity('tasktable', 'tasksSeattle', '1', task)

task = {'description' : 'Buy detergent', 'priority' : 300}
table_service.insert_or_replace_entity('tasktable', 'tasksSeattle', '3', task)

task10 = {'PartitionKey': 'tasksSeattle', 'RowKey': '10', 'description' : 'Go grocery shopping', 'priority' : 400}
task11 = {'PartitionKey': 'tasksSeattle', 'RowKey': '11', 'description' : 'Clean the bathroom', 'priority' : 100}
table_service.begin_batch()
table_service.insert_entity('tasktable', task10)
table_service.insert_entity('tasktable', task11)
table_service.commit_batch()

task = table_service.get_entity('tasktable', 'tasksSeattle', '1')
print(task.description)
print(task.priority)

tasks = table_service.query_entities('tasktable', "PartitionKey eq 'tasksSeattle'")
for task in tasks:
    print(task.description)
    print(task.priority)
    
tasks = table_service.query_entities('tasktable', "PartitionKey eq 'tasksSeattle'", 'description')
for task in tasks:
    print(task.description)
    
table_service.delete_entity('tasktable', 'tasksSeattle', '1')
table_service.delete_table('tasktable')
Example #32
0



sender = '*****@*****.**'
#receiver = '*****@*****.**'
receiver = '*****@*****.**'
msg = email.message.Message()
msg['From'] = sender
msg['To'] = receiver
msg.add_header('Content-Type','text/html')
past_time = 0
while True:
    test = "finishtransmissiontime gt %d" % past_time
    #print test
    tasks = table_service.query_entities('StreamOutputTable1', filter=test)
    if len(tasks)<1: continue
    print 'length of records found:', str(len(tasks))
    send_mails = []
    for record in tasks:
        device_temparature = float(record.temp)
        device_check = check_device_status(record)
        if device_check:
            send_mails.append(device_check)
        past_time = record.finishtransmissiontime
    if len(send_mails)<1:
        print 'looking for another batch of emails'
        continue
    print 'trying to login'
    #mail = getmail_data(sender)
    print 'successfully logged in'
coffee.brand = 'Folgers'
coffee.flavor = 'Hazelnut'
coffee.cupsize = '30OZ'
coffee.cost = 5.99
table_service.insert_entity('itemstable', coffee)
print('Created entry for Hazelnut...\n')
time.sleep(1)
###
# Use the Azure Storage Storage SDK for Python to query for entities in our Table
###
print('With some data in our Azure Storage Table, we can query the data.\n’
print(‘Let\'s see what the cars menu looks like.')
raw_input('Press Enter to continue...')
# In this query, you define the partition key to search within, and then which properties to retrieve
# Structuring queries like this improves performance as your application scales up and keeps the queries efficient
items = table_service.query_entities('itemstable', filter="PartitionKey eq 'carsmenu'", select='make,model,year,color,price')
for item in items:
print('Name: ' + item.description)
print('Cost: ' + str(item.cost) + '\n')
items = table_service.query_entities('itemstable', filter="PartitionKey eq 'coffeestore'", select='flavor,cupsize,cost')
for item in items:
print('Name: ' + item.description)
print('Price: ' + str(item.price) + '\n')
time.sleep(1)
## This was a quick demo to see Tables in action.
# Although the actual cost is minimal (fractions of a cent per month) for the three entities we created,
# it's good to clean up resources when you're done
###
print('\nWe should clean up the Azure Storage resources we created.')
raw_input('Press Enter to continue...')
response = table_service.delete_table('itemstable')
Example #34
0
coffee.RowKey = '1001'
coffee.brand = 'Regular'
coffee.flavor = 'Hazelnut'
coffee.size = 'Small'
coffee.price = 5
table_service.insert_entity('itemstable', coffee)
print('Created entry for Regular Hazelnut...')
###
# Use the Azure Storage Storage SDK for Python to query for entities in our Table
###
print('With some data in our Azure Storage Table, we can query the data.\nLet\'s see what the car menu looks like.')
raw_input('Press Enter to continue...')

# In this query, you define the partition key to search within, and then which properties to retrieve
# Structuring queries like this improves performance as your application scales up and keeps the queries efficient
items = table_service.query_entities('itemstable', filter="PartitionKey eq 'carmenu'", select='make,model,year,price')
for item in items:
    print(' Make: ' + item.make)
	print(' Model: ' + item.model)
	print(' Year: ' + item.year)
    print(' Price: ' + str(item.price) + '\n')

items = table_service.query_entities('itemstable', filter="PartitionKey eq 'coffeemenu'", select='brand,price')
for item in items:
    print(' Brand: ' + item.brand)
    print('Price: ' + str(item.price) + '\n')

time.sleep(1)


###
Example #35
0
coffee.brand = 'dunkin'
coffee.flavor = 'fruity'
coffee.size = '20oz'
coffee.price = 4.50
table_service.insert_entity('itemstable', coffee)
print('Created entry for dunkin...\n')

###
# Use the Azure Storage Storage SDK for Python to query for entities in our Table
###
print('With some data in our Azure Storage Table, we can query the data.\nLet\'s see what the pizza menu looks like.')
raw_input('Press Enter to continue...')

# In this query, you define the partition key to search within, and then which properties to retrieve
# Structuring queries like this improves performance as your application scales up and keeps the queries efficient
items = table_service.query_entities('itemstable', filter="PartitionKey eq 'pizzamenu'", select='description,cost')
for item in items:
    print('Name: ' + item.description)
    print('Cost: ' + str(item.cost) + '\n')

items = table_service.query_entities('itemstable', filter="PartitionKey eq 'clothingstore'", select='sku,item,cost')
print('Here are clothing items.')
for item in items:
    print('SKU: ' + item.sku)
    print('Item: ' + item.item)
    print('Price: ' + str(item.cost) + '\n')

items = table_service.query_entities('itemstable', filter="PartitionKey eq 'cardealership'", select='make,model,year,color,price')
print('And here are some cars.')
for item in items:
    print('Make: ' + item.make)
Example #36
0
table_service.insert_entity('itemstable', clothing)
print('Created entry for Jeans...\n')
time.sleep(1)

###
# Use the Azure Storage Storage SDK for Python to query for entities in our Table
###
print(
    'With some data in our Azure Storage Table, we can query the data.\nLet\'s see what the carstype looks like.'
)
raw_input('Press Enter to continue...')

# In this query, you define the partition key to search within, and then which properties to retrieve
# Structuring queries like this improves performance as your application scales up and keeps the queries efficient
items = table_service.query_entities('itemstable',
                                     filter="PartitionKey eq 'carstype'",
                                     select='make,model,year,color,price')
for item in items:
    print('make: ' + item.make)
    print('model: ' + item.model)
    print('year: ' + str(item.year))
    print('color: ' + item.color)
    print('price: ' + str(item.price) + '\n')

# items = table_service.query_entities('itemstable', filter="PartitionKey eq 'clothingstore'", select='description,price')
# for item in items:
#     print('Name: ' + item.description)
#     print('Price: ' + str(item.price) + '\n')

# time.sleep(1)
Example #37
0
    return a, likedByOtherCountList

table_service = TableService(account_name='seva', account_key='SgbxLwWkBH4XuGebxECoXfNVG3mVM5YjOs+SWTDUSacc+3YgUmcafYXrXdz5k0HtlZQ3AuEJ1IcFtZYeGVR9Hw==')
batch =TableBatch()
#table_service.delete_table('MyVkApp')
#table_service.create_table('MyVkApp')

login = input("Введите имя пользователя: ")
password = input("Введите пароль: ")
session = vk.AuthSession(app_id='5889724', user_login=login, user_password=password)
api = vk.API(session)

#Вычисление количества друзей текущего пользователя
friends =api.friends.get(count='')

friends_list =table_service.query_entities('MyVkApp', filter="PartitionKey eq 'my_friends1'")

k =1

for friend in friends_list:
    
    try:
        friends =api.friends.get(user_id=friend.user_id, count='')
        friends_count =len(friends)
    except:
        friends_count =0
    try:
        groups =api.groups.get(user_id=friend.user_id, count='')
        groups_count =len(groups)
    except:
        groups_count =0
Example #38
0
coffee.flavor = 'Dirt'
coffee.sizecup = 'Large'
coffee.pricecup = 1000.00
table_service.insert_entity('itemstable', coffee)
print('Created entry for Large Dirt Flavored Starbucks...\n')
time.sleep(1)

###
# Use the Azure Storage Storage SDK for Python to query for entities in our Table
###
print('With some data in our Azure Storage Table, we can query the data.\nLet\'s see what the car menu looks like.')
raw_input('Press Enter to continue...')

# In this query, you define the partition key to search within, and then which properties to retrieve
# Structuring queries like this improves performance as your application scales up and keeps the queries efficient
items = table_service.query_entities('itemstable', filter="PartitionKey eq 'cars'", select='description,cost')
for item in items:
    print('Name: ' + item.description)
    print('Cost: ' + str(item.cost) + '\n')

items = table_service.query_entities('itemstable', filter="PartitionKey eq 'coffeeshop'", select='description,price')
for item in items:
    print('Name: ' + item.description)
    print('Price: ' + str(item.price) + '\n')

time.sleep(1)


###
# This was a quick demo to see Tables in action.
# Although the actual cost is minimal (fractions of a cent per month) for the three entities we created, it's good to clean up resources when you're done
table_service.insert_entity('htangitemstable', coffee)
print('Created entry for Nescafe...\n')
time.sleep(1)

###
# Use the Azure Storage Storage SDK for Python to query for entities in our Table
###
print(
    'With some data in our Azure Storage Table, we can query the data.\nLet\'s see what the car selection looks like.'
)
input('Press Enter to continue...')

# In this query, you define the partition key to search within, and then which properties to retrieve
# Structuring queries like this improves performance as your application scales up and keeps the queries efficient
items = table_service.query_entities('htangitemstable',
                                     filter="PartitionKey eq 'car_selections'",
                                     select='make,model,year,color,price')
for item in items:
    print('Make: ' + item.make)
    print('Model: ' + item.model)
    print('Year: ' + str(item.year) + '\n')
    print('Color: ' + item.color)
    print('Price: ' + str(item.price) + '\n')

items = table_service.query_entities('htangitemstable',
                                     filter="PartitionKey eq 'coffeemenu'",
                                     select='brand,flavor,size,price')
for item in items:
    print('Brand: ' + item.brand)
    print('Flavor: ' + item.flavor)
    print('Size: ' + item.size)
Example #40
0
table_service.insert_entity('itemstable', coffee)
print('Created entry for Columbia...\n')
time.sleep(1)

###
# Use the Azure Storage Storage SDK for Python to query for entities in our Table
###
print(
    'With some data in our Azure Storage Table, we can query the data.\nLet\'s see what the car list looks like.'
)
raw_input('Press Enter to continue...')

# In this query, you define the partition key to search within, and then which properties to retrieve
# Structuring queries like this improves performance as your application scales up and keeps the queries efficient
items = table_service.query_entities('itemstable',
                                     filter="PartitionKey eq 'carlist'",
                                     select='make,model,year,color,price')
for item in items:
    print('Make: ' + item.make)
    print('Model: ' + item.model)
    print('Year: ' + item.year)
    print('Color: ' + item.color)
    print('Price: ' + str(item.price) + '\n')

items = table_service.query_entities('itemstable',
                                     filter="PartitionKey eq 'coffeeinv'",
                                     select='brand,flavor,size,price')
for item in items:
    print('Brand: ' + item.brand)
    print('Flavor: ' + item.flavor)
    print('Size: ' + item.size)
Example #41
0
class ReadAzureTable(object):
    def __init__(self):
        self.table_service = TableService(account_name=tc.account_name,
                                          account_key=tc.account_key,
                                          endpoint_suffix=tc.endpoint_suffix)
        self.PT_azuretable = 5
        # self.station = pd.read_csv('station.csv')
        # self.deviceType = pd.read_csv('deviceType.csv')
        # self.deviceMode = pd.read_csv('deviceMode.csv')

    def readData(self, deviceCode, deviceModeCode, startTime, endTime):
        '''
            deviceCode='350M202M4M2' #需要访问的设备编号

        '''
        sTime = parser.parse(startTime)
        eTime = parser.parse(endTime)
        # print (startTime)
        # print (endTime)
        print(type(eTime))
        print((sTime))
        sRowKey = mathtool.TimetoStamp(sTime)  #用来把时间转换为TimeStamp
        eRowKey = mathtool.TimetoStamp(eTime)
        # print (sRowKey)
        # print (eRowKey)

        if sTime.month == eTime.month:
            azureTableNameSuffix = 'T' + sTime.strftime('%Y%m') + 'PT' + str(
                self.PT_azuretable) + 'S'
            filter_string = 'PartitionKey eq ' + '\'' + str(
                deviceCode) + '\'' + ' and RowKey ge ' + '\'' + str(
                    sRowKey) + '\'' + ' and RowKey le ' + '\'' + str(
                        eRowKey) + '\''
            tableName = 'S' + str(deviceModeCode) + azureTableNameSuffix
            distinguish_hlx = deviceModeCode.split('M')[1]
            selectStr = 'HL101,HL102,HL107,s'
            print(tableName)
            try:
                if distinguish_hlx == '202':
                    tasks = self.table_service.query_entities(tableName,
                                                              filter_string,
                                                              select=selectStr)
                else:
                    tasks = self.table_service.query_entities(
                        tableName, filter_string)
                rows_list = []
                for item in tasks:
                    rows_list.append(item)

                df = pd.DataFrame(rows_list)

                return df
            except:
                print('cannot find the specific table:%s' % tableName)
                return None
        else:
            azureTableNameSuffix1 = 'T' + sTime.strftime('%Y%m') + 'PT' + str(
                self.PT_azuretable) + 'S'
            azureTableNameSuffix2 = 'T' + eTime.strftime('%Y%m') + 'PT' + str(
                self.PT_azuretable) + 'S'
            filter_string = 'PartitionKey eq ' + '\'' + str(
                deviceCode) + '\'' + ' and RowKey ge ' + '\'' + str(
                    sRowKey) + '\'' + ' and RowKey le ' + '\'' + str(
                        eRowKey) + '\''
            tableName1 = 'S' + str(deviceModeCode) + azureTableNameSuffix1
            tableName2 = 'S' + str(deviceModeCode) + azureTableNameSuffix2
            distinguish_hlx = deviceModeCode.split('M')[1]
            selectStr = 'HL101,HL102,HL107,s'
            try:
                if distinguish_hlx == '202':
                    tasks1 = self.table_service.query_entities(
                        tableName1, filter_string, select=selectStr)
                    tasks2 = self.table_service.query_entities(
                        tableName2, filter_string, select=selectStr)
                else:
                    tasks1 = self.table_service.query_entities(
                        tableName1, filter_string)
                    tasks2 = self.table_service.query_entities(
                        tableName2, filter_string)
                rows_list = []
                for item1 in tasks1:
                    rows_list.append(item1)
                for item2 in tasks2:
                    rows_list.append(item2)

                df = pd.DataFrame(rows_list)

                return df
            except:
                print('cannot find the specific table:%s or %s' %
                      (tableName1, tableName2))
                return None

    def BaseInformation(self):
        pass
# DEVICE_COUNT = int(os.environ['IotDeviceCount'])

IOT_HUB_NAME = 'iothub-w74e5yvuyvfs2'
IOT_HUB_OWNER_KEY = 'Pl839QpYfaiBMbbOt8RxbrLk2RvOpuy+z+aSsecr+28='
IOT_HUB_DEVICE_KEY = '1n7jyr+tccLWN1FYgOBh9PAhEiZ0RDg1EGmpxo50o4w='

ACCOUNT_NAME = "stgw74e5yvuyvfs2"
ACCOUNT_KEY = "CKXt/8SpgY6JHfVwBQUkwBCDeBKyJzQV10sgut3L4dS/bjKlSxQsPedHkrI7td8I0NBWJoQOSkcChGo1MIf9Cw=="

table_service = TableService(account_name=ACCOUNT_NAME,
                             account_key=ACCOUNT_KEY)

connectionString = 'HostName=%s.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=%s' % (
    IOT_HUB_NAME, IOT_HUB_OWNER_KEY)

assets = table_service.query_entities('equipment')

dm = DeviceManager(connectionString)

for asset in assets:
    deviceId = asset.RowKey
    dm.createDeviceId(deviceId)
    print(dm.retrieveDeviceId(deviceId))

print(dm.listDeviceIds())

connectionString = 'HostName=%s.azure-devices.net;SharedAccessKeyName=device;SharedAccessKey=%s' % (
    IOT_HUB_NAME, IOT_HUB_DEVICE_KEY)

sender = D2CMessageSender(connectionString)