Ok I'm working on getting better with python, so I'm not sure this is the right way to go about what I'm doing to begin with, but here's my current problem...
I need to get some information via a SOAP method, and only use part of the information now but store the entire result for future uses (we need to use the service as little as possible). Looking up the best way to access the service I figured suds was the way to go, and it was simple and worked like a charm to get the data. But now I want to save the result somehow, preferably serialized / in a database so I can pull it out later and use it the same.
What's the best way to do this, it looks like pickle/json isn't an option? Thanks!
Update Reading the top answer at How can I pickle suds results? gives me a better idea of why this isn't an option, I guess I'm stuck recreating a basic object w/ the information I need?
The solutions suggesed above lose valuable information about class names - it can be of value in some libraries like DFP client https://github.com/googleads/googleads-python-lib where entity types might be encoded in dynamically generated class names (i.e. TemplateCreative/ImageCreative)
Here's the solution I used that preserves class names and restores dict-serialized objects without data loss (except suds.sax.text.Text which would be converted into regular unicode objects and maybe some other types I haven't run into)
Here is what I came up with before researching and finding this answer. This actually works well for me on complex suds responses and also on other objects such as
__builtins__
since the solution is suds agnostic:This solution works and is actually faster. It also works on objects that don't have the
__keylist__
attribute.I ran a benchmark 100 times on a complex suds output object, this solutions run time was 0.04 to .052 seconds (0.045724287 average). While
recursive_asdict
solution above ran in .082 to 0.102 seconds so nearly double (0.0829765582 average).I then went back to the drawing board and re-did the function to get more performance out of it, and it does not need the
datetime
import. I leveraged in using the__keylist__
attribute, so this will not work on other objects such as__builtins__
but works nicely for suds object output:The run time was 0.18 - 0.033 seconds (0.0260889721 average), so nearly 4x as faster than the
recursive_asdict
solution.I updated the
recursive_asdict
example above to be compatible with python3 (items
instead ofiteritems
).I have been using following approach to convert Suds object into JSON:
Yep, I confirm the explanation I gave in the answer you refer to -- dynamically generated classes are not easily picklable (nor otherwise easily serializable), you need to extract all the state information, pickle that state, and reconstruct the tricky sudsobject on retrieval if you really insist on using it;-).
I made an implementation of a dummy class for Object intance of suds, and then being able to serialize. The FakeSudsInstance behaves like an original Suds Object instance, see below:
Now, after a suds call, for example below:
I hope it helps.