Skip to content

Instantly share code, notes, and snippets.

@mikejs
Created July 11, 2010 22:55
Show Gist options
  • Save mikejs/471919 to your computer and use it in GitHub Desktop.
Save mikejs/471919 to your computer and use it in GitHub Desktop.
import unicodedata
from pymongo.son import SON
from pymongo.son_manipulator import SONManipulator
class NormalizeUnicode(SONManipulator):
def __init__(self, form="NFKD"):
self.__form = form
def transform_incoming(self, son, collection):
def transform_value(value):
if isinstance(value, unicode):
return unicodedata.normalize(self.__form, value)
elif isinstance(value, list):
return [transfom_value(v) for v in value]
elif isinstance(value, dict):
return transform_dict(SON(value))
return value
def transform_dict(object):
for (key, value) in object.items():
object[key] = transform_value(value)
return object
return transform_dict(SON(son))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment