Skip to content

Instantly share code, notes, and snippets.

@mattgen88
Created June 4, 2013 04:15
Show Gist options
  • Save mattgen88/5703529 to your computer and use it in GitHub Desktop.
Save mattgen88/5703529 to your computer and use it in GitHub Desktop.
You'll never pass a dictionary that doesn't have anything other than strings and numbers to tornado's finish method. Why would you use it to return things like database entries from mongo containing BSON Object IDs or anything else for that matter. And of course you won't try and coerce it to a string automatically and just fail instead.
12 »···def finish(self, chunk=None):
13 »···»···def iterate(dictionary, comparator, mapping):
14 »···»···»···for key, value in dictionary.iteritems():
15 »···»···»···»···if isinstance(value, dict):
16 »···»···»···»···»···value = iterate(value, comparator, mapping)
17 »···»···»···»···else:
18 »···»···»···»···»···if comparator(value):
19 »···»···»···»···»···»···value = mapping(value)
20 »···»···»···»···dictionary[key] = value
21 »···»···»···return dictionary
22
23 »···»···def bson_check(data):
24 »···»···»···return isinstance (data, bson.ObjectId)
25
26 »···»···def bson_conversion(data):
27 »···»···»···return str(data)
28
29 »···»···# Because tornado does magic...
30 »···»···if isinstance(chunk, dict):
31 »···»···»···chunk = iterate(chunk, bson_check, bson_conversion)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment