question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add ability to disable Decimal usage for DynamoDB number type.

See original GitHub issue

Hi,

Is there any strong reason why using a DynamoDB’s Table resource will convert the number type "N" to a Decimal() object?

Shouldn’t it try looking up the right python type, such as int or float or long?

I am trying to unpack a record’s data (a mapping) into a function call, specifically: next_execution = now() + datetime.timedelta(**dynamo_record['frequency']) but datetime.timedelta will not accept the Decimal object into its arguments, although it does accept long and float.

TypeError: unsupported type for timedelta days component: Decimal

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:90
  • Comments:108 (5 by maintainers)

github_iconTop GitHub Comments

76reactions
garnaatcommented, Nov 16, 2015

FWIW, I use this little function to recurse into Python objects returned by the boto3 DynamoDB resource layer and convert any Decimal values to int or float. It is by no means foolproof and doesn’t in any way solve the problem of lack of precision in Python’s float type but it solves my problem which is mainly to turn the data into something that can be returned to API Gateway via a Python Lambda function.

def replace_decimals(obj):
    if isinstance(obj, list):
        for i in xrange(len(obj)):
            obj[i] = replace_decimals(obj[i])
        return obj
    elif isinstance(obj, dict):
        for k in obj.iterkeys():
            obj[k] = replace_decimals(obj[k])
        return obj
    elif isinstance(obj, decimal.Decimal):
        if obj % 1 == 0:
            return int(obj)
        else:
            return float(obj)
    else:
        return obj

52reactions
jameslscommented, Nov 18, 2015

Would adding some sort of use_decimal=False option to the config object when creating clients/resources be helpful?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Supported data types for DynamoDB Mapper for Java
Learn about the data types supported when using DynamoDB Mapper and the AWS SDK for ... Set collection types, SS (string set) type,...
Read more >
Amazon DynamoDB: Storing integers as Numbers vs. Strings
I know that DynamoDB already stores Numbers as strings at some point in the background, but I'm unsure if there is possibility for...
Read more >
Source code for boto3.dynamodb.types - AWS
You # may not use this file except in compliance with the License. ... that ``Decimal`` objects are used to be able to...
Read more >
Amazon Redshift Numeric: 3 Data Types Simplified
In AWS, Integer, Decimal, and Floating-point are all referred to by the term Redshift Numeric Data Types.
Read more >
Decimal type | CDP Public Cloud
The decimal type is a numeric data type with fixed scale and precision suitable for financial and other arithmetic calculations where the imprecise ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found