Skip to content

Commit b3cf839

Browse files
rhttpike3
authored andcommitted
feat: Implement NumberInput UserParam class
1 parent 991a017 commit b3cf839

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

mesa/visualization/UserParam.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from numbers import Number
2+
13
NUMBER = "number"
24
CHECKBOX = "checkbox"
35
CHOICE = "choice"
@@ -268,3 +270,19 @@ def __init__(self, value=None):
268270
self._value = value
269271
valid = isinstance(self.value, str)
270272
self.maybe_raise_error(valid)
273+
274+
275+
class NumberInput(UserParam):
276+
"""
277+
a simple numerical input
278+
279+
Example:
280+
number_option = NumberInput("My Number", value=123)
281+
"""
282+
283+
def __init__(self, name="", value=None, description=None):
284+
self.param_type = NUMBER
285+
self.name = name
286+
self._value = value
287+
valid = isinstance(self.value, Number)
288+
self.maybe_raise_error(valid)

tests/test_usersettableparam.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
Checkbox,
66
Choice,
77
StaticText,
8+
NumberInput
89
)
910

1011

1112
class TestOption(TestCase):
1213
def setUp(self):
1314
self.number_option = UserSettableParameter("number", value=123)
15+
self.number_option_standalone = NumberInput("number", value=123)
1416
self.checkbox_option = UserSettableParameter("checkbox", value=True)
1517
self.checkbox_option_standalone = Checkbox(value=True)
1618
self.choice_option = UserSettableParameter(
@@ -29,9 +31,10 @@ def setUp(self):
2931
self.static_text_option = StaticText("Hurr, Durr Im'a Sheep")
3032

3133
def test_number(self):
32-
assert self.number_option.value == 123
33-
self.number_option.value = 321
34-
assert self.number_option.value == 321
34+
for option in [self.number_option, self.number_option_standalone]:
35+
assert option.value == 123
36+
option.value = 321
37+
assert option.value == 321
3538

3639
def test_checkbox(self):
3740
for option in [self.checkbox_option, self.checkbox_option_standalone]:

0 commit comments

Comments
 (0)