Exemple #1
0
 def __init__(
     self,
     value: typing.Union[int, typing.Iterable[LogicConstructibleT],
                         BinaryValue],
     range: typing.Optional[Range] = None,
 ) -> None:
     if isinstance(value, int):
         if value < 0:
             bitlen = int.bit_length(value + 1) + 1
         else:
             bitlen = max(1, int.bit_length(value))
         if range is None:
             self._value = [Logic(v) for v in _int_to_bitstr(value, bitlen)]
         else:
             if bitlen > len(range):
                 raise ValueError(f"{value} will not fit in {range}")
             self._value = [
                 Logic(v) for v in _int_to_bitstr(value, len(range))
             ]
     elif isinstance(value, typing.Iterable):
         self._value = [Logic(v) for v in value]
     elif isinstance(value, BinaryValue):
         self._value = [Logic(v) for v in value.binstr]
     else:
         raise TypeError(
             f"cannot construct {type(self).__qualname__} from value of type {type(value).__qualname__}"
         )
     if range is None:
         self._range = Range(len(self._value) - 1, "downto", 0)
     else:
         self._range = range
     if len(self._value) != len(self._range):
         raise ValueError(
             f"value of length {len(self._value)} will not fit in {self._range}"
         )
Exemple #2
0
 def __setitem__(
     self,
     item: typing.Union[int, slice],
     value: typing.Union[LogicConstructibleT,
                         typing.Iterable[LogicConstructibleT]],
 ) -> None:
     if isinstance(item, int):
         super().__setitem__(item,
                             Logic(typing.cast(LogicConstructibleT, value)))
     elif isinstance(item, slice):
         super().__setitem__(
             item,
             (Logic(v) for v in typing.cast(
                 typing.Iterable[LogicConstructibleT], value)),
         )
     else:
         raise TypeError(
             f"indexes must be ints or slices, not {type(item).__name__}")
Exemple #3
0
 def is_resolvable(self) -> bool:
     return all(bit in (Logic(0), Logic(1)) for bit in self)