Skip to content
This repository has been archived by the owner on Oct 11, 2023. It is now read-only.

Commit

Permalink
Changes to work with a real PandA
Browse files Browse the repository at this point in the history
- Block names can have numbers and underscores in
- Type hints seem to work better on the class, not instance
- Added demo
- Added type hints function which doesn't look through parent classes
  • Loading branch information
coretl authored and olliesilvester committed Aug 8, 2023
1 parent 72b3517 commit 2ed483b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 7 deletions.
14 changes: 14 additions & 0 deletions docs/examples/panda_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from bluesky import RunEngine

# these three lines just let you use await statements
# #in ipython terminal with the Run Engine event loop.
from IPython import get_ipython
from ophyd.v2.core import DeviceCollector

from ophyd_epics_devices.panda import PandA

get_ipython().run_line_magic("autoawait", "call_in_bluesky_event_loop")
RE = RunEngine()

with DeviceCollector():
my_panda = PandA("TS-PANDA")
20 changes: 13 additions & 7 deletions src/ophyd_epics_devices/panda.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import atexit
import re
from enum import Enum
Expand Down Expand Up @@ -34,6 +36,10 @@
)
from p4p.client.thread import Context

from ophyd_epics_devices.utils import get_type_hints_no_inheritance

ctxt = Context("pva")


class PulseBlock(Device):
delay: SignalRW[float]
Expand Down Expand Up @@ -93,7 +99,7 @@ class PVIEntry(TypedDict, total=False):


def block_name_number(block_name: str) -> Tuple[str, int]:
m = re.match("^([a-z]+)([0-9]*)$", block_name)
m = re.match("^([0-9_a-z]+)([0-9]*)$", block_name)
assert m, f"Expected '<block_name><block_num>', got '{block_name}'"
name, num = m.groups()
return name, int(num or 1)
Expand Down Expand Up @@ -144,7 +150,7 @@ def _del_ctxt():

def verify_block(self, name: str, num: int):
"""Given a block name and number, return information about a block."""
anno = get_type_hints(self).get(name)
anno = get_type_hints(type(self)).get(name)

block: Device = Device()

Expand All @@ -164,8 +170,7 @@ async def _make_block(self, name: str, num: int, block_pv: str, sim: bool = Fals
sim mode then does a pvi call, and identifies this signal from the pvi call.
"""
block = self.verify_block(name, num)

field_annos = get_type_hints(block)
field_annos = get_type_hints_no_inheritance(type(block))
block_pvi = await pvi_get(block_pv, self.ctxt) if not sim else None

# finds which fields this class actually has, e.g. delay, width...
Expand Down Expand Up @@ -231,7 +236,7 @@ def _make_signal(self, signal_pvi: PVIEntry, dtype: Optional[Type] = None):
return signal_factory(dtype, "pva://" + read_pv, "pva://" + write_pv)

def set_attribute(self, name, num, block):
anno = get_type_hints(self).get(name)
anno = get_type_hints(type(self)).get(name)

# get_origin to see if it's a device vector.
if (anno == DeviceVector[PulseBlock]) or (anno == DeviceVector[SeqBlock]):
Expand All @@ -251,10 +256,11 @@ async def connect(self, sim=False) -> None:
pvi = await pvi_get(self._init_prefix + ":PVI", self.ctxt) if not sim else None
hints = {
attr_name: attr_type
for attr_name, attr_type in get_type_hints(self).items()
for attr_name, attr_type in get_type_hints_no_inheritance(
type(self)
).items()
if not attr_name.startswith("_")
}

# create all the blocks pvi says it should have,
if pvi:
for block_name, block_pvi in pvi.items():
Expand Down
13 changes: 13 additions & 0 deletions src/ophyd_epics_devices/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from typing import get_type_hints


# Use with types, not instances
def get_type_hints_no_inheritance(cls):
cls_hints = get_type_hints(cls)

for base_cls in cls.__bases__:
base_hints = get_type_hints(base_cls)
for base_hint_names in base_hints.keys():
if base_hint_names in cls_hints:
del cls_hints[base_hint_names]
return cls_hints
21 changes: 21 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from typing import get_type_hints

from ophyd_epics_devices.utils import get_type_hints_no_inheritance


def test_get_type_hints_no_inheritance():
class BaseClass:
base_integer: int
base_string: str

class SubClass(BaseClass):
integer: int
string: str

assert get_type_hints(SubClass) == {
"base_integer": int,
"base_string": str,
"integer": int,
"string": str,
}
assert get_type_hints_no_inheritance(SubClass) == {"integer": int, "string": str}

0 comments on commit 2ed483b

Please sign in to comment.