Skip to content

Commit c231cd9

Browse files
committed
chore: address review comments
1 parent cad6c34 commit c231cd9

File tree

10 files changed

+29
-32
lines changed

10 files changed

+29
-32
lines changed

src/fastcs/attributes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def set_path(self, path: list[str]):
109109

110110
def __repr__(self):
111111
name = self.__class__.__name__
112-
path = ".".join([str(node) for node in self._path] + [self._name]) or None
112+
path = ".".join(self._path + [self._name]) or None
113113
datatype = self._datatype.__class__.__name__
114114

115115
return f"{name}(path={path}, datatype={datatype}, io_ref={self._io_ref})"

src/fastcs/controller.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -158,18 +158,18 @@ def add_sub_controller(
158158

159159
sub_controller.set_path(self.path + [name])
160160
self.__sub_controller_tree[name] = sub_controller
161-
super().__setattr__(str(name), sub_controller)
161+
super().__setattr__(name, sub_controller)
162162

163163
if isinstance(sub_controller.root_attribute, Attribute):
164-
self.attributes[str(name)] = sub_controller.root_attribute
164+
self.attributes[name] = sub_controller.root_attribute
165165

166166
@property
167167
def sub_controllers(self) -> dict[str, BaseController]:
168168
return self.__sub_controller_tree
169169

170170
def __repr__(self):
171171
name = self.__class__.__name__
172-
path = ".".join([str(p) for p in self.path]) or None
172+
path = ".".join(self.path) or None
173173
sub_controllers = list(self.sub_controllers.keys()) or None
174174

175175
return f"{name}(path={path}, sub_controllers={sub_controllers})"
@@ -219,11 +219,8 @@ async def disconnect(self) -> None:
219219

220220

221221
class ControllerVector(MutableMapping[int, Controller], BaseController):
222-
"""A collection of SubControllers, with an arbitrary integer index.
223-
An instance of this class can be registered with a parent ``Controller`` to include
224-
it's children as part of a larger controller. Each child of the vector will keep
225-
a string name of the vector.
226-
"""
222+
"""A controller with a collection of identical sub controllers distinguished
223+
by a numeric value"""
227224

228225
root_attribute: Attribute | None = None
229226

@@ -239,14 +236,22 @@ def __init__(
239236
for index, child in children.items():
240237
super().add_sub_controller(str(index), child)
241238

242-
def add_sub_controller(self, *args, **kwargs):
239+
def add_sub_controller(
240+
self, name: str, sub_controller: Controller | ControllerVector
241+
):
243242
raise NotImplementedError(
244243
"Cannot add named sub controller to ControllerVector. "
245-
"Use __setitem__ instead, for indexed sub controllers"
244+
"Use __setitem__ instead, for indexed sub controllers. "
245+
"E.g., vector[1] = Controller()"
246246
)
247247

248248
def __getitem__(self, key: int) -> Controller:
249-
return self._children[key]
249+
try:
250+
return self._children[key]
251+
except KeyError as exception:
252+
raise KeyError(
253+
f"ControllerVector does not have Controller with key {key}"
254+
) from exception
250255

251256
def __setitem__(self, key: int, value: Controller) -> None:
252257
if not isinstance(key, int):
@@ -266,9 +271,5 @@ def __iter__(self) -> Iterator[int]:
266271
def __len__(self) -> int:
267272
return len(self._children)
268273

269-
def children(self) -> Iterator[tuple[str, Controller]]:
270-
for key, child in self._children.items():
271-
yield str(key), child
272-
273-
def __hash__(self):
274-
return hash(id(self))
274+
def children(self) -> Iterator[tuple[int, Controller]]:
275+
yield from self._children.items()

src/fastcs/controller_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def __repr__(self):
4141
return (
4242
f"ControllerAPI("
4343
f"path={self.path}, "
44-
f"sub_apis=[{', '.join(str(sub_api) for sub_api in self.sub_apis.keys())}]"
44+
f"sub_apis=[{', '.join(sub_api for sub_api in self.sub_apis.keys())}]"
4545
f")"
4646
)
4747

src/fastcs/transport/epics/ca/ioc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def _add_sub_controller_pvi_info(pv_prefix: str, parent: ControllerAPI):
116116
child_name = (
117117
f"__{child.path[-1]}" # Sub-Controller of ControllerVector
118118
if child.path[-1].isdigit()
119-
else str(child.path[-1])
119+
else child.path[-1]
120120
)
121121

122122
_add_pvi_info(child_pvi, parent_pvi, child_name.lower())

src/fastcs/transport/epics/gui.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def __init__(self, controller_api: ControllerAPI, pv_prefix: str) -> None:
5353

5454
def _get_pv(self, attr_path: list[str], name: str):
5555
attr_prefix = ":".join(
56-
[self._pv_prefix] + [snake_to_pascal(str(node)) for node in attr_path]
56+
[self._pv_prefix] + [snake_to_pascal(node) for node in attr_path]
5757
)
5858
return f"{attr_prefix}:{snake_to_pascal(name)}"
5959

src/fastcs/transport/epics/pva/pvi_tree.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ class PviDevice(dict[str, "PviDevice"]):
4242
"""For creating a pvi structure in pva."""
4343

4444
pv_prefix: str
45-
controller_api: ControllerAPI | None
4645
description: str | None
4746
device_signal_info: _PviSignalInfo | None
4847

src/fastcs/transport/epics/util.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,4 @@
33

44

55
def controller_pv_prefix(prefix: str, controller_api: ControllerAPI) -> str:
6-
return ":".join(
7-
[prefix] + [snake_to_pascal(str(node)) for node in controller_api.path]
8-
)
6+
return ":".join([prefix] + [snake_to_pascal(node) for node in controller_api.path])

src/fastcs/transport/graphql/graphql.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def _process_commands(self, controller_api: ControllerAPI):
8686
def _process_sub_apis(self, root_controller_api: ControllerAPI):
8787
"""Recursively add fields from the queries and mutations of sub apis"""
8888
for controller_api in root_controller_api.sub_apis.values():
89-
name = "".join([str(node) for node in controller_api.path])
89+
name = "".join(controller_api.path)
9090
child_tree = GraphQLAPI(controller_api)
9191
if child_tree.queries:
9292
self.queries.append(

src/fastcs/transport/rest/rest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ async def attr_get() -> Any: # Must be any as response_model is set
101101

102102
def _add_attribute_api_routes(app: FastAPI, root_controller_api: ControllerAPI) -> None:
103103
for controller_api in root_controller_api.walk_api():
104-
path = [str(node) for node in controller_api.path]
104+
path = controller_api.path
105105

106106
for attr_name, attribute in controller_api.attributes.items():
107107
attr_name = attr_name.replace("_", "-")
@@ -151,7 +151,7 @@ async def command() -> None:
151151

152152
def _add_command_api_routes(app: FastAPI, root_controller_api: ControllerAPI) -> None:
153153
for controller_api in root_controller_api.walk_api():
154-
path = [str(node) for node in controller_api.path]
154+
path = controller_api.path
155155

156156
for name, method in root_controller_api.command_methods.items():
157157
cmd_name = name.replace("_", "-")

src/fastcs/transport/tango/dsr.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def _collect_dev_attributes(
6161
) -> dict[str, Any]:
6262
collection: dict[str, Any] = {}
6363
for controller_api in root_controller_api.walk_api():
64-
path = [str(node) for node in controller_api.path]
64+
path = controller_api.path
6565

6666
for attr_name, attribute in controller_api.attributes.items():
6767
attr_name = attr_name.title().replace("_", "")
@@ -109,8 +109,7 @@ def _wrap_command_f(
109109
) -> Callable[..., Awaitable[None]]:
110110
async def _dynamic_f(tango_device: Device) -> None:
111111
tango_device.info_stream(
112-
f"called {'_'.join([str(node) for node in controller_api.path])} "
113-
f"f method: {method_name}"
112+
f"called {'_'.join(controller_api.path)} f method: {method_name}"
114113
)
115114

116115
coro = method()
@@ -126,7 +125,7 @@ def _collect_dev_commands(
126125
) -> dict[str, Any]:
127126
collection: dict[str, Any] = {}
128127
for controller_api in root_controller_api.walk_api():
129-
path = [str(node) for node in controller_api.path]
128+
path = controller_api.path
130129

131130
for name, method in controller_api.command_methods.items():
132131
cmd_name = name.title().replace("_", "")

0 commit comments

Comments
 (0)