Message326845
OK, I appreciate the response. The MappingProxy objects I am working with are in a nested data structure, so accessing them to coerce them directly isn't feasible. I created
class MappingProxyEncoder(JSONEncoder):
def default(self, obj):
if isinstance(obj, MappingProxyType):
return obj.copy()
return JSONEncoder.default(self, obj)
and using that works fine for json.dumps (passes all my tests, at least). But I suspect that obj.copy() is more expensive than the code I'm replacing, which is a local implementation of ImmutableDict I was trying to do away with, following a readthrough of PEP 416. I wonder if there's a cheaper way to get at the underlying dictionary.
I suspect I'm not the only python user who foolishly expected MappingProxy to be a drop-in replacement for local ImmutableDict implementations. Maybe this ticket will be useful for others to read. |
|
Date |
User |
Action |
Args |
2018-10-02 03:01:36 | Michael Smith2 | set | recipients:
+ Michael Smith2, rhettinger, bob.ippolito |
2018-10-02 03:01:36 | Michael Smith2 | set | messageid: <1538449296.36.0.545547206417.issue34858@psf.upfronthosting.co.za> |
2018-10-02 03:01:36 | Michael Smith2 | link | issue34858 messages |
2018-10-02 03:01:35 | Michael Smith2 | create | |
|