0

If there's no solution to this, I'll just change the list using the index. I don't want to go this route because I know I will not remember that maxpwm is list item #8 for example. The whole purpose of this is just better readability in the future because I plan on using these values in different functions to read and write.

class hwcfg:
    def __init__(self, a):
       self.file = a
       self.minauxinput1 = a[0]
       self.minauxinput2 = a[1]
       self.maxauxinput  = a[2]
       self.vtypenstsize = a[4]
       self.minpwm       = a[7]
       self.maxpwm       = a[8]
       self.mindac       = a[11]
       self.maxdac       = a[12]

bytes = bytearray.fromhex('7F 66 02 00 3A 00 13 00 44 54 02 13 17 00 44 54 31 00 31 00 3B 0E 00 00 A2 05 00 00 00 00 59 00 59 0D 00 00 40 00 A6 01 24 0D 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 7A 4B')
file = bytes[16:]
integers = []
i = struct.iter_unpack('h', file)
for x in i: 
    integers.append(x[0])
param = hwcfg(integers)
param.maxauxinput = 1

is there a way I can write this so that setting param.maxauxinput = 1 also sets file[2] or a[2] = 1?

  • Does this answer your question? [What's the pythonic way to use getters and setters?](https://stackoverflow.com/questions/2627002/whats-the-pythonic-way-to-use-getters-and-setters) – buran Sep 25 '22 at 06:35
  • I gave it a try and it didn't update the list item. – Isaac Zaiek Sep 25 '22 at 07:01

1 Answers1

1

You can use setters and getters, the pythonic way. Something within these lines:

import struct

class hwcfg:
    def __init__(self, a):
       self.file = a

    @property
    def minauxinput1(self):
        return self.file[0]

    @minauxinput1.setter
    def minauxinput1(self, value):
        self.file[0] = value

    # DO THE SAME for rest attributes

    #    self.minauxinput2 = a[1]
    #    self.maxauxinput  = a[2]
    #    self.vtypenstsize = a[4]
    #    self.minpwm       = a[7]
    #    self.maxpwm       = a[8]
    #    self.mindac       = a[11]
    #    self.maxdac       = a[12]

bytes = bytearray.fromhex('7F 66 02 00 3A 00 13 00 44 54 02 13 17 00 44 54 31 00 31 00 3B 0E 00 00 A2 05 00 00 00 00 59 00 59 0D 00 00 40 00 A6 01 24 0D 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 7A 4B')
file = bytes[16:]
integers = []
i = struct.iter_unpack('h', file)
for x in i: 
    integers.append(x[0])
param = hwcfg(integers)
print(param.file)
print(param.minauxinput1)
param.minauxinput1 = 1
print(param.file)
print(param.minauxinput1)
param.file = [51, 49, 3643, 0, 1442, 0, 0, 89, 3417, 0, 64, 422, 3364, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 19322]
print(param.file)
print(param.minauxinput1)

output

[49, 49, 3643, 0, 1442, 0, 0, 89, 3417, 0, 64, 422, 3364, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 19322]
49
[1, 49, 3643, 0, 1442, 0, 0, 89, 3417, 0, 64, 422, 3364, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 19322]
1
[51, 49, 3643, 0, 1442, 0, 0, 89, 3417, 0, 64, 422, 3364, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 19322]
51
buran
  • 13,682
  • 10
  • 36
  • 61