From 0ca8976bdd7bdd19294baa78a5cd95f4eea28688 Mon Sep 17 00:00:00 2001 From: Mikhail Andrenkov Date: Wed, 15 Jun 2022 14:21:24 -0400 Subject: [PATCH 1/5] Use `pformat()` to format NumPy arrays in job results --- xcc/commands.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/xcc/commands.py b/xcc/commands.py index 94cc708..88406b2 100644 --- a/xcc/commands.py +++ b/xcc/commands.py @@ -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 @@ -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()} + + # 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 From b444ed41ab4fe48c15b95f73637557d1c26aac4a Mon Sep 17 00:00:00 2001 From: Mikhail Andrenkov Date: Wed, 15 Jun 2022 14:22:02 -0400 Subject: [PATCH 2/5] Update tests to reflect new result formatting --- tests/test_commands.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/tests/test_commands.py b/tests/test_commands.py index a182f67..d2b429f 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -4,6 +4,7 @@ """ import json +from inspect import cleandoc import numpy as np import pytest @@ -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 @@ -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): From 38338dffb34a6890c9b24387cad70ddf407b9d0e Mon Sep 17 00:00:00 2001 From: Mikhail Andrenkov Date: Wed, 15 Jun 2022 14:22:36 -0400 Subject: [PATCH 3/5] Document improvement in changelog --- .github/CHANGELOG.md | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/.github/CHANGELOG.md b/.github/CHANGELOG.md index 1f109d5..692731a 100644 --- a/.github/CHANGELOG.md +++ b/.github/CHANGELOG.md @@ -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. + [#32](https://github.com/XanaduAI/xanadu-cloud-client/pull/32) + + 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) From 43895b34be8b59d4e763c5194b80216356d6aae5 Mon Sep 17 00:00:00 2001 From: Mikhail Andrenkov Date: Wed, 15 Jun 2022 14:23:52 -0400 Subject: [PATCH 4/5] Update PR number in changelog --- .github/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/CHANGELOG.md b/.github/CHANGELOG.md index 692731a..3975f1f 100644 --- a/.github/CHANGELOG.md +++ b/.github/CHANGELOG.md @@ -23,7 +23,7 @@ [#31](https://github.com/XanaduAI/xanadu-cloud-client/pull/31) * Job results are now displayed using the `pprint` module. - [#32](https://github.com/XanaduAI/xanadu-cloud-client/pull/32) + [#33](https://github.com/XanaduAI/xanadu-cloud-client/pull/33) Before: From c63aba2cfeedc96794cb2e1524c0676f4f90af70 Mon Sep 17 00:00:00 2001 From: Mikhail Andrenkov Date: Thu, 16 Jun 2022 09:19:17 -0400 Subject: [PATCH 5/5] Update PR number in .github/CHANGELOG.md Co-authored-by: Paul Finlay <50180049+doctorperceptron@users.noreply.github.com> --- .github/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/CHANGELOG.md b/.github/CHANGELOG.md index 3975f1f..9a6a017 100644 --- a/.github/CHANGELOG.md +++ b/.github/CHANGELOG.md @@ -23,7 +23,7 @@ [#31](https://github.com/XanaduAI/xanadu-cloud-client/pull/31) * Job results are now displayed using the `pprint` module. - [#33](https://github.com/XanaduAI/xanadu-cloud-client/pull/33) + [#34](https://github.com/XanaduAI/xanadu-cloud-client/pull/34) Before: