App class

hardware_control.base.App module

class hardware_control.base.App.App(ipaddress=None, dummy: bool = False)

Bases: QApplication

The main app class.

This class should be used to create the main Qt app in your program. It keeps track of all connected instruments, settings, and current data.

add_auto_update_instrument_parameter(instrument: str, parameter: str) None

Add an instrument’s parameter to a list for automatic updates.

The app class provides a mechanism to automaticaly update instrument parameters by querying the instrument in a regular interval.

To limit the number of requests and how much data we exchange with an instrument driver, not all parameters are automatically updated all the time. This function can be used to enable automatic updates.

add_continuous_command(instrument: str, command: str, status: bool = False) None

Add an instrument’s parameter to a list of continuously-called commands.

The app class provides a mechanism to call instrument commands once every regular interval. The command will not be called continuously unless the status parameter in the self.continuous_commands set is set to True.

add_dataset(dataset: Dataset) None

Add a dataset object to the app. See the Dataset base class for more details.

add_hook(instrument: str, parameter: str, when: str, function: Callable[[Any], Any]) None

Add a hook to a parameter or command.

Every hook function takes a single parameter: the value of the parameter to be set (set to None for commands). If a hook requires several parameters, one should pass these in when defining the hook using lambda functions, e.g.:

app.add_hook(instrument, parameter, ‘pre_set_hooks’, lambda x: change_ui(app, x, widget)

where app and widget are defined in the enironment where add_hook is called.

Parameters:
  • instrument – The name of the instrument

  • parameter – The name of the parameter

  • when – At what point the hook should be called. Possible options are: “post_read_hooks”, “pre_set_hooks”, “pre_call_hooks”.

  • function – The hook function to be added.

add_instrument(instrument: Instrument, ignore_flag: bool | None = False) None

Add an instrument object to the app. See the Instrument base class for more details.

add_load_settings_hook(instrument: str, filename: str | Path) None

Add a hook that loads a file with a dictionary of instrument settings.

Parameters:

filename – Name of JSON file to read settings from. See load_settings method for details on the formatting of the JSON file.

add_remote_instrument(name: str, address: str) None

Connect to a instrument driver on, for example, a different computer.

This is relatively straightforward since the ZMQ publishers that update the data model do not care about which computer they are running on. The main task is to create subscribers to the already existing publishers (from the app and on the remote side). This task is mostly handled by ZMQRemoteAdapter.

Parameters:
  • name – The remote instrument name

  • address – The ipaddress:port combination where the remote instrument is running

add_save_format(format_name: str, save_fxn: Callable[[Dataset, str], None]) None

Add a save format for a dataset.

Parameters:
  • format_name – Name of the new save format

  • save_fxn – The function used for the new save format. This function must take a Dataset object and a filename as argument.

add_skip_update_instrument_parameter(instrument: str, parameter: str) None

A method to skip automatic updates for certain parameters.

This is mostly used for parameters that are defined in a GUI but not in an instrument driver. For example, some scopes only have highZ inputs and no option to change the input impedance. The GUI might define this parameter to be automatically updated,vi which would lead to logging errors/warnings. By putting it on the skip list, the automated update will just not request an update.

add_widget(instrument: str, parameter: str, widget) None

Add a widget to a parameter.

This enables automatic updating if the value changes.

call_continuous_commands(priority=0) None

Call all commands that are to be called continuously.

call_instrument_command(instrument: str, command: str, priority: int = 1) None

Sends a command to the instrument via ZMQ.

Any pre_call_hooks will be called, and the command will only execute if none of the hooks returns None.

Parameters:
close() None

Closes the application.

Safely closes the application by disconnecting all instruments and saving any data specified to save automatically before close.

get_instrument_parameter(instrument: str, parameter: str, return_set_value: bool = False) str | None

Return the last value read back for the given instrument parameter.

This function does not communicate with the instrument.

is_parameter(instrument: str, parameter: str) bool

Check if the app knows about this instrument parameter.

list_instrument_parameters(instrument) list[str]

Compile a list of all the parameters for a specified instrument.

list_instruments() list[str]

Compile a list of all the instruments in the app.

load_settings_hook(instrument: str, filename: str | Path, skip_hooks: bool = True) None
parse_message_from_instruments() None

Check for messages from the instruments.

Call any defined hooks and update any widgets.

register_parameters_and_commands(instrument: str, read_parameters, set_parameters, commands) None

Adds all parameter names to a data dictionary and sets default values.

save_settings(filename: str | Path) None

Save all settings as JSON.

Parses out all set_value entries from app._data. The dictionary is then saved to a JSON file.

Parameters:

filename – Name of file to save set_value entries to

set_instrument_parameter(instrument: str, parameter: str, value: Any, priority: int = 1, skip_hooks=False) None

Send the value to the instrument thread via ZMQ.

Any pre_set_hooks will be called and the value will only be set if none of them returns None.

Parameters:
  • instrument – The name of the instrument

  • parameter – The name of the parameter

  • value – The value to be set (needs to be able to be converted to a string)

  • priority – The priority of the message. See: base.zmq_helper.add_ZMQ_message_to_queue()

  • skip_hooks – If True, don’t call the available hooks (usefull when loading settings from a file for example)

stop() None

Stop all threads and wait until they return.

update_instrument_parameter(instrument: str, parameter: str, priority: int = 1) None

Sends request to the instrument thread via ZMQ to query the parameter value.

Any post_read_hooks will be called.

Parameters:
update_instruments() None

Update the app’s dictionary of instrument parameter values in a desginated set of parameters.

hardware_control.base.App.ensure_instrument_exists(f)

Error check to make sure that an instrument exists in the app.

This decorator assumes that the first argument of the decorated function is the app class and the second argument is the instrument name.

hardware_control.base.App.ensure_parameter_and_instrument_exists(f)

Error check to make sure that a parameter/command exists in the app.

This decorator assumes that the first argument of the decorated function is the app class, the second argument is the instrument name, and the third is the parameter name.