Skip to content

Commit

Permalink
make instrument optional in Function too
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcjohnson committed Feb 22, 2016
1 parent d035740 commit ce89956
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 39 deletions.
2 changes: 1 addition & 1 deletion qcodes/instrument/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
83 changes: 45 additions & 38 deletions qcodes/instrument/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down

0 comments on commit ce89956

Please sign in to comment.