Created
December 17, 2010 21:29
-
-
Save reedobrien/745735 to your computer and use it in GitHub Desktop.
A son manipulator for decimal.Decimal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from decimal import Decimal | |
from pymongo.son_manipulator import SONManipulator | |
class DecimalTransform(SONManipulator): | |
def transform_incoming(self, son, collection): | |
for (key, value) in son.items(): | |
if isinstance(value, Decimal): | |
son[key] = {'_type' : 'decimal', 'value' : unicode(value)} | |
elif isinstance(value, dict): | |
son[key] = self.transform_incoming(value, collection) | |
return son | |
def transform_outgoing(self, son, collection): | |
for (key, value) in son.items(): | |
if isinstance(value, dict): | |
if "_type" in value and value["_type"] == "decimal": | |
son[key] = Decimal(value['value']) | |
else: | |
son[key] = self.transform_outgoing(value, collection) | |
return son |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment