def test_main(self): def f(): pass docstring = "some docstring" g = partial_with_docstring(f, docstring) self.assertEqual(g.__doc__, docstring)
def test_partial_with_docstring(): def f(): pass docstring = "some docstring" g = partial_with_docstring(f, docstring) assert g.__doc__ == docstring
def test_partial_with_docstring_returns_value(): def f(a: int, b: int): return a + b docstring = "some docstring" g = partial_with_docstring(f, docstring, a=1) assert g.__doc__ == docstring assert g(b=2) == 3
def test_partial_with_docstring_pass_args(): """ When one uses partial to bind the last argument it should be possible to provide arguments before as positional args. This matches the behaviour of functools.partial """ def f(a: int, b: int): return a + b docstring = "some docstring" g = partial_with_docstring(f, docstring, b=1) assert g.__doc__ == docstring assert g(2) == 3
def __init__(self, parent: 'DG1062', name: str, channel: int) -> None: """ Args: parent: The instrument this channel belongs to name (str) channel (int) """ super().__init__(parent, name) self.channel = channel for param, unit in [("freq", "Hz"), ("ampl", "V"), ("offset", "V"), ("phase", "deg"), ("sample_rate", "1/s")]: self.add_parameter( param, unit=unit, get_cmd=partial(self._get_waveform_param, param), set_cmd=partial(self._set_waveform_param, param), ) self.add_parameter("waveform", get_cmd=partial(self._get_waveform_param, "waveform")) self.add_parameter( "impedance", get_cmd=f":OUTPUT{channel}:IMP?", set_cmd=f":OUTPUT{channel}:IMP {{}}", unit="Ohm", vals=vals.MultiType( vals.Ints(min_value=DG1062Channel.min_impedance, max_value=DG1062Channel.max_impedance), vals.Enum("INF", "MIN", "MAX", "HighZ")), get_parser=(lambda value: "HighZ" if float(value) > DG1062Channel. max_impedance else float(value)), set_parser=lambda value: "INF" if value == "HighZ" else value) self.add_parameter( "sync", get_cmd=f":OUTPUT{channel}:SYNC?", set_cmd=f"OUTPUT{channel}:SYNC {{}}", vals=vals.Enum(0, 1, "ON", "OFF"), ) self.add_parameter( "polarity", get_cmd=f":OUTPUT{channel}:GAT:POL?", set_cmd=f":OUTPUT{channel}:GAT:POL {{}}", vals=vals.OnOff(), val_mapping={ 1: 'POSITIVE', 0: 'NEGATIVE' }, ) self.add_parameter( "state", get_cmd=f"OUTPUT{channel}:STATE?", set_cmd=f"OUTPUT{channel}:STATE {{}}", ) self.add_parameter("duty_cycle", get_cmd=self._get_duty_cycle, set_cmd=self._set_duty_cycle, unit="%", vals=vals.Numbers(min_value=1, max_value=99), docstring=('This functions reads/sets the duty ' 'cycle for a square and pulse wave ' 'since these inheret a duty cycle.\n' 'For other waveforms it will give ' 'the user an error')) burst = DG1062Burst(cast(DG1062, self.parent), "burst", self.channel) self.add_submodule("burst", burst) # We want to be able to do the following: # >>> help(gd.channels[0].sin) # >>> gd.channels[0].sin(freq=2E3, ampl=1.0, offset=0, phase=0) # We do not use add_function as it is more cumbersome to use. for waveform in self.waveforms: f = partial_with_docstring( self.apply, docstring="Args: " + ", ".join(self.waveform_params[waveform]), waveform=waveform) setattr(self, waveform.lower(), f) # Retreive current waveform from device self.waveform()