Skip to content
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

Improve formatting of job results in CLI command #34

Merged
merged 5 commits into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,36 @@
* An exception is now raised when saving a refresh token with invalid characters.
[#31](https://github.com/XanaduAI/xanadu-cloud-client/pull/31)

* Job results are now displayed using the `pprint` module.
[#34](https://github.com/XanaduAI/xanadu-cloud-client/pull/34)

Before:

```json
{
"output": [
"[[0 0 0 0]\n [0 0 0 0]\n [0 0 0 0]\n [0 0 0 0]\n [0 0 0 0]]"
]
}
```

After:

```json
{
"output": [[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]]
}
```

### Contributors

This release contains contributions from (in alphabetical order):

[Jack Woehr](https://githup.com/jwoehr), [Hudhayfa Zaheem](https://github.com/HudZah).
[Mikhail Andrenkov](https://github.com/Mandrenkov), [Jack Woehr](https://githup.com/jwoehr), [Hudhayfa Zaheem](https://github.com/HudZah).

## Release 0.2.1 (current release)

Expand Down
15 changes: 13 additions & 2 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""

import json
from inspect import cleandoc

import numpy as np
import pytest
Expand Down Expand Up @@ -72,7 +73,7 @@ def language(self):
return "blackbird:1.0"

def get_result(self, integer_overflow_protection=True):
return {"output": [np.zeros((4, 4))]}
return {"output": [np.zeros((4, 4))], "metadata": np.ones((2, 2))}

def cancel(self):
pass
Expand Down Expand Up @@ -265,7 +266,17 @@ def test_circuit(self):
def test_result(self):
"""Tests that the result of a job can be retrieved."""
have_result = xcc.commands.get_job(id="foo", result=True)
want_result = json.dumps({"output": [str(np.zeros((4, 4)))]}, indent=4)
want_result = cleandoc(
"""
{
"metadata": [[1.0, 1.0], [1.0, 1.0]],
"output": [[[0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0]]]
}
"""
)
assert have_result == want_result

def test_invalid_number_of_flags(self):
Expand Down
19 changes: 18 additions & 1 deletion xcc/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import json
import sys
from functools import wraps
from pprint import pformat
from typing import Any, Callable, List, Mapping, Sequence, Tuple, Union

import fire
import numpy as np
from fire.core import FireError
from fire.formatting import Error
from pydantic import ValidationError
Expand Down Expand Up @@ -251,7 +253,22 @@ def get_job(
if status:
return job.status
if result:
return job.get_result(integer_overflow_protection=False)
result = job.get_result(integer_overflow_protection=False)

def convert_to_list(value: Union[np.ndarray, List[np.ndarray]]) -> List:
"""Converts the given job result value into a native Python list."""
if isinstance(value, np.ndarray):
return value.tolist()

return [array.tolist() for array in value]

# Convert all NumPy arrays into lists to take advantage of pprint.
list_result = {key: convert_to_list(value) for key, value in result.items()}
Mandrenkov marked this conversation as resolved.
Show resolved Hide resolved

# Use "" instead of '' to ensure the output is compatible with JSON.
json_result = pformat(list_result).replace("'", '"')
# Apply the same formatting rules as the JSON dumper in beautify().
return json_result.replace("{", "{\n ").replace("\n", "\n ").replace("}", "\n}")

return job.overview

Expand Down