I have several classes for which i have defined some proprties doing their own stuff and returning a numpy ndarray:
def lin2db(x)
return 10*np.log10(x)
class Example:
...
@property
def snr(self):
# compute snr
return ndarray(snr)
@property
def eta(self):
# compute eta
return ndarray(eta)
...
For each of these proprties i have another one defined to returned the standard value transformed in decibels cause i would like to avoid explicitly trasform the base properties when i use them and it is much more comfortable to just add ‘_db’ in the attribute name when called:
@property
def snr_db(self):
return lin2db(self.snr)
@property
def eta_db(self):
return lin2db(self.snr)
...
However, this leads to prliferation of properties in my classes basivally doing always the same thing, which confuses a lot my code.
Is there a more concise way to retain teh same ieasyness in getting the original properties and the transformed one avoiding populating with so many properties definitioons?
I was thinking of extending the ndarray class / defining a wrapper class holding the ndarray and defining a db method doing the transformation so that i could do:
x = Thing()
x.snr # returns snr
x.snr.db # returns snr_db
but I would prefer to manage pure ndarrays, not extended classes or wrapper classes as the latter solution especially amy complicate the array manipulation. Is there a way to achieve my expected behaviour?
Emanuele Virgillito is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.