Skip to content

PreDefinedDistributions

ConstantDistribution

Wrapping this class around a Distributions disables setattr and makes the wrapped objects constant.

Source code in emod_api/demographics/PreDefinedDistributions.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
21
22
23
24
25
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
15
16
17
18
19
def to_dict(self):
    """
    Calls the to_dict() method of the wrapped distribution.
    """
    return self.distribution.to_dict()