Esempio n. 1
0
    def upload_to_device(self,
                         ct: t.Union[CommandTable, str, dict],
                         *,
                         validate: bool = True) -> None:
        """Upload command table into the device.

        The command table can either be specified through the dedicated
        ``CommandTable`` class or in a raw format, meaning a json string or json
        dict. In the case of a json string or dict the command table is
        validated by default against the schema provided by the device.

        Args:
            ct: Command table.
            validate: Flag if the command table should be validated. (Only
                applies if the command table is passed as a raw json string or
                json dict)

        Raises:
            RuntimeError: If the command table upload into the device failed.
            zhinst.toolkit.exceptions.ValidationError: Incorrect schema.
        """
        try:
            self.data(json.dumps(ct.as_dict()))  # type: ignore
        except AttributeError:
            if validate:
                ct_new = CommandTable(self.load_validation_schema())
                ct_new.update(ct)
                self.upload_to_device(ct_new)
            elif isinstance(ct, str):
                self.data(ct)
            else:
                self.data(json.dumps(ct))
        self.check_status()
Esempio n. 2
0
    def load_from_device(self) -> CommandTable:
        """Load command table from the device.

        Returns:
            command table.
        """
        ct = CommandTable(self.load_validation_schema())
        ct.update(self.data())
        return ct
Esempio n. 3
0
def test_ct_clear(command_table_schema):
    ct2 = CommandTable(command_table_schema)
    ct = CommandTable(command_table_schema)
    ct.table[0].amplitude00.value = 1
    ct.header.userString = "Foo bar"
    ct.clear()
    assert ct.as_dict() == ct2.as_dict()
Esempio n. 4
0
def test_modify_completed_command_table(command_table_schema,
                                        command_table_completed):
    ct = CommandTable(command_table_schema)
    ct.update(command_table_completed)
    assert ct.table[0].amplitude00.as_dict() == {
        "increment": False,
        "value": 0,
    }
    ct.table[0].amplitude00.value = 1
    ct.table[0].amplitude00.increment = True
    assert ct.table[0].amplitude00.as_dict() == {
        "increment": True,
        "value": 1,
    }
Esempio n. 5
0
def test_load_completed_command_table(command_table_schema,
                                      command_table_completed):
    ct = CommandTable(command_table_schema)
    ct.update(command_table_completed)
    assert command_table_completed == ct.as_dict()

    ct.update(json.dumps(command_table_completed))
    assert command_table_completed == ct.as_dict()
Esempio n. 6
0
def test_initialize_command_table_str_json():
    with open("tests/data/command_table_schema_v1.json") as file:
        data = file.read()
    obj = CommandTable(data)
    assert isinstance(obj._ct_schema, dict)
Esempio n. 7
0
def test_initialize_command_table_dict_json(command_table_schema):
    obj = CommandTable(command_table_schema)
    assert isinstance(obj._ct_schema, dict)
Esempio n. 8
0
def command_table(command_table_schema):
    yield CommandTable(command_table_schema)
Esempio n. 9
0
def test_command_table_schema_version_mismatch(command_table_schema,
                                               command_table_completed):
    command_table_completed["header"]["version"] = "2.0"
    ct = CommandTable(command_table_schema)
    with pytest.raises(ValidationError):
        ct.update(command_table_completed)