You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fromrandomimportrandintfromaws_lambda_powertools.utilities.batchimportBasePartialProcessor, batch_processorimportboto3importostable_name=os.getenv("TABLE_NAME", "table_not_found")
classMyPartialProcessor(BasePartialProcessor):
""" Process a record and stores successful results at a Amazon DynamoDB Table Parameters ---------- table_name: str DynamoDB table name to write results to """def__init__(self, table_name: str):
self.table_name=table_namesuper().__init__()
def_prepare(self):
# It's called once, *before* processing# Creates table resource and clean previous resultsself.ddb_table=boto3.resource("dynamodb").Table(self.table_name)
self.success_messages.clear()
def_clean(self):
# It's called once, *after* closing processing all records (closing the context manager)# Here we're sending, at once, all successful messages to a ddb tablewithself.ddb_table.batch_writer() asbatch:
forresultinself.success_messages:
batch.put_item(Item=result)
def_process_record(self, record):
# It handles how your record is processed# Here we're keeping the status of each run# where self.handler is the record_handler function passed as an argumenttry:
result=self.handler(record) # record_handler passed to decorator/context managerreturnself.success_handler(record, result)
exceptExceptionasexc:
returnself.failure_handler(record, exc)
defsuccess_handler(self, record):
entry= ("success", result, record)
message= {"age": result}
self.success_messages.append(message)
returnentrydefrecord_handler(record):
returnrandint(0, 100)
@batch_processor(record_handler=record_handler, processor=MyPartialProcessor(table_name))deflambda_handler(event, context):
return {"statusCode": 200}
fromrandomimportrandintfromaws_lambda_powertools.utilities.batchimportBasePartialProcessor, batch_processorimportboto3importostable_name=os.getenv("TABLE_NAME", "table_not_found")
classMyPartialProcessor(BasePartialProcessor):
""" Process a record and stores successful results at a Amazon DynamoDB Table Parameters ---------- table_name: str DynamoDB table name to write results to """def__init__(self, table_name: str):
self.table_name=table_namesuper().__init__()
def_prepare(self):
# It's called once, *before* processing# Creates table resource and clean previous resultsself.ddb_table=boto3.resource("dynamodb").Table(self.table_name)
self.success_messages.clear()
def_clean(self):
# It's called once, *after* closing processing all records (closing the context manager)# Here we're sending, at once, all successful messages to a ddb tablewithself.ddb_table.batch_writer() asbatch:
forresultinself.success_messages:
batch.put_item(Item=result)
def_process_record(self, record):
# It handles how your record is processed# Here we're keeping the status of each run# where self.handler is the record_handler function passed as an argumenttry:
result=self.handler(record) # record_handler passed to decorator/context managerreturnself.success_handler(record, result)
exceptExceptionasexc:
returnself.failure_handler(record, exc)
defsuccess_handler(self, record):
entry= ("success", result, record)
message= {"age": result}
self.success_messages.append(message)
returnentrydefrecord_handler(record):
returnrandint(0, 100)
@batch_processor(record_handler=record_handler, processor=MyPartialProcessor(table_name))deflambda_handler(event, context):
return {"statusCode": 200}
michaelbrewer
changed the title
Bug: Creating a custom batch processor example
Bug: Batch, reating a custom batch processor example
May 18, 2022
michaelbrewer
changed the title
Bug: Batch, reating a custom batch processor example
Bug: Batch, creating a custom batch processor example
May 18, 2022
michaelbrewer
changed the title
Bug: Batch, creating a custom batch processor example
Bug: Batch, creating a custom batch processor example has python syntax errors
May 18, 2022
michaelbrewer
changed the title
Bug: Batch, creating a custom batch processor example has python syntax errors
Bug: Batch - creating a custom batch processor example has python syntax errors
May 18, 2022
sthulb
changed the title
Bug: Batch - creating a custom batch processor example has python syntax errors
Docs: Batch - creating a custom batch processor example has python syntax errors
May 18, 2022
Expected Behaviour
Example for Creating a custom batch processor should be validate and compile
Current Behaviour
success_handler
is missingresult
Code snippet
Possible Solution
Fix the code example and ensure that it compiles, a PR for this already exists #1114 , a deployed fix is here : https://gyft.github.io/aws-lambda-powertools-python/latest/utilities/batch/#create-your-own-partial-processor
Steps to Reproduce
Creating a custom batch processor
AWS Lambda Powertools for Python version
latest
AWS Lambda function runtime
3.9
Packaging format used
PyPi
Debugging logs
The text was updated successfully, but these errors were encountered: