def _wipe_dynamodb_table(table: Table) -> None: """ Based off https://stackoverflow.com/a/61641725 """ # get the table keys - assume AttributeName exists table_key_names = [str(key["AttributeName"]) for key in table.key_schema] """ NOTE: there are reserved attributes for key names, please see https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ReservedWords.html if a hash or range key is in the reserved word list, you will need to use the ExpressionAttributeNames parameter described at https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb.html#DynamoDB.Table.scan """ # Only retrieve the keys for each item in the table (minimize data transfer) projection_expression = ", ".join(table_key_names) response = table.scan(ProjectionExpression=projection_expression) data = response.get("Items") assert data is not None, f"Expected items, got {data}" while "LastEvaluatedKey" in response: response = table.scan( ProjectionExpression=projection_expression, ExclusiveStartKey=response["LastEvaluatedKey"], ) data.extend(response["Items"]) with table.batch_writer() as batch: for each in data: batch.delete_item(Key={key: each[key] for key in table_key_names})
def get_all_counts(cls, table: Table) -> t.Dict[str, int]: response = table.scan(FilterExpression=Attr("PK").eq(cls._get_pkey())) return { t.cast(str, item["SK"]).split("#", 1)[1]: int(t.cast(Decimal, item["WriteCount"])) for item in response["Items"] }
def _table_is_empty(table: Table) -> bool: """ fun fact: some_table.item_count? It's only updated every 6 hours. you have to do a scan. """ items = table.scan()["Items"] if items: return False # Something's in there! return True
def scan_info(table: Table, house: str) -> Union[Any, bool]: try: db_response = table.scan( FilterExpression=Attr('house').eq(house.lower())) items = db_response['Items'] return items except Exception as e: print("Something went wrong: ", e) return False
def _dump_dynamodb_table(table: Table) -> Optional[str]: """ Outputs a nicely-formatted Python list of all the items in the table. (you may need a `from decimal import Decimal` to interact with it, though.) """ items = table.scan()["Items"] if not items: return None return pretty_format(items)
def from_time_range(cls, table: Table, hash_type: str, start_time: str = None, end_time: str = None) -> t.List[t.Dict]: """ Given a hash type and time range, give me all the hashes found for that type and time range """ if start_time is None: start_time = datetime.datetime.min.isoformat() if end_time is None: end_time = datetime.datetime.max.isoformat() return table.scan( FilterExpression=Key("SK").eq(hash_type) & Key("UpdatedAt").between(start_time, end_time), ProjectionExpression=cls.DEFAULT_PROJ_EXP, ).get("Items", [])
def seed_fake_data_if_needed(pac_table: Table): if pac_table.scan()['Count'] != 0: return print("Seeding data") for _ in range(100): price = random.choice([499, 999, 1999]) status = Status.Available funder = '0x1234' signer = random_eth_address() pac_table.put_item( Item={ 'id': signer, # using signer as the unique id 'price': str(price), 'status': status.name, 'price_status': "{}-{}".format(price, status.name), 'funder': funder, 'signer': signer, 'updated': datetime.now().isoformat() })