我们从Python开源项目中,提取了以下42个代码示例,用于说明如何使用boto3.dynamodb.conditions.Attr()。
def parse_filters(self, filters, doc_class): index_name = None filter_expression_list = [] query_params = {} for idx, filter in enumerate(filters): prop_name, prop_value = filter.split(':')[3:5] if idx == 0: prop = doc_class()._base_properties[prop_name] index_name = prop.kwargs.get(self.index_field_name, None) or \ self.default_index_name.format(prop_name) query_params['KeyConditionExpression'] = Key(prop_name).eq(prop_value) else: filter_expression_list.append(Attr(prop_name).eq(prop_value)) if len(filter_expression_list) > 1: query_params['FilterExpression'] = And(*filter_expression_list) elif len(filter_expression_list) == 1: query_params['FilterExpression'] = filter_expression_list[0] if index_name != '_id': query_params['IndexName'] = index_name return query_params
def get_waiting_tasks(self, concurrency_key): """ Returns list of waiting tasks with the specified concurrency key :param concurrency_key: concurrency key of the tasks :return: concurrency_key: list of waiting tasks """ args = { "IndexName": "WaitForExecutionTasks", "Select": "ALL_ATTRIBUTES", "KeyConditionExpression": Key(TASK_TR_CONCURRENCY_ID).eq(concurrency_key), "FilterExpression": Attr(TASK_TR_STATUS).eq(STATUS_WAITING) } waiting_list = [] while True: resp = self._action_table.query_with_retries(**args) waiting_list += resp.get("Items", []) last = resp.get("LastEvaluatedKey") if last is not None: args["ExclusiveStartKey"] = last else: break return waiting_list
def lambda_handler(event, context): wotd = table.query( KeyConditionExpression=Key('language').eq(event['request']['intent']['slots']["Language"]["value"].lower()) & Key('id').gt(0), FilterExpression=Attr('isActive').eq(True) ) print(wotd['Items']) item = wotd["Items"] print(item) parsed = '<speak>The ' + event["request"]["intent"]["slots"]["Language"]["value"] + ' word of the day is <audio src="' + item[0]["word_sound"] + \ '"/> which means ' + item[0]["word_translation"] + \ '. Here is the word used in a sentence. <audio src="' + item[0]["sentence_sound"] + \ '"/> which means ' + item[0]["sentence_translation"] + '</speak>' response = { 'version': '1.0', 'response': { 'outputSpeech': { 'type': 'SSML', 'ssml' : parsed } } } return response
def query_with_limit_and_filter_by_boto3(): table = dynamodb.Table('Movies') print("Movies from 1992 - titles A-L, with genres and lead actor") try: response = table.query( ProjectionExpression="#yr, title, info.genres, info.actors[0]", ExpressionAttributeNames={"#yr": "year"}, # Expression Attribute Names for Projection Expression only. KeyConditionExpression=Key('year').eq(1992) & Key('title').between('A', 'L'), FilterExpression=Attr('rating').lt(decimal.Decimal(str('7.0'))), Limit=10, ) except ClientError as e: print(e.response['Error']['Message']) else: items = response['Items'] for i in items: print(i['year'], ":", i['title'])
def _expression_func(self, op, *values, **kwargs): # for use by index ... bad values = map(self.typecast_for_storage, values) self.op = op self.express_args = values use_key = kwargs.get('use_key', False) if self.hash_key and op != 'eq': raise ValidationException('Query key condition not supported') elif self.hash_key or self.range_key or use_key: use_key = True func = getattr(Key(self.name), op, None) else: func = getattr(Attr(self.name), op, None) if not func: raise ValidationException('Query key condition not supported') return self, func(*values), use_key
def write_lock_item(self, lockname, caller, waitms): expire_ts = timestamp_millis() + waitms logger.debug("Write_item: lockname=" + lockname + ", caller=" + caller + ", Expire time is " + str(expire_ts)) try: self.get_table().put_item( Item={ 'lockname': lockname, 'expire_ts': expire_ts, 'holder': caller }, # TODO: adding Attr("holder").eq(caller) should make it re-entrant ConditionExpression=Attr("holder").eq(NO_HOLDER) | Attr('lockname').not_exists() ) except botocore.exceptions.ClientError as e: if e.response['Error']['Code'] == 'ConditionalCheckFailedException': logger.warn("Write_item: lockname=" + lockname + ", caller=" + caller + ", lock is being held") return False logger.debug("Write_item: lockname=" + lockname + ", caller=" + caller + ", lock is acquired") return True
def clear_lock_item(self, lockname, caller): try: self.get_table().put_item( Item={ 'lockname': lockname, 'expire_ts': 0, 'holder': NO_HOLDER }, ConditionExpression=Attr("holder").eq(caller) | Attr('lockname').not_exists() ) except botocore.exceptions.ClientError as e: if e.response['Error']['Code'] == 'ConditionalCheckFailedException': logger.warn("clear_lock_item: lockname=" + lockname + ", caller=" + caller + " release failed") return False logger.debug("clear_lock_item: lockname=" + lockname + ", caller=" + caller + " release succeeded") return True
def prune_expired(self, lockname, caller): now = timestamp_millis() logger.debug("Prune: lockname=" + lockname + ", caller=" + caller + ", Time now is %s" + str(now)) try: self.get_table().put_item( Item={ 'lockname': lockname, 'expire_ts': 0, 'holder': NO_HOLDER }, ConditionExpression=Attr("expire_ts").lt(now) | Attr('lockname').not_exists() ) except botocore.exceptions.ClientError as e: if e.response['Error']['Code'] == 'ConditionalCheckFailedException': logger.warn("Prune: lockname=" + lockname + ", caller=" + caller + " Prune failed") return False logger.debug("Prune: lockname=" + lockname + ", caller=" + caller + " Prune succeeded") return True
def update_limit_value(limit_type): service, limit_name = limit_type.split(NAME_SEPARATOR) checker = get_aws_limit_checker() limits = checker.get_limits(use_ta=settings.PREMIUM_ACCOUNT) default_limit = limits[service][limit_name].default_limit dynamodb = get_boto_resource('dynamodb') tickets_table = get_tickets_table() tickets = tickets_table.scan( FilterExpression=Attr('limit_type').eq(limit_type) )['Items'] if tickets: max_value = max(ticket['limit_value'] for ticket in tickets) else: max_value = 0 max_value = max([max_value, default_limit]) update_dynamodb_limit_value(limit_type, max_value)
def get_recently_sent_alerts(limits): table = create_or_get_table( table_name=SENT_ALERTS_TABLE_NAME, attribute_definitions=[ { 'AttributeName': 'limit_name', 'AttributeType': 'S', }, ], key_schema=[ { 'AttributeName': 'limit_name', 'KeyType': 'HASH' }, ], ) three_days_ago_ts = Decimal((datetime.utcnow() - timedelta(days=3)).strftime('%s')) alerts = table.scan( FilterExpression=Attr('alert_sent').gt(three_days_ago_ts) )['Items'] return [alert['limit_name'] for alert in alerts]
def update_user_score(self, username, question_id, provided_answer, is_correct): update_expression_components = [] expression_attribute_names = {} expression_attribute_values = {} update_expression_components.append('#ans.#qid = :answer') expression_attribute_names['#ans'] = 'answers' expression_attribute_names['#qid'] = question_id expression_attribute_values[':answer'] = { 'provided_answer': provided_answer, 'is_correct': is_correct, } update_expression_components.append( 'total_answered = total_answered + :inc') expression_attribute_values[':inc'] = 1 if is_correct: update_expression_components.append( 'total_correct = total_correct + :inc') try: self._dynamodb.Table(self._table_name).update_item( Key={ 'username': username }, UpdateExpression='SET ' + ', '.join( update_expression_components), ExpressionAttributeNames=expression_attribute_names, ConditionExpression=Attr( 'answers.' + question_id).not_exists(), ExpressionAttributeValues=expression_attribute_values ) except self._dynamodb.meta.client.exceptions.\ ConditionalCheckFailedException: raise UserAlreadyAnsweredError( 'User \'%s\' has already answered this question. A specific ' 'user is only allowed to answer a question once.' % username )
def _search(self, search_terms, begins_with=None): """ Returns a list of Archive id's in the table on Dynamo """ kwargs = dict( ProjectionExpression='#id', ExpressionAttributeNames={"#id": "_id"}) if len(search_terms) > 0: kwargs['FilterExpression'] = reduce( lambda x, y: x & y, [Attr('tags').contains(arg) for arg in search_terms]) if begins_with: if 'FilterExpression' in kwargs: kwargs['FilterExpression'] = kwargs[ 'FilterExpression'] & Key('_id').begins_with(begins_with) else: kwargs['FilterExpression'] = Key( '_id').begins_with(begins_with) while True: res = self._table.scan(**kwargs) for r in res['Items']: yield r['_id'] if 'LastEvaluatedKey' in res: kwargs['ExclusiveStartKey'] = res['LastEvaluatedKey'] else: break
def find_by_identity(identity): return table.scan( FilterExpression=Attr('TrueIdentity').eq(identity) & Attr('Identity').eq('Uncovered') )['Items']
def find_all_uncovered(): return table.scan( FilterExpression=Attr('Identity').eq('Uncovered') )['Items']
def find_by_name(name): result = table.scan( FilterExpression=Attr('Identity').eq('Uncovered') & Attr('Name').eq(name) )['Items'] return result[0] if result else None
def sync_config(self): self.connect_dynamodb() results = self.dynamodb.scan( FilterExpression=Attr('name').exists() ) return results.get('Items', None)
def find(self, user_id): """ Search dynamodb for all non-expired alerts for a given user. :param user_id: The auth0 id of the user. :return: List of alerts """ self.connect_dynamodb() response = self.dynamodb.scan( FilterExpression=Attr('user_id').eq(user_id) ) return response.get('Items')
def putSecret(name, secret, version="", kms_key="alias/credstash", region=None, table="credential-store", context=None, digest=DEFAULT_DIGEST, **kwargs): ''' put a secret called `name` into the secret-store, protected by the key kms_key ''' if not context: context = {} session = get_session(**kwargs) kms = session.client('kms', region_name=region) key_service = KeyService(kms, kms_key, context) sealed = seal_aes_ctr_legacy( key_service, secret, digest_method=digest, ) dynamodb = session.resource('dynamodb', region_name=region) secrets = dynamodb.Table(table) data = { 'name': name, 'version': paddedInt(version), } data.update(sealed) return secrets.put_item(Item=data, ConditionExpression=Attr('name').not_exists())
def deleteSecrets(name, region=None, table="credential-store", **kwargs): session = get_session(**kwargs) dynamodb = session.resource('dynamodb', region_name=region) secrets = dynamodb.Table(table) response = secrets.scan(FilterExpression=boto3.dynamodb.conditions.Attr("name").eq(name), ProjectionExpression="#N, version", ExpressionAttributeNames={"#N": "name"}) for secret in response["Items"]: print("Deleting %s -- version %s" % (secret["name"], secret["version"])) secrets.delete_item(Key=secret)
def get_unread_message_ids_asof(user_id, asof): # get list of messageIds q = {'KeyConditionExpression': Key('userId').eq(user_id), 'FilterExpression': Attr('created').lte(asof) & Attr('is_read').ne(1), 'Limit':1000, 'Select':'SPECIFIC_ATTRIBUTES', 'ProjectionExpression':'messageId'} r = collect_results(get_history_table().query,q) msg_ids = [x['messageId'] for x in r] return msg_ids
def get_user_messages(user_id,start_t=None,end_t=None,type_filter=None): q = {'KeyConditionExpression': Key('userId').eq(user_id)} if start_t is not None and end_t is not None: q['FilterExpression'] = Attr('created').gte(start_t) & Attr('created').lte(end_t) elif start_t is not None: q['FilterExpression'] = Attr('created').gte(start_t) elif end_t is not None: q['FilterExpression'] = Attr('created').lte(end_t) if type_filter is not None: type_exp = Attr('_type').is_in(type_filter) if 'FilterExpression' in q: q['FilterExpression'] = q['FilterExpression'] & type_exp else: q['FilterExpression'] = type_exp return collect_results(get_history_table().query,q)
def lookup(account_id=None, user_id=None, session_id=None, max_expired_age=None): q = {'Select': 'ALL_ATTRIBUTES'} if user_id is not None: q['KeyConditionExpression'] = Key('userId').eq(user_id) if session_id is not None: q['KeyConditionExpression'] = q['KeyConditionExpression'] & Key('sessionId').eq(session_id) if account_id is not None: q['FilterExpression'] = Attr('accountId').eq(account_id) elif account_id is not None: # use the account GSI q['KeyConditionExpression'] = Key('accountId').eq(account_id) q['IndexName'] = os.getenv('SESSION_TABLE_ACCOUNT_GSI') if session_id is not None: q['FilterExpression'] = Attr('sessionId').eq(session_id) elif session_id is not None: q['FilterExpression'] = Attr('sessionId').eq(session_id) else: return get_all_sessions(max_expired_age=max_expired_age) if max_expired_age is not None: exp_filter = Attr('expires').gte(int(time.time()-max_expired_age)) if 'FilterExpression' in q: q['FilterExpression'] = q['FilterExpression'] & exp_filter else: q['FilterExpression'] = exp_filter if 'KeyConditionExpression' in q: return collect_results(get_session_table().query,q) else: return collect_results(get_session_table().scan,q) # expensive call, so cache briefly if we have a flood of broadcast messages
def get_all_sessions(max_expired_age=None): q = {'Select': 'ALL_ATTRIBUTES'} if max_expired_age is not None: q['FilterExpression'] = Attr('expires').gte(int(time.time()-max_expired_age)) return collect_results(get_session_table().scan,q)
def eq(self, value): # == # Creates a condition where the attribute is equal to the value. # Attr & Key return self._expression_func('eq', value)
def ne(self, value): # != # Creates a condition where the attribute is not equal to the value # Attr return self._expression_func('ne', value)
def lt(self, value): # < # Creates a condition where the attribute is less than the value. # Attr & Key return self._expression_func('lt', value)
def gt(self, value): # > # Creates a condition where the attribute is greater than the value. # Attr & Key return self._expression_func('gt', value)
def gte(self, value): # >= # Creates a condition where the attribute is greater than or equal to # the value. # Attr & Key return self._expression_func('gte', value)
def between(self, low_value, high_value): # Creates a condition where the attribute is greater than or equal to # the low value and less than or equal to the high value. # Attr & Key return self._expression_func('between', low_value, high_value)
def begins_with(self, value): # Creates a condition where the attribute begins with the value # Attr & Key return self._expression_func('begins_with', value)
def exists(self): # Creates a condition where the attribute exists. # Attr if self.hash_key or self.range_key: # ValidationException raise ValidationException('Query key condition not supported') return self.name, Attr(self.name).exists(), False
def not_exists(self): # Creates a condition where the attribute does not exists. # Attr if self.hash_key or self.range_key: # ValidationException raise ValidationException('Query key condition not supported') return self.name, Attr(self.name).not_exists(), False
def deletesubnets(stack_id, ddb_t): """ Delete any subnets in the DB for a stack""" try: response = ddb_t.scan( FilterExpression=Attr('StackId').eq(stack_id) ) for item in response['Items']: print item ddb_t.delete_item(Key={'Cidr' : item['Cidr']}) except Exception: pass
def scanFilter(attributeName, attribute, table): return table.scan(FilterExpression=Attr(attributeName).eq(attribute))
def get_pending_tickets(): table = get_tickets_table() cases = table.scan( FilterExpression=Attr('limit_type').eq('unknown') & Attr('body').ne('N/A') )['Items'] cases = sorted(cases, key=lambda case: case['display_id'], reverse=True) return cases
def handler(event, context): log.debug("Received event {}".format(json.dumps(event))) accountInfo = dynamodb.Table(os.environ['TAILOR_TABLENAME_ACCOUNTINFO']) try: print('context:resource-path', event['context']['resource-path'] == '/cloudtrail/{accountId}') print('path:accountId', re.match("^[0-9]{12}$", event['params']['path']['accountId'])) except Exception as e: print(e) print("regex not matching any values passed in request") raise Exception({"code": "4000", "message": "ERROR: Bad request"}) # Payload processing logic if event['context']['resource-path'] == '/cloudtrail/{accountId}' and \ re.match("^[0-9]{12}$", event['params']['path']['accountId']): requestId = str(uuid.uuid4()) accountId = event['params']['path']['accountId'] stage = event['stage-variables']['stage'] # Check if account is known to Tailor getAccountId = accountInfo.scan( ProjectionExpression='accountId, accountEmailAddress', FilterExpression=Attr('accountId').eq(accountId) ) if getAccountId['Count'] == 0: print("Account not found") raise Exception({"code": "4040", "message": "ERROR: Not found"}) elif int(getAccountId['Count']) > 0: # Update accountInfo with new requestId accountInfo.update_item( Key={ 'accountEmailAddress': getAccountId['Items'][0]['accountEmailAddress'] }, UpdateExpression='SET #requestId = :val1', ExpressionAttributeNames={'#requestId': "requestId"}, ExpressionAttributeValues={':val1': requestId} ) # Build Lambda invoke payload message = {"requestId": requestId, "accountId": accountId, "accountEmailAddress": getAccountId['Items'][0]['accountEmailAddress']} payload = {"message": message} # Call Lambda awslambda.invoke( FunctionName='talr-cloudtrail-' + stage, InvocationType='Event', Payload=json.dumps(payload), ) return {"code": "2020", "message": "Request Accepted", "requestId": requestId} else: raise Exception({"code": "4000", "message": "ERROR: Bad request"})
def handler(event, context): log.debug("Received event {}".format(json.dumps(event))) accountInfo = dynamodb.Table(os.environ['TAILOR_TABLENAME_ACCOUNTINFO']) try: print('context:resource-path', event['context']['resource-path'] == '/cloudabilty') print('body-json:accountId', re.match("^[0-9]{12}$", event['body-json']['accountId'])) except Exception as e: print(e) print("regex not matching any values passed in request") raise Exception({"code": "4000", "message": "ERROR: Bad request"}) # VPC DNS logic if event['context']['resource-path'] == '/cloudability' and \ re.match("^[0-9]{12}$", event['body-json']['accountId']): requestId = str(uuid.uuid4()) accountId = event['body-json']['accountId'] stage = event['stage-variables']['stage'] # Check if account already exists getAccountId = accountInfo.scan( ProjectionExpression='accountId, accountEmailAddress', FilterExpression=Attr('accountId').eq(accountId) ) if getAccountId['Count'] == 0: print("Account not found") raise Exception({"code": "4040", "message": "ERROR: Not found"}) elif int(getAccountId['Count']) > 0: # Update accountInfo with new requestId accountInfo.update_item( Key={ 'accountEmailAddress': getAccountId['Items'][0]['accountEmailAddress'] }, UpdateExpression='SET #requestId = :val1', ExpressionAttributeNames={'#requestId': "requestId"}, ExpressionAttributeValues={':val1': requestId} ) # Build Lambda invoke payload message = {"requestId": requestId, "accountId": accountId, "accountEmailAddress": getAccountId['Items'][0]['accountEmailAddress'] } payload = {"Records": [{"Sns": {"Message": message}}]} # Call Lambda awslambda.invoke( FunctionName='talr-cloudability-' + stage, InvocationType='Event', Payload=json.dumps(payload), ) return {"code": "2020", "message": "Request Accepted", "requestId": requestId} else: raise Exception({"code": "4000", "message": "ERROR: Bad request"})
def Q(**mapping): """A Q object represents an AND'd together query using boto3's Attr object, based on a set of keyword arguments that support the full access to the operations (eq, ne, between, etc) as well as nested attributes. It can be used input to both scan operations as well as update conditions. """ expression = None while len(mapping): attr, value = mapping.popitem() parts = attr.split('__') attr = Attr(parts.pop(0)) op = 'eq' while len(parts): if not hasattr(attr, parts[0]): # this is a nested field, extend the attr attr = Attr('.'.join([attr.name, parts.pop(0)])) else: op = parts.pop(0) break assert len(parts) == 0, "Left over parts after parsing query attr" op = getattr(attr, op) try: attr_expression = op(value) except TypeError: # A TypeError calling our attr op likely means we're invoking exists, not_exists or another op that # doesn't take an arg or takes multiple args. If our value is True then we try to re-call the op # function without any arguments, if our value is a list we use it as the arguments for the function, # otherwise we bubble it up. if value is True: attr_expression = op() elif isinstance(value, collections.Iterable): attr_expression = op(*value) else: raise try: expression = expression & attr_expression except TypeError: expression = attr_expression return expression
def is_in(self, value): # Creates a condition where the attribute is in the value # Attr if self.hash_key or self.range_key: # ValidationException raise ValidationException('Query key condition not supported') return self.name, Attr(self.name).is_in(value), False