A short note on DynamoDB of AWS and how it suprisingly gives
Decimals when you extract records. And forced me to write ducktape-code.
DynamoDB implicitely converts integers to decimals. Decimals cause some problems in Python code since it is not a basic type. For example,
json.dumps() doesn't work anymore.
- You put a Python datastructure with lists and dictionairies into DynamoDB with strings and integers.
- DynamoDB converts and stores the datastructure
- You load an entry from DynamoDB
- ...it suddenly contains
Decimalsinstead of integers
json.dumps()crashes (among others)
I now simply convert decimals back to
int. The alternative of storing the object as a string (convert to JSON) I dislike. Figuring out how to configure DynamoDB...I couldn't find a decent way within half an hour. I did find a discussion on a feature request to disable decimals and/or find some sane way to work with it, but no solutions.
So I wrote a little function to convert a dictionary:
def convert_decimal(dictionary): """ Converts decimals to float and int. """ for key in dictionary.keys(): value = dictionairy[key] if isinstance(value, Decimal): if v % 1 == 0: dictionary[key] = int(value) else: dictionary[key] = float(value) return dictionary