Skip to content

Commit

Permalink
Simplify/improve DMX Element.append() code
Browse files Browse the repository at this point in the history
  • Loading branch information
TeamSpen210 committed Oct 30, 2024
1 parent 464119e commit 49e647c
Showing 1 changed file with 7 additions and 21 deletions.
28 changes: 7 additions & 21 deletions src/srctools/dmx.py
Original file line number Diff line number Diff line change
Expand Up @@ -1153,38 +1153,24 @@ def append(self, value: ConvValue) -> None:
If not already an array, it is converted to one
holding the existing value.
"""
if not isinstance(self._value, list):
self._value = [self._value]
[val_type, result] = deduce_type_single(value)
if val_type is not self._typ:
# Try converting.
try:
func = cast('Callable[[Value], ValueT]', TYPE_CONVERT[val_type, self._typ])
except KeyError:
raise ValueError(f'Cannot convert {val_type} to {self._typ} type!') from None
try:
func = cast('Callable[[Value], ValueT]', TYPE_CONVERT[val_type, self._typ])
except KeyError:
raise ValueError(f'Cannot convert {val_type} to {self._typ} type!') from None
if isinstance(self._value, list):
self._value.append(func(result))
else:
self._value.append(result) # type: ignore # (we know it's right)
self._value = [self._value, func(result)]

def extend(self, values: Iterable[ConvValue]) -> None:
"""Append multiple values to the array.
If not already an array, it is converted to one
holding the existing value.
"""
if not isinstance(self._value, list):
self._value = [self._value]
for value in values:
[val_type, result] = deduce_type_single(value)
if val_type is not self._typ:
# Try converting.
try:
func = cast('Callable[[Value], ValueT]', TYPE_CONVERT[val_type, self._typ])
except KeyError:
raise ValueError(f'Cannot convert {val_type} to {self._typ} type!') from None
self._value.append(func(result))
else:
self._value.append(result) # type: ignore # (we know it's right)
self.append(value)

def clear_array(self) -> None:
"""Remove all items in this, if it is an array."""
Expand Down

0 comments on commit 49e647c

Please sign in to comment.