Skip to content

Instantly share code, notes, and snippets.

@reedobrien
Created December 17, 2010 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reedobrien/745735 to your computer and use it in GitHub Desktop.
Save reedobrien/745735 to your computer and use it in GitHub Desktop.
A son manipulator for decimal.Decimal
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