Boto DynamoDB 请求中的布尔值转换为 decimal.Decimal 对象
Boolean Converted To decimal.Decimal object in Boto DynamoDB Requests
当我使用 boto.dynamodb2.table.Table 方法向 dynamoDB 发送一个 put item 请求并在其中一个项目的属性中包含一个布尔值,然后对同一项目发出 get item 请求时,属性值 returns 为十进制('1').
>>> Table.put_item(data={
'id': 'sjx7MQrKNqD7uQ6Xc2UepQkBY7xbJxvcGViP',
'active': True
})
>>> response = Table.get_item(id='sjx7MQrKNqD7uQ6Xc2UepQkBY7xbJxvcGViP')
>>> print(response)
{'active': Decimal('1'), 'id': 'sjx7MQrKNqD7uQ6Xc2UepQkBY7xbJxvcGViP'}
尽管在 boto Github repo 中有很多关于在将 python float values to decimal.Decimal objects 或字符串发送到 dynamoDB 之前转换它们以保持数据完整性的讨论,但我一直无法找到任何关于正在转换的布尔值。
AWS documentation indicates that a boolean is an acceptable datatype and doesn't mention anything about it being converted to a string like numbers are. But, there is a cryptic method for Table called use_boolean() 没有文档。所以,我很困惑。
这是其他人遇到的问题吗?如果是这样,有什么解释吗?如果不是,有什么线索说明为什么我的构建会这样做吗?
我自己 运行 遇到了同样的问题,并在下面的评论中进一步研究 use_boolean()
并找到了解决方案。
似乎只在 2015 年 1 月 17 日在此拉取请求中将对 dynamoDB 中的 Boolean
类型的支持添加到 boto:
https://github.com/boto/boto/pull/2667
这个补丁相对较新(如果你认为新的 <1 年)所以可以解释为什么 use_boolean()
方法没有很好的记录,但该方法似乎类似于 use_decimals()
方法,在普通的旧 boto.dynamodb
教程中有详细记录:
http://boto.readthedocs.org/en/latest/dynamodb_tut.html#working-with-decimals
在拉取请求中,您可以看到围绕 boto 的那些已经将其布尔类型强制转换为 int 的用户的重要性以及如何保持向后兼容性的讨论。
拉取请求引入了一个 NonBooleanDynamizer
声明为默认值 _dynamizer
;除非您在 table 对象上调用 use_boolean()
方法。补丁最相关的部分如下:
https://github.com/kain-jy/boto/commit/886c4bf1877538a6acc28dd5f9bdd1c8f1c30dd9#diff-454bd7ad5c48dd01834d852f01e4b573R114
下面应该更清楚地说明如何使用 boto.dynamodb2
(我还没有完全研究过普通旧 boto.dynamodb
的等价物,但是 use_boolean 函数参数的散布在代码库建议有一种方法,如果需要的话):
>>> from boto.dynamodb2.fields import HashKey
>>> from boto.dynamodb2.table import Table
>>> data = {'true': True, 'false': False, 'one': 1, 'zero': 0}
>>> table = Table.create('q32109154-test1', schema=[HashKey('hkey')])
>>> data['hkey'] = 'test1'
>>> table.put_item(data=data)
>>> result = table.get_item(hkey='test1')
>>> print [(k, v) for k, v in result.items()]
[(u'hkey', u'test1'), (u'zero', Decimal('0')), (u'true', Decimal('1')), (u'false', Decimal('0')), (u'one', Decimal('1'))]
>>> table = Table.create('q32109154-test2', schema=[HashKey('hkey')])
>>> data['hkey'] = 'test2'
>>> table.use_boolean()
>>> table.put_item(data=data)
>>> result = table.get_item(hkey='test2')
>>> print [(k, v) for k, v in result.items()]
[(u'hkey', u'test2'), (u'zero', Decimal('0')), (u'true', True), (u'false', False), (u'one', Decimal('1'))]
当我使用 boto.dynamodb2.table.Table 方法向 dynamoDB 发送一个 put item 请求并在其中一个项目的属性中包含一个布尔值,然后对同一项目发出 get item 请求时,属性值 returns 为十进制('1').
>>> Table.put_item(data={
'id': 'sjx7MQrKNqD7uQ6Xc2UepQkBY7xbJxvcGViP',
'active': True
})
>>> response = Table.get_item(id='sjx7MQrKNqD7uQ6Xc2UepQkBY7xbJxvcGViP')
>>> print(response)
{'active': Decimal('1'), 'id': 'sjx7MQrKNqD7uQ6Xc2UepQkBY7xbJxvcGViP'}
尽管在 boto Github repo 中有很多关于在将 python float values to decimal.Decimal objects 或字符串发送到 dynamoDB 之前转换它们以保持数据完整性的讨论,但我一直无法找到任何关于正在转换的布尔值。
AWS documentation indicates that a boolean is an acceptable datatype and doesn't mention anything about it being converted to a string like numbers are. But, there is a cryptic method for Table called use_boolean() 没有文档。所以,我很困惑。
这是其他人遇到的问题吗?如果是这样,有什么解释吗?如果不是,有什么线索说明为什么我的构建会这样做吗?
我自己 运行 遇到了同样的问题,并在下面的评论中进一步研究 use_boolean()
并找到了解决方案。
似乎只在 2015 年 1 月 17 日在此拉取请求中将对 dynamoDB 中的 Boolean
类型的支持添加到 boto:
https://github.com/boto/boto/pull/2667
这个补丁相对较新(如果你认为新的 <1 年)所以可以解释为什么 use_boolean()
方法没有很好的记录,但该方法似乎类似于 use_decimals()
方法,在普通的旧 boto.dynamodb
教程中有详细记录:
http://boto.readthedocs.org/en/latest/dynamodb_tut.html#working-with-decimals
在拉取请求中,您可以看到围绕 boto 的那些已经将其布尔类型强制转换为 int 的用户的重要性以及如何保持向后兼容性的讨论。
拉取请求引入了一个 NonBooleanDynamizer
声明为默认值 _dynamizer
;除非您在 table 对象上调用 use_boolean()
方法。补丁最相关的部分如下:
https://github.com/kain-jy/boto/commit/886c4bf1877538a6acc28dd5f9bdd1c8f1c30dd9#diff-454bd7ad5c48dd01834d852f01e4b573R114
下面应该更清楚地说明如何使用 boto.dynamodb2
(我还没有完全研究过普通旧 boto.dynamodb
的等价物,但是 use_boolean 函数参数的散布在代码库建议有一种方法,如果需要的话):
>>> from boto.dynamodb2.fields import HashKey
>>> from boto.dynamodb2.table import Table
>>> data = {'true': True, 'false': False, 'one': 1, 'zero': 0}
>>> table = Table.create('q32109154-test1', schema=[HashKey('hkey')])
>>> data['hkey'] = 'test1'
>>> table.put_item(data=data)
>>> result = table.get_item(hkey='test1')
>>> print [(k, v) for k, v in result.items()]
[(u'hkey', u'test1'), (u'zero', Decimal('0')), (u'true', Decimal('1')), (u'false', Decimal('0')), (u'one', Decimal('1'))]
>>> table = Table.create('q32109154-test2', schema=[HashKey('hkey')])
>>> data['hkey'] = 'test2'
>>> table.use_boolean()
>>> table.put_item(data=data)
>>> result = table.get_item(hkey='test2')
>>> print [(k, v) for k, v in result.items()]
[(u'hkey', u'test2'), (u'zero', Decimal('0')), (u'true', True), (u'false', False), (u'one', Decimal('1'))]