I have a class, that is used to translate binary stream to human readable. I want to translate it both ways, because I send and receive binary messages. Attributes of this class are made mostly the same way - take the bytes from startbyte to stopbyte and decode them - so I made a decision to use a property to do that. But can I make a general "property" that will be used when defining my class attributes?
class Packet(object):
def __init__(self, data):
self.data = data
def standard_getter(startbyte, stopbyte):
def getter(self):
return decode(self.data[startbyte:stopbyte])
return getter
def standard_setter(startbyte, stopbyte):
def setter(self, value):
self.data[startbyte:stopbyte] = encode(value)
return setter
# the way I define properties by now:
protocol_type = property(standard_getter(16, 18), standard_setter(16, 18))
protocol_sub_type = property(standard_getter(18, 20), standard_setter(18, 20))
# the way I would like to do it:
protocol_type = property(standard_property(16, 18))
# or
protocol_type = standard_property(16, 18)
I tried to define a function, that takes two arguments and returns property(getter, setter), but always I'm stuck in giving "self" instance to the function. Is there a nice way I can make it?