Skip to content

Commit 89d636b

Browse files
committed
add back Ref.set_current
assignment expressions do not support attribute assignment
1 parent 637ffb5 commit 89d636b

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

docs/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
FROM python:3.8
22

3-
WORKDIR app/
3+
WORKDIR /app/
44

55
# Install NodeJS
66
# --------------

docs/source/examples/simple_dashboard.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,18 @@ async def RandomWalk():
2323
.number-input-container input + input {margin-left: 4%}
2424
"""
2525
),
26-
NumberInput("Mean", mu.current, mu.set, (-1, 1, 0.01)),
27-
NumberInput("Standard Deviation", sigma.current, sigma.set, (0, 1, 0.01)),
26+
NumberInput(
27+
"Mean",
28+
mu.current,
29+
mu.set_current,
30+
(-1, 1, 0.01),
31+
),
32+
NumberInput(
33+
"Standard Deviation",
34+
sigma.current,
35+
sigma.set_current,
36+
(0, 1, 0.01),
37+
),
2838
)
2939

3040

idom/utils.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
from typing import List, Tuple, Any, Dict, Callable, Optional, Generic, TypeVar
44

55

6-
_Current = TypeVar("_Current")
6+
_RefValue = TypeVar("_RefValue")
77

88

9-
class Ref(Generic[_Current]):
9+
class Ref(Generic[_RefValue]):
1010
"""Hold a reference to a value
1111
1212
This is used in imperative code to mutate the state of this object in order to
@@ -22,9 +22,18 @@ class Ref(Generic[_Current]):
2222

2323
__slots__ = "current"
2424

25-
def __init__(self, initial_value: _Current) -> None:
25+
def __init__(self, initial_value: _RefValue) -> None:
2626
self.current = initial_value
2727

28+
def set_current(self, new: _RefValue) -> _RefValue:
29+
"""Set the current value and return what is now the old value
30+
31+
This is nice to use in ``lambda`` functions.
32+
"""
33+
old = self.current
34+
self.current = new
35+
return old
36+
2837
def __eq__(self, other: Any) -> bool:
2938
return isinstance(other, Ref) and (other.current == self.current)
3039

tests/test_utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@
66

77
def test_basic_ref_behavior():
88
r = idom.Ref(1)
9+
assert r.current == 1
10+
911
r.current = 2
1012
assert r.current == 2
1113

14+
assert r.set_current(3) == 2
15+
assert r.current == 3
16+
1217

1318
def test_ref_equivalence():
1419
assert idom.Ref([1, 2, 3]) == idom.Ref([1, 2, 3])

0 commit comments

Comments
 (0)