Skip to content

Commit 462c203

Browse files
committed
Add color merging methods to Tally
1 parent 91bd5fa commit 462c203

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

src/tslumd/tallyobj.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,62 @@ def from_display(cls, display: 'tslumd.Display', **kwargs) -> 'Tally':
117117
kw.update({attr:getattr(display, attr) for attr in cls._prop_attrs})
118118
return cls(display.index, **kw)
119119

120+
def set_color(self, tally_type: TallyType, color: TallyColor):
121+
"""Set the color property (or properties) for the given TallyType
122+
123+
Sets the of :attr:`rh_tally`, :attr:`txt_tally` or :attr:`lh_tally`
124+
properties matching the :class:`~.common.TallyType` value
125+
126+
Arguments:
127+
tally_type (TallyType): The :class:`~.common.TallyType` member(s)
128+
to set. Multiple types can be specified using
129+
bitwise ``|`` operators.
130+
color (TallyColor): The :class:`~.common.TallyColor` to set
131+
132+
.. versionadded:: 0.0.4
133+
"""
134+
for ttype in tally_type:
135+
setattr(self, ttype.name, color)
136+
137+
def merge_color(self, tally_type: TallyType, color: TallyColor):
138+
"""Merge the color property (or properties) for the given TallyType
139+
using the :meth:`set_color` method
140+
141+
Combines the existing color value with the one provided using a bitwise
142+
``|`` (or) operation
143+
144+
Arguments:
145+
tally_type (TallyType): The :class:`~.common.TallyType` member(s)
146+
to merge. Multiple types can be specified using
147+
bitwise ``|`` operators.
148+
color (TallyColor): The :class:`~.common.TallyColor` to merge
149+
150+
.. versionadded:: 0.0.4
151+
"""
152+
for ttype in tally_type:
153+
cur_color = getattr(self, ttype.name)
154+
new_color = cur_color | color
155+
if new_color == cur_color:
156+
continue
157+
self.set_color(ttype, new_color)
158+
159+
def merge(self, other: 'Tally', tally_type: Optional[TallyType] = TallyType.all_tally):
160+
"""Merge the color(s) from another Tally instance into this one using
161+
the :meth:`merge_color` method
162+
163+
Arguments:
164+
other (Tally): The Tally instance to merge with
165+
tally_type (TallyType, optional): The :class:`~.common.TallyType`
166+
member(s) to merge. Multiple types can be specified using
167+
bitwise ``|`` operators.
168+
Default is :attr:`~.common.TallyType.all_tally` (all three types)
169+
170+
.. versionadded:: 0.0.4
171+
"""
172+
for ttype in tally_type:
173+
color = getattr(other, ttype.name)
174+
self.merge_color(ttype, color)
175+
120176
def update(self, **kwargs) -> Set[str]:
121177
"""Update any known properties from the given keyword-arguments
122178

tests/test_tallyobj.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,66 @@ def test_tally_display_conversion(faker):
3838
assert disp == Display.from_tally(tally) == tally
3939
assert Tally.from_display(disp).normalized_brightness == tally.normalized_brightness
4040

41+
def test_color_merge():
42+
t1 = Tally(0)
43+
44+
t1.set_color(TallyType.rh_tally | TallyType.lh_tally, TallyColor.RED)
45+
assert t1.rh_tally == TallyColor.RED
46+
assert t1.txt_tally == TallyColor.OFF
47+
assert t1.lh_tally == TallyColor.RED
48+
49+
t1.set_color(TallyType.rh_tally | TallyType.lh_tally, TallyColor.GREEN)
50+
assert t1.rh_tally == TallyColor.GREEN
51+
assert t1.txt_tally == TallyColor.OFF
52+
assert t1.lh_tally == TallyColor.GREEN
53+
54+
t1.merge_color(TallyType.all_tally, TallyColor.RED)
55+
assert t1.rh_tally == TallyColor.AMBER
56+
assert t1.txt_tally == TallyColor.RED
57+
assert t1.lh_tally == TallyColor.AMBER
58+
59+
t1.merge_color(TallyType.all_tally, TallyColor.GREEN)
60+
assert t1.rh_tally == TallyColor.AMBER
61+
assert t1.txt_tally == TallyColor.AMBER
62+
assert t1.lh_tally == TallyColor.AMBER
63+
64+
# Reset t1 to OFF, OFF, RED
65+
t1.rh_tally = TallyColor.OFF
66+
t1.txt_tally = TallyColor.OFF
67+
t1.lh_tally = TallyColor.RED
68+
69+
# Another Tally with only `txt_tally` set
70+
t2 = Tally(1, txt_tally=TallyColor.GREEN)
71+
72+
# Only `txt_tally` should change
73+
t1.merge(t2, TallyType.all_tally)
74+
assert t1.rh_tally == TallyColor.OFF
75+
assert t1.txt_tally == TallyColor.GREEN
76+
assert t1.lh_tally == TallyColor.RED
77+
78+
# Reset t2 to GREEN, RED, GREEN
79+
# t1 is still OFF, GREEN, RED
80+
t2.rh_tally = TallyColor.GREEN
81+
t2.txt_tally = TallyColor.RED
82+
t2.lh_tally = TallyColor.GREEN
83+
84+
t1.merge(t2, TallyType.rh_tally | TallyType.lh_tally)
85+
assert t1.rh_tally == TallyColor.GREEN
86+
assert t1.txt_tally == TallyColor.GREEN
87+
assert t1.lh_tally == TallyColor.AMBER
88+
89+
t1.merge(t2, TallyType.all_tally)
90+
assert t1.rh_tally == TallyColor.GREEN
91+
assert t1.txt_tally == TallyColor.AMBER
92+
assert t1.lh_tally == TallyColor.AMBER
93+
94+
t2.rh_tally = TallyColor.RED
95+
t1.merge(t2, TallyType.all_tally)
96+
assert t1.rh_tally == TallyColor.AMBER
97+
assert t1.txt_tally == TallyColor.AMBER
98+
assert t1.lh_tally == TallyColor.AMBER
99+
100+
41101
def test_broadcast(faker):
42102
for _ in range(1000):
43103
i = faker.pyint(max_value=0xfffe)

0 commit comments

Comments
 (0)