Skip to content

Commit 685120f

Browse files
committed
BaseOutput: add the get_output_dict method
In many cases of programmatic usage, you want to obtain all or a subset of outputs in one step, without having to list the outputs first and retrieve them one by one. Here we add the `BaseOutput.get_output_dict` method for this use case, which by default returns a dictionary of all available outputs. A subset can be selected via the `names` argument, and the outputs can be converted to the type of a desired library using `to`.
1 parent 913ddec commit 685120f

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

docs/getting_started.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ pw_out.outputs.fermi_energy
7575
If an output is not available, it will not be in the namespace.
7676
For programmatic access, use the `get_output` method.
7777

78+
79+
Finally, you can obtain a dictionary of all **available** outputs in your preferred library:
80+
81+
```python
82+
pw_out.get_output_dict(to='ase')
83+
```
84+
85+
7886
### Parsing a single output file
7987

8088
If you want to parse the contents of a single output file of the `pw.x` calculation, you can use the `from_files` method:

src/qe_tools/outputs/base.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,22 @@ def get_output(self, name: str, to=None):
5858
else output_data
5959
)
6060

61+
def get_output_dict(
62+
self, names: None | list[str] = None, to: None | str = None
63+
) -> dict:
64+
"""Return a dictionary of (selected) outputs.
65+
66+
Args:
67+
names (list[str]): Output names to include.
68+
to (str): Convert each output to a target library (e.g., "aiida", "ase",
69+
"pymatgen").
70+
71+
Returns:
72+
dict: Mapping from output name to value.
73+
"""
74+
names = names or self.list_outputs()
75+
return {name: self.get_output(name, to=to) for name in names}
76+
6177
def list_outputs(self, only_available: bool = True) -> list[str]:
6278
"""List the output names.
6379

tests/outputs/test_base.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,18 @@ def test_list_outputs(raw_outputs):
4646
"A",
4747
"not-parsed",
4848
]
49+
50+
51+
def test_get_output_dict(raw_outputs):
52+
assert TestBaseOutput(raw_outputs).get_output_dict() == {"A": 1}
53+
assert TestBaseOutput(raw_outputs).get_output_dict(
54+
[
55+
"A",
56+
]
57+
) == {"A": 1}
58+
with pytest.raises(KeyError):
59+
TestBaseOutput(raw_outputs).get_output_dict(
60+
[
61+
"B",
62+
]
63+
)

0 commit comments

Comments
 (0)