When I set an element of a dictionary in a class via property decorator, the @property getter will be called. This is a problem when I want the getter to do something to the output.
Background
The topic is related to a chemistry project.
I want to make a more readable and more accessible code, instead of working with indexes, eq. new = self.species['CO2'] * fraction
is better then self.species[14] * fraction
I also tried Correct usage of a getter/setter for dictionary values but it can not solve the setter/getter problem.
Currently I go around this issue by dissabling the getter, defining a get_dict
function and only allow the whole dictionary to be set.
But with this approach I can not simply loop via arrays (dictA:dict
and new_values:numpy array
):
for i,value in enumerate(dictA):
dictA[value] = new_values[i]
Running Example
class example():
def __init__(self):
self._propA = {'A':1,'B':0,'C':0}
@property
def propA(self):
print('Getter')
return normalize_dict(self._propA)
@propA.setter
def propA(self,propB:dict):
print('Setter')
match = list(set(propB).difference(self._propA))
if match:
raise ValueError('{match} is/are not part of the required input: {listing}'.format(match=match,listing=list(self._propA.keys())))
else:
for i,value in enumerate(propB):
self._propA[value] = propB[value]
return self._propA
Supporting code
def normalize_dict(inquiry: dict):
inquiry_new = {}
try:
for i,value in enumerate(dict(inquiry)):
inquiry_new[value] = inquiry[value]
except TypeError:
error_string = 'Not a dictionary type class!'
raise TypeError(error_string)
for i,(valA,valB) in enumerate(inquiry_new.items()):
if type(valB)!=float and type(valB)!=int:
raise ValueError(valB,'is not a number')
if float(valB) < 0:
print ('Input is negative. They are ignored!')
continue
sum = 0
for i,(valA,valB) in enumerate(inquiry_new.items()):
if valB < 0:
valB = 0
sum += valB
for i,(valA,valB) in enumerate(inquiry_new.items()):
inquiry_new[valA] = valB/sum
return inquiry_new
Results
main.py
:
test = example()
test.propA = {'A':5,'B':4,'C':1}
print(test.propA)
test.propA = { 'A':1 }
test.propA = { 'B':5 }
test.propA = { 'C':4 }
print(test.propA)
test.propA['A'] = 5
test.propA['B'] = 4
test.propA['C'] = 1
print(test.propA)
Output
Setter
Getter
{'A': 0.5, 'B': 0.4, 'C': 0.1}
Setter
Setter
Setter
Getter
{'A': 0.1, 'B': 0.5, 'C': 0.4}
Getter
Getter
Getter
Getter
{'A': 0.1, 'B': 0.5, 'C': 0.4}
Wanted Output
Setter
Getter
{'A': 0.5, 'B': 0.4, 'C': 0.1}
Setter
Setter
Setter
Getter
{'A': 0.1, 'B': 0.5, 'C': 0.4}
Setter
Setter
Setter
Getter
{'A': 0.5, 'B': 0.4, 'C': 0.1}
Issues
As you can see from the output the 'Getter' is called instead of the 'Setter'.