Skip to content

Commit

Permalink
Correct for enum module changes in Python>=3.11
Browse files Browse the repository at this point in the history
  • Loading branch information
nocarryr committed Dec 22, 2023
1 parent e735902 commit 430e11e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
24 changes: 22 additions & 2 deletions src/tslumd/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ def to_str(self) -> str:
def __str__(self):
return self.name

@classmethod
def all(cls):
"""Iterate over all members
.. versionadded:: 0.0.6
"""
yield from cls.__members__.values()

def __format__(self, format_spec):
if format_spec == '':
return str(self)
Expand Down Expand Up @@ -110,7 +118,9 @@ def is_iterable(self) -> bool:
"""
if self == TallyType.all_tally:
return True
return self.name is None
mask = 1 << (self.bit_length() - 1)
return self ^ mask != 0


@classmethod
def all(cls) -> Iterator[TallyType]:
Expand All @@ -133,7 +143,7 @@ def from_str(s: str) -> TallyType:
>>> TallyType.from_str('rh_tally')
<TallyType.rh_tally: 1>
>>> TallyType.from_str('rh|txt_tally')
<TallyType.txt_tally|rh_tally: 3>
<TallyType.rh_tally|txt_tally: 3>
>>> TallyType.from_str('rh|txt|lh')
<TallyType.all_tally: 7>
>>> TallyType.from_str('all')
Expand Down Expand Up @@ -175,6 +185,16 @@ def __iter__(self) -> Iterator[TallyType]:
if ttype in self:
yield ttype

def __repr__(self):
if not self.is_iterable:
return super().__repr__()
if self.name is not None:
members = self.name
else:
members = '|'.join([m.name for m in self])
return f'<{self.__class__.__name__}.{members}: {self.value}>'


class TallyState(enum.IntFlag):
OFF = 0 #: Off
PREVIEW = 1 #: Preview
Expand Down
2 changes: 1 addition & 1 deletion tests/test_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ async def wait_for_receiver():
receiver.bind_async(loop, on_tally_updated=evt_listener.callback)

# Send a broadcast tally for each color setting all TallyType's to it
for color in TallyColor:
for color in TallyColor.all():
color_kw = {k:color for k in color_kw.keys()}
await sender.send_broadcast_tally(screen_index, **color_kw)
await wait_for_receiver()
Expand Down

0 comments on commit 430e11e

Please sign in to comment.