Skip to content

Commit

Permalink
Fix performance issue when command table active validation is off (#269)
Browse files Browse the repository at this point in the history
* Do not validate command table in as_dict() when active validation is off
  • Loading branch information
markheik committed Nov 2, 2023
1 parent 7e7bccb commit 4c95859
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/zhinst/toolkit/command_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,11 @@ def as_dict(self) -> dict:
:class:`~zhinst.toolkit.exceptions.ValidateError`: The command table
does not correspond to the given JSON schema.
.. versionchanged:: 0.6.2
Removed validation when `active_validation` is set to `False`.
This improves performance when validation is not needed.
.. versionchanged:: 0.4.2
Removed `$schema` key from resulting dictionary.
Expand All @@ -474,7 +479,8 @@ def as_dict(self) -> dict:
"header": self._header.as_dict(),
"table": self._table.as_list(),
}
_validate_instance(result, self._ct_schema)
if self.active_validation:
_validate_instance(result, self._ct_schema)
return result

def update(self, command_table: t.Union[str, dict]) -> None:
Expand All @@ -493,7 +499,8 @@ def json_to_dict(json_: t.Union[str, t.Dict]) -> t.Dict:
return json_ # type: ignore[return-value]

command_table = json_to_dict(copy.deepcopy(command_table))
_validate_instance(command_table, self._ct_schema)
if self._active_validation:
_validate_instance(command_table, self._ct_schema)

def build_nodes(path: t.Optional[ParentEntry], index: int, obj: dict):
for k, v in obj.items():
Expand Down
2 changes: 2 additions & 0 deletions tests/command_table/test_command_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ def test_command_table_active_validation_table(command_table_schema):
ct.table[999999]
ct = CommandTable(command_table_schema, active_validation=False)
ct.table[999999].amplitude00.value = 1
ct.as_dict()
ct.active_validation = True
with pytest.raises(ValidationError):
ct.as_dict()

Expand Down

0 comments on commit 4c95859

Please sign in to comment.