-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for basic IO and calculations with default settings (#44)
Also changes multiplicity to spin to match xtb's usage
- Loading branch information
1 parent
2be7a6f
commit 8bb3e7f
Showing
37 changed files
with
6,230 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
"""Pretty print CJSON files but with flattened arrays for improved readability without | ||
significantly increasing footprint.""" | ||
|
||
import json | ||
|
||
|
||
def _flatten_arrays(data: dict) -> dict: | ||
"""Turn any lists of simple items (not dicts or lists) into strings.""" | ||
if isinstance(data, list): | ||
# Turn simple lists into flat strings | ||
if all(not isinstance(i, (dict, list)) for i in data): | ||
return json.dumps(data) | ||
# Recursively flatten any nested lists | ||
else: | ||
items = [_flatten_arrays(i) for i in data] | ||
return items | ||
elif isinstance(data, dict): | ||
# Recursively flatten all entries | ||
new = {k: _flatten_arrays(v) for k, v in data.items()} | ||
return new | ||
else: | ||
return data | ||
|
||
|
||
def cjson_dumps( | ||
cjson: dict, | ||
prettyprint=True, | ||
indent=2, | ||
**kwargs, | ||
) -> str: | ||
"""Serialize a CJSON object to a JSON formatted string. | ||
With the default `prettyprint` option, all simple arrays (not themselves containing | ||
objects/dicts or arrays/lists) will be flattened onto a single line, while all other | ||
array elements and object members will be pretty-printed with the specified indent | ||
level (2 spaces by default). | ||
`indent` and any `**kwargs` are passed to Python's `json.dumps()` as is, so the same | ||
values are valid e.g. `indent=0` will insert newlines while `indent=None` will | ||
afford a compact single-line representation. | ||
""" | ||
if prettyprint: | ||
flattened = _flatten_arrays(cjson) | ||
# Lists are now strings, remove quotes to turn them back into lists | ||
cjson_string = ( | ||
json.dumps(flattened, indent=indent, **kwargs) | ||
.replace('"[', '[').replace(']"', ']') | ||
) | ||
# Any strings within lists will have had their quotes escaped, so get rid of escapes | ||
cjson_string = cjson_string.replace(r'\"', '"') | ||
else: | ||
cjson_string = json.dumps(cjson, indent=indent, **kwargs) | ||
return cjson_string |
Oops, something went wrong.