Skip to content

Commit 8e68af2

Browse files
Jammy2211Jammy2211
authored andcommitted
git ch0;10;1cMerge branch 'main' of github.com:rhayes777/PyAutoFit
2 parents c9d01ec + 84aa6f7 commit 8e68af2

4 files changed

Lines changed: 41 additions & 57 deletions

File tree

autofit/mapper/model_object.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,8 @@ def dict(self) -> dict:
296296
type_ = "tuple_prior"
297297
elif isinstance(self, Array):
298298
type_ = "array"
299+
elif isinstance(self, Constant):
300+
type_ = "constant"
299301
else:
300302
raise AssertionError(
301303
f"{self.__class__.__name__} cannot be serialised to dict"

autofit/mapper/prior/constant.py

Lines changed: 17 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
from autofit.mapper.model_object import ModelObject
44

55

6-
class Constant(ModelObject, Number):
6+
class Constant(float, ModelObject):
7+
def __new__(cls, value: float, id_=None):
8+
obj = super().__new__(cls, value)
9+
return obj
10+
711
def __init__(self, value: float, id_=None):
812
"""
913
Represents a constant value in a model.
@@ -18,63 +22,27 @@ def __init__(self, value: float, id_=None):
1822
value
1923
The constant value.
2024
"""
21-
super().__init__(
22-
id_=id_,
23-
)
25+
if isinstance(value, Constant):
26+
value = value.value
27+
28+
ModelObject.__init__(self, id_=id_)
2429
self.value = value
2530

2631
def __str__(self):
2732
return str(self.value)
2833

2934
def __eq__(self, other):
30-
if isinstance(other, (int, float)):
31-
return self.value == other
32-
return super().__eq__(other)
35+
if isinstance(other, ModelObject):
36+
return ModelObject.__eq__(self, other)
37+
return self.value == other
38+
39+
def __ne__(self, other):
40+
if isinstance(other, ModelObject):
41+
return ModelObject.__ne__(self, other)
42+
return self.value != other
3343

3444
def __hash__(self):
3545
return hash(self.id)
3646

3747
def dict(self):
3848
return {"type": "Constant", "value": self.value}
39-
40-
def __add__(self, other):
41-
return self.value + other
42-
43-
def __sub__(self, other):
44-
return self.value - other
45-
46-
def __mul__(self, other):
47-
return self.value * other
48-
49-
def __truediv__(self, other):
50-
return self.value / other
51-
52-
def __pow__(self, other):
53-
return self.value**other
54-
55-
def __radd__(self, other):
56-
return other + self.value
57-
58-
def __rsub__(self, other):
59-
return other - self.value
60-
61-
def __rmul__(self, other):
62-
return other * self.value
63-
64-
def __rtruediv__(self, other):
65-
return other / self.value
66-
67-
def __rpow__(self, other):
68-
return other**self.value
69-
70-
def __lt__(self, other):
71-
return self.value < other
72-
73-
def __le__(self, other):
74-
return self.value <= other
75-
76-
def __gt__(self, other):
77-
return self.value > other
78-
79-
def __ge__(self, other):
80-
return self.value >= other

autofit/mapper/prior/tuple_prior.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,17 @@ def instance_tuples(self):
5151
instances: [(String, instance)]
5252
A list of instances
5353
"""
54-
return list(
55-
sorted(
54+
return [
55+
(key, value.value if isinstance(value, Constant) else value)
56+
for key, value in sorted(
5657
[
5758
(key, value)
5859
for key, value in self.__dict__.items()
5960
if key != "id" and isinstance(value, (int, float))
60-
]
61-
+ [
62-
(key, value.value)
63-
for key, value in self.__dict__.items()
64-
if isinstance(value, Constant)
6561
],
6662
key=lambda tup: tup[0],
6763
)
68-
)
64+
]
6965

7066
def value_for_arguments(self, arguments):
7167
"""

test_autofit/mapper/test_constant.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,21 @@ def test_model_with_constants():
167167
assert instance.centre == 1.0
168168
assert isinstance(instance.centre, float)
169169
assert not isinstance(instance.centre, Constant)
170+
171+
172+
def test_as_float():
173+
constant = af.Constant(1.0)
174+
assert float(constant) == 1.0
175+
176+
constant_2 = af.Constant(2.0)
177+
assert float(constant_2) == 2.0
178+
179+
assert constant.id != constant_2.id
180+
181+
assert isinstance(float(constant), float)
182+
183+
184+
def test_is_instance():
185+
constant = af.Constant(1.0)
186+
187+
assert isinstance(constant, float)

0 commit comments

Comments
 (0)