harp-tech / harp-python

A low-level interface to data collected with the Harp binary protocol
https://harp-tech.org/articles/python.html
MIT License
4 stars 2 forks source link

`DeviceReader` `registers` property should be indexable by register number #21

Closed bruno-f-cruz closed 10 months ago

bruno-f-cruz commented 10 months ago

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.