Created
July 11, 2010 22:55
-
-
Save mikejs/471919 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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