Skip to content

Instantly share code, notes, and snippets.

@lbenitez000
Last active March 16, 2017 16:02
Show Gist options
  • Save lbenitez000/85fb43e40a7839e13405 to your computer and use it in GitHub Desktop.
Save lbenitez000/85fb43e40a7839e13405 to your computer and use it in GitHub Desktop.
A monkey patch for boto's Decimal context to allow inexact and rounded representation of floats. Used to store any float in DynamoDB when inexactitude is allowed.
# Monkey patch Decimal's default Context to allow
# inexact and rounded representation of floats
import decimal
from boto.dynamodb.types import DYNAMODB_CONTEXT
# Inhibit Inexact Exceptions
DYNAMODB_CONTEXT.traps[decimal.Inexact] = 0
# Inhibit Rounded Exceptions
DYNAMODB_CONTEXT.traps[decimal.Rounded] = 0
@joaoportela
Copy link

Thank you for this.

I couldn't find a general and meaningful representation for my floating numbers that didn't cause issues, some I just patched things like you said.

@manuelnaranjo
Copy link

Great solution! good catch, btw you want to fix boto3 as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment