-
Notifications
You must be signed in to change notification settings - Fork 104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pretty print json for human readable files #2706
Conversation
I would say that is not important and that you should feel free to bump those to |
@@ -198,4 +198,4 @@ def _dump_ok_file(self): | |||
|
|||
def _dump_status_json(self): | |||
with open(self.STATUS_json, "w") as fp: | |||
json.dump(self.status_dict, fp, indent=1) | |||
json.dump(self.status_dict, fp, indent=4) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a home made JSON parser somewhere. Just make sure that this file either is not read by that parser, or if it is, that the parser can deal with this indentation. 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was hoping for the test to detect that 😁
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🙏🏻
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not even sure the C-layer reads this file anymore... 🤔 It definitively checks whether it exists (and removes it upon starting a forward model) to deduce running state, but I'm not sure it actually loads the file. It appears that this is all done in the Python layer... If so, there is no worries related to parsing it :)
Codecov Report
@@ Coverage Diff @@
## main #2706 +/- ##
==========================================
- Coverage 65.05% 64.95% -0.10%
==========================================
Files 651 651
Lines 53907 53657 -250
Branches 4610 4732 +122
==========================================
- Hits 35069 34854 -215
+ Misses 17358 17323 -35
Partials 1480 1480
Continue to review full report at Codecov.
|
ert/serialization/_serializer.py
Outdated
@@ -31,6 +31,8 @@ async def decode_from_path( | |||
|
|||
class _json_serializer(Serializer): | |||
def encode(self, obj: Any, *args: Any, **kwargs: Any) -> str: | |||
if "indent" not in kwargs: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to move this into the function declaration as an optional argument and then afterwards passed explicitly to dumps
. It should yield the same behaviour and maybe look a bit less wacky :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that worked! Looks better now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, pylint
complained:
************* Module ert.serialization._serializer
ert/serialization/_serializer.py:33:4: W0221: Number of parameters was 4 in 'Serializer.encode' and is now 5 in overridden '_json_serializer.encode' method (arguments-differ)
So either go wacky, add pyllint exception, or add indent
as a argument to all encode()
implementations.
) | ||
|
||
# Most compact json serialization: | ||
assert json_serializer.encode( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An alternative method here would be to just drop all whitespaces both from obj_json
and the result of json_serializer
. It might be easier to read in the future and it removes the need to ever toggle indent
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure I got your question? There are multiple ways to reach a compact json serialization, one way is through the options to encode()
. This test is basically testing that separators
is passed on while we are overriding indent
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the late reply. I retract my point :)
An argument against this, is that raw JSON will use more space (a lot of Another argument against, it that transport will marginally be slowed down due to more stuff to compress. Both of these arguments are also against the use of JSON at all, but that's beyond the scope of this PR. Wouldn't |
That is correct. Avoiding whitespace for speed reasons is possibly premature optimization. |
Fair enough. |
Setting |
No, the debugging argument is good, and 4 spaces is the sweet spot. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are good arguments both ways, but this is also easy to change at any point based on new data. I suggest we merge this as is now :)
Issue
Resolves #318 once more..
Approach
Add
indent=4
to occurences ofjson.dump()
whenever it writes to a file that a human might read.There are some occurences ofindent=1
already in the code base, not sure if consistency with that is important, or if we should try to be consistent with howblack
would format the same dictionaries in source codeChanged existing
indent=1
intoindent=4
.Considered to add
sort_keys=True
also, but skipped it, might confuse.Occurences of
json.dump()
in tests are not touched.Pre review checklist
Adding labels helps the maintainers when writing release notes, see sections and the
corresponding labels here: https://github.com/equinor/ert/blob/main/.github/release.yml