The class DeviceReader contains a map of all the registers indexed by the register name. This makes it unnecessarily difficult, for a single register data file, to find which reader to use to read from that register if the file name does not contain metadata regarding the name of the reader. I suggest keeping a private dictionary in the class that maps RegisterNumber(int) -> RegisterName(str).
This new map could be used by overloading the __getitem__ method of the class:
map : Dict[int, str]
def __getitem__(self, key: Union[str, int]):
if isinstance(key, int):
return self.registers[self.map[key]]
elif isinstance(key, str):
return self.registers[key]
else:
raise TypeError(f"key must be int or str, not {type(key)}")
This dictionary could be easily created when the init of the constructor is called by searching the register map.
The class DeviceReader contains a map of all the registers indexed by the register name. This makes it unnecessarily difficult, for a single register data file, to find which reader to use to read from that register if the file name does not contain metadata regarding the name of the reader. I suggest keeping a private dictionary in the class that maps RegisterNumber(int) -> RegisterName(str).
This new map could be used by overloading the
__getitem__
method of the class:This dictionary could be easily created when the init of the constructor is called by searching the register map.