Wrapping this class around a Distributions disables setattr and makes the wrapped objects constant.
Source code in emod_api/demographics/PreDefinedDistributions.py
| class ConstantDistribution:
"""
Wrapping this class around a Distributions disables __setattr__ and makes the wrapped objects constant.
"""
def __init__(self, distribution):
self.distribution = distribution
def to_dict(self):
"""
Calls the to_dict() method of the wrapped distribution.
"""
return self.distribution.to_dict()
def copy(self):
"""
Creates a deepcopy of the wrapped Distribution object.
"""
return copy.deepcopy(self.distribution)
def __getattr__(self, item):
return self.distribution.__dict__[item]
def __setattr__(self, key, value):
if key == "distribution": # only allow to set member 'distribution'
vars(self)["distribution"] = value
else:
raise Exception(
"The object is constant and cannot be changed. Please use copy() to make a copy of the object.")
|
copy()
Creates a deepcopy of the wrapped Distribution object.
Source code in emod_api/demographics/PreDefinedDistributions.py
| def copy(self):
"""
Creates a deepcopy of the wrapped Distribution object.
"""
return copy.deepcopy(self.distribution)
|
to_dict()
Calls the to_dict() method of the wrapped distribution.
Source code in emod_api/demographics/PreDefinedDistributions.py
| def to_dict(self):
"""
Calls the to_dict() method of the wrapped distribution.
"""
return self.distribution.to_dict()
|