How to improve a large snapshot instance and make it more readable
See original GitHub issueI parsed an XML file and obtained a very large instance which contain many objects and lists. I want to make the parsed instance more readable and testable with snapshot data.
Here are my questions:
- What’s the best practice to make a snapshot of a large instance?
- How to make the snapshot file more readable (code highlighting, line breaks)?
- My IDE (VSCode) doesn’t recognize
.ambr
file, and will not have code highlighting automatically for that snapshot file. Any solution?
Snapshot Approach 1: stringify self.__dict__
def __repr__(self) -> str:
return f"{type(self).__name__}({self.__dict__})"
output:
Context({xml_id': 1234, 'links': [...], ...}) # a very long line
Pro: The type in the format Context({ ... })
would be highlighted with different color using Ruby/Python code highlighter.
Con: The class Context
has many list properties and the line of snapshot would be very long (> 10,000 columns). It’s hard to read and compare.
Snapshot Approach 2: JSON serialization
def __repr__(self) -> str:
return json.dumps(self.__dict__, indent=2)
output:
{
"xml_id": 1234,
"links": [
...
],
...
}
Pro: The line breaks make the large instance more readable.
Con: It’s hard to add types to custom objects like the format Context({ ... })
with json format. I want add the types to object strings and make them distinguished with string values by different highlighting color.
Any suggestion?
Issue Analytics
- State:
- Created 2 years ago
- Comments:12 (3 by maintainers)
Top GitHub Comments
@xareelee also you can look into the matchers option, which I think can implement these cases without the need for monkey patching, as it allows you to replace the object that will be serialized.
@iamogbz Thanks for the info. I think that it should be documented in
docs/
for other users.