1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
The to_js function supports Python integers, floats, mx.DateTime dates (if mx.DateTime can be imported), strings, None, sequences, and dictionaries. It will complain loudly if you try to pass another type. The reliance on type rather than interface is rather unfortunate, in that Python classes that emulate the behavior of the builtin containers may not be their subclasses; with some database drivers, for instance, the result set returned for a row behaves a bit like a list and a bit like a dictionary, but is an instance of neither. To marshal such an object with this code, you have to cast it first. The recipe could conceivably be refined by replacing the type checks for those containers with some other heuristic for determining whether an object is "close enough" to a list/tuple/dict, but no way of doing that that wasn't ugly and potentially error-prone has occurred to me.
Interface Checking. Idea: To avoid type checks, it might be possible to adapt Raymond Hettinger's "Metaclass for Interface Checking" recipe described at:
to determine if something was "close enough" to a list/tuple/dict etc.
Bad Date() Marshalling?