diff --git a/qcodes/instrument/base.py b/qcodes/instrument/base.py index 28c4276e652..3e1519be19c 100644 --- a/qcodes/instrument/base.py +++ b/qcodes/instrument/base.py @@ -64,7 +64,7 @@ def add_function(self, name, **kwargs): ''' if name in self.functions: raise KeyError('Duplicate function name {}'.format(name)) - self.functions[name] = Function(self, name, **kwargs) + self.functions[name] = Function(name=name, instrument=self, **kwargs) def snapshot_base(self, update=False): if update: diff --git a/qcodes/instrument/function.py b/qcodes/instrument/function.py index bde4b17e236..cd829fa45f6 100644 --- a/qcodes/instrument/function.py +++ b/qcodes/instrument/function.py @@ -6,41 +6,45 @@ class Function(Metadatable): - def __init__(self, instrument, name, call_cmd=None, async_call_cmd=None, + ''' + defines a function (with arbitrary parameters) that this instrument + can execute. + + You execute this function object like a normal function, or use its + .call method; or call it async with the .call_async method. + + name: the local name of this parameter + instrument: an instrument that handles this function + default None + + call_cmd: command to execute on instrument + - a string (with positional fields to .format, "{}" or "{0}" etc) + you can only use a string if an instrument is provided, + this string will be passed to instrument.write + - a function (with parameter count matching parameters list) + async_call_cmd: an async function to use for call_async, or for both + sync and async if call_cmd is missing or None. + + parameters: list of Validator objects, + one for each parameter to the Function + + parameter_parser: function to transform the input parameter(s) + to encoded value(s) sent to the instrument. + If there are multiple arguments, this function should accept all + the arguments in order, and return a tuple of values. + return_parser: function to transform the response from the instrument + to the final output value. + may be a type casting function like `int` or `float`. + If None (default), will not wait for or read any response + NOTE: parsers only apply if call_cmd is a string. The function forms + of call_cmd and async_call_cmd should do their own parsing. + + parse_function: DEPRECATED - use return_parser instead + ''' + def __init__(self, name, instrument=None, + call_cmd=None, async_call_cmd=None, parameters=[], parameter_parser=None, return_parser=None, parse_function=None, **kwargs): - ''' - defines a function (with arbitrary parameters) that this instrument - can execute. - - You execute this function object like a normal function, or use its - .call method; or call it async with the .call_async method. - - instrument: an instrument that handles this function - name: the local name of this parameter - - call_cmd: command to execute on instrument - - a string (with positional fields to .format, "{}" or "{0}" etc) - - a function (with parameter count matching parameters list) - async_call_cmd: an async function to use for call_async, or for both - sync and async if call_cmd is missing or None. - - parameters: list of Validator objects, - one for each parameter to the Function - - parameter_parser: function to transform the input parameter(s) - to encoded value(s) sent to the instrument. - If there are multiple arguments, this function should accept all - the arguments in order, and return a tuple of values. - return_parser: function to transform the response from the instrument - to the final output value. - may be a type casting function like `int` or `float`. - If None (default), will not wait for or read any response - NOTE: parsers only apply if call_cmd is a string. The function forms - of call_cmd and async_call_cmd should do their own parsing. - - parse_function: DEPRECATED - use return_parser instead - ''' super().__init__(**kwargs) self._instrument = instrument @@ -63,11 +67,14 @@ def _set_params(self, parameters): def _set_call(self, call_cmd, async_call_cmd, parameter_parser, return_parser): - ask_or_write = self._instrument.write - ask_or_write_async = self._instrument.write_async - if isinstance(call_cmd, str) and return_parser: - ask_or_write = self._instrument.ask - ask_or_write_async = self._instrument.ask_async + if self._instrument: + ask_or_write = self._instrument.write + ask_or_write_async = self._instrument.write_async + if isinstance(call_cmd, str) and return_parser: + ask_or_write = self._instrument.ask + ask_or_write_async = self._instrument.ask_async + else: + ask_or_write, ask_or_write_async = None, None self._call, self._call_async = syncable_command( param_count=self._param_count,