Skip to content

Commit

Permalink
update driver example notebooks with typed kwargs
Browse files Browse the repository at this point in the history
  • Loading branch information
jenshnielsen committed May 13, 2024
1 parent 940d0f4 commit b5d6476
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,12 @@
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Logging hadn't been started.\n",
"Activating auto-logging. Current session state plus future input saved.\n",
"Filename : C:\\Users\\jenielse\\.qcodes\\logs\\command_history.log\n",
"Mode : append\n",
"Output logging : True\n",
"Raw input log : False\n",
"Timestamping : True\n",
"State : active\n",
"Qcodes Logfile : C:\\Users\\jenielse\\.qcodes\\logs\\220617-25816-qcodes.log\n"
]
}
],
"outputs": [],
"source": [
"from typing import TYPE_CHECKING\n",
"\n",
"if TYPE_CHECKING:\n",
" from typing_extensions import Unpack\n",
"import os\n",
"\n",
"import numpy as np\n",
Expand All @@ -44,7 +32,7 @@
" load_or_create_experiment,\n",
" plot_dataset,\n",
")\n",
"from qcodes.instrument import Instrument\n",
"from qcodes.instrument import Instrument, InstrumentBaseKWArgs\n",
"from qcodes.parameters import Parameter, ParameterWithSetpoints"
]
},
Expand Down Expand Up @@ -111,7 +99,7 @@
"\n",
"class OzzyLowScope(Instrument):\n",
"\n",
" def __init__(self, name, **kwargs):\n",
" def __init__(self, name: str, **kwargs: \"Unpack[InstrumentBaseKWArgs]\"):\n",
"\n",
" super().__init__(name, **kwargs)\n",
"\n",
Expand Down Expand Up @@ -494,7 +482,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.13"
"version": "3.12.1"
},
"toc": {
"base_numbering": 1,
Expand Down
38 changes: 27 additions & 11 deletions docs/examples/writing_drivers/Creating-Instrument-Drivers.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,23 @@
"import ctypes # only for DLL-based instrument\n",
"import sys\n",
"import time\n",
"from typing import Any\n",
"from typing import TYPE_CHECKING, Any\n",
"\n",
"if TYPE_CHECKING:\n",
" from typing_extensions import (\n",
" Unpack, # can be imported from typing if python >= 3.12\n",
" )\n",
"\n",
"import numpy as np\n",
"\n",
"from qcodes import validators as vals\n",
"from qcodes.instrument import Instrument, InstrumentChannel, VisaInstrument\n",
"from qcodes.instrument import (\n",
" Instrument,\n",
" InstrumentBaseKWArgs,\n",
" InstrumentChannel,\n",
" VisaInstrument,\n",
" VisaInstrumentKWArgs,\n",
")\n",
"from qcodes.instrument_drivers.AlazarTech.utils import TraceParameter\n",
"from qcodes.parameters import ManualParameter, MultiParameter, Parameter"
]
Expand Down Expand Up @@ -196,10 +207,13 @@
" \"\"\"\n",
"\n",
" # all instrument constructors should accept **kwargs and pass them on to\n",
" # super().__init__\n",
" def __init__(self, name, address, **kwargs):\n",
" # super().__init__ By using Unpack[VisaKWArgs] we ensure that the instrument class takes exactly the same keyword args as the base class\n",
" # Visa instruments are also required to take name, address and terminator as arguments.\n",
" # name and address should be positional arguments and terminator a keyword argument with the default value set to the terminator that the\n",
" # instrument expects (check the instrument manual)\n",
" def __init__(self, name: str, address: str, terminator: str = \"\\r\", **kwargs: \"Unpack[VisaInstrumentKWArgs]\"):\n",
" # supplying the terminator means you don't need to remove it from every response\n",
" super().__init__(name, address, terminator=\"\\r\", **kwargs)\n",
" super().__init__(name, address, terminator=terminator, **kwargs)\n",
"\n",
" self.attenuation = Parameter(\n",
" \"attenuation\",\n",
Expand Down Expand Up @@ -266,20 +280,21 @@
" SMUA and SMUB.\n",
" \"\"\"\n",
"\n",
" def __init__(self, parent: Instrument, name: str, channel: str) -> None:\n",
" def __init__(self, parent: Instrument, name: str, channel: str, **kwargs: \"Unpack[InstrumentBaseKWArgs]\") -> None:\n",
" \"\"\"\n",
" Args:\n",
" parent: The Instrument instance to which the channel is\n",
" to be attached.\n",
" name: The 'colloquial' name of the channel\n",
" channel: The name used by the Keithley, i.e. either\n",
" 'smua' or 'smub'\n",
" **kwargs: Forwarded to base class.\n",
" \"\"\"\n",
"\n",
" if channel not in [\"smua\", \"smub\"]:\n",
" raise ValueError('channel must be either \"smub\" or \"smua\"')\n",
"\n",
" super().__init__(parent, name)\n",
" super().__init__(parent, name, **kwargs)\n",
" self.model = self._parent.model\n",
"\n",
" self.volt = Parameter(\n",
Expand Down Expand Up @@ -343,11 +358,12 @@
" tested with Keithley2614B\n",
" \"\"\"\n",
"\n",
" def __init__(self, name: str, address: str, **kwargs) -> None:\n",
" def __init__(self, name: str, address: str, terminator=\"\\n\", **kwargs: \"Unpack[VisaInstrumentKWArgs]\") -> None:\n",
" \"\"\"\n",
" Args:\n",
" name: Name to use internally in QCoDeS\n",
" address: VISA ressource address\n",
" terminator: read/write terminator for communication\n",
" **kwargs: kwargs are forwarded to the base class.\n",
" \"\"\"\n",
" super().__init__(name, address, terminator=\"\\n\", **kwargs)\n",
Expand Down Expand Up @@ -416,7 +432,7 @@
"class AlazarTechATS(Instrument):\n",
" dll_path = \"C:\\\\WINDOWS\\\\System32\\\\ATSApi\"\n",
"\n",
" def __init__(self, name, system_id=1, board_id=1, dll_path=None, **kwargs):\n",
" def __init__(self, name, system_id=1, board_id=1, dll_path=None, **kwargs: \"Unpack[InstrumentBaseKWArgs]\"):\n",
" super().__init__(name, **kwargs)\n",
"\n",
" # connect to the DLL\n",
Expand Down Expand Up @@ -471,7 +487,7 @@
"class SomeDLLInstrument(Instrument):\n",
" dll_path = \"C:\\\\WINDOWS\\\\System32\\\\ATSApi\"\n",
"\n",
" def __init__(self, name, dll_path=None, **kwargs):\n",
" def __init__(self, name, dll_path=None, **kwargs: \"Unpack[InstrumentBaseKWArgs]\"):\n",
" super().__init__(name, **kwargs)\n",
"\n",
" if sys.platform != \"win32\":\n",
Expand Down Expand Up @@ -557,7 +573,7 @@
" This is a virtual driver only and will not talk to your instrument.\n",
" \"\"\"\n",
"\n",
" def __init__(self, name, **kwargs):\n",
" def __init__(self, name: str, **kwargs: \"Unpack[InstrumentBaseKWArgs]\"):\n",
" super().__init__(name, **kwargs)\n",
"\n",
" # ManualParameter has an \"initial_value\" kwarg, but if you use this\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,15 @@
}
],
"source": [
"from typing import TYPE_CHECKING\n",
"\n",
"import numpy as np\n",
"\n",
"import qcodes.validators as vals\n",
"from qcodes.instrument.visa import VisaInstrument\n",
"from qcodes.instrument.visa import VisaInstrument, VisaInstrumentKWArgs\n",
"\n",
"if TYPE_CHECKING:\n",
" from typing_extensions import Unpack\n",
"\n",
"\n",
"class Weinschel8320(VisaInstrument):\n",
Expand All @@ -78,8 +83,8 @@
" Weinschel is formerly known as Aeroflex/Weinschel\n",
" \"\"\"\n",
"\n",
" def __init__(self, name, address, **kwargs):\n",
" super().__init__(name, address, terminator='\\r', **kwargs)\n",
" def __init__(self, name: str, address: str, terminator='\\r', **kwargs: 'Unpack[VisaInstrumentKWArgs]'):\n",
" super().__init__(name, address, terminator=terminator, **kwargs)\n",
"\n",
" self.add_parameter('attenuation', unit='dB',\n",
" set_cmd='ATTN ALL {:02.0f}',\n",
Expand Down

0 comments on commit b5d6476

Please sign in to comment.