Skip to content

Commit

Permalink
Wip reuse existing instrument rather than error
Browse files Browse the repository at this point in the history
  • Loading branch information
jenshnielsen committed Mar 22, 2022
1 parent e522014 commit 4c7078c
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions qcodes/instrument/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,26 @@ def __call__(cls, *args: Any, **kwargs: Any) -> Any:
Overloads `type.__call__` to add code that runs only if __init__ completes
successfully.
"""
if len(args) >= 1:
name = args[0]
else:
name = kwargs.get("name", None)
existing_instr = None
if name is not None:

try:
existing_instr = cls.find_instrument(name, cls) # type: ignore[attr-defined]
except (KeyError, TypeError):
pass

if existing_instr is not None:
log.info(f"Reusing existing instrument {name}")
# todo this is only really safe if this is the same instrument
# e.g. address should be the same but that is a trait only implemented
# in subclasses
return existing_instr


new_inst = super().__call__(*args, **kwargs)
is_abstract = new_inst._is_abstract()
if is_abstract:
Expand Down

0 comments on commit 4c7078c

Please sign in to comment.