コード例 #1
0
    def __init__(self, size: int) -> None:
        '''
        Initialize this fixed list to the passed length and all items of this
        fixed list to ``None``.

        Parameters
        ----------
        size : IntType
            Length to constrain this fixed list to.

        Raises
        ----------
        _BeartypeUtilCachedFixedListException
            If this length is either not an integer *or* is but is
            **non-positive** (i.e., is less than or equal to 0).
        '''

        # If this length is *NOT* an integer, raise an exception.
        if not isinstance(size, int):
            raise _BeartypeUtilCachedFixedListException(
                'Fixed list length {!r} not integer.'.format(size))
        # Else, this length is an integer.

        # If this length is non-positive, raise an exception.
        if size <= 0:
            raise _BeartypeUtilCachedFixedListException(
                'Fixed list length {!r} <= 0.'.format(size))
        # Else, this length is positive.

        # Make it so with the standard Python idiom for preallocating list
        # space -- which, conveniently, is also the optimally efficient means
        # of doing so. See also the timings in this StackOverflow answer:
        #     https://stackoverflow.com/a/10617221/2809027
        super().__init__([None] * size)
コード例 #2
0
    def _die_if_slice_len_ne_value_len(self, index, value) -> None:
        '''
        Raise an exception only if the passed parameters when passed to the
        parent :meth:`__setitem__` dunder method signify an external attempt to
        change the length of this fixed length with slicing.

        This function is intended to be called by the :meth:`__setitem__`
        dunder method to validate the passed parameters.

        Parameters
        ----------
        index
            0-based index, slice object, or tuple of 0-based indices and slice
            objects to index this fixed list with.
        value
            Object to set this index(s) of this fixed list to.

        Raises
        ----------
        _BeartypeUtilCachedFixedListException
            If this index is a **slice object** (i.e., :class:`slice` instance
            underlying slice syntax) and this value is either:

            * **Unsized** (i.e., unsupported by the :func:`len` builtin).
            * Sized but has a length differing from that of this fixed list.
        '''

        # If this index is *NOT* a slice, silently reduce to a noop.
        if not isinstance(index, slice):
            return
        # Else, this index is a slice.

        # If this value is *NOT* a sized container, raise an exception.
        if not isinstance(value, Sized):
            raise _BeartypeUtilCachedFixedListException(
                '{} slice {!r} not settable to unsized {}.'.format(
                    self._label, index, get_object_representation(value)))
        # Else, this value is a sized container.

        # 0-based first and one-past-the-last indices sliced by this slice.
        start, stop_plus_one, _ = index.indices(len(self))

        # Number of items of this fixed list sliced by this slice. By
        # definition, this is guaranteed to be a non-negative integer.
        slice_len = stop_plus_one - start

        # Number of items of this sized container to set this slice to.
        value_len = len(value)

        # If these two lengths differ, raise an exception.
        if slice_len != value_len:
            raise _BeartypeUtilCachedFixedListException(
                '{} slice {!r} of length {} not settable to '
                '{} of differing length {}.'.format(
                    self._label,
                    index,
                    slice_len,
                    get_object_representation(value),
                    value_len,
                ))
コード例 #3
0
 def remove(self, *args):
     raise _BeartypeUtilCachedFixedListException('{} not removable.'.format(
         self._label))
コード例 #4
0
 def pop(self, *args):
     raise _BeartypeUtilCachedFixedListException('{} not poppable.'.format(
         self._label))
コード例 #5
0
 def extend(self, obj):
     raise _BeartypeUtilCachedFixedListException(
         '{} not extendable by {}.'.format(self._label,
                                           get_object_representation(obj)))
コード例 #6
0
 def clear(self):
     raise _BeartypeUtilCachedFixedListException('{} not clearable.'.format(
         self._label))
コード例 #7
0
 def __imul__(self, value):
     raise _BeartypeUtilCachedFixedListException(
         '{} not multipliable by {}.'.format(
             self._label, get_object_representation(value)))
コード例 #8
0
 def remove(self, *args) -> NoReturn:
     raise _BeartypeUtilCachedFixedListException(
         f'{self._label} not removable.')
コード例 #9
0
 def __delitem__(self, index):
     raise _BeartypeUtilCachedFixedListException(
         '{} index {!r} not deletable.'.format(self._label, index))
コード例 #10
0
 def pop(self, *args) -> NoReturn:
     raise _BeartypeUtilCachedFixedListException(
         f'{self._label} not poppable.')
コード例 #11
0
 def extend(self, obj) -> NoReturn:
     raise _BeartypeUtilCachedFixedListException(
         f'{self._label} not extendable by '
         f'{get_object_representation(obj)}.')
コード例 #12
0
 def clear(self) -> NoReturn:
     raise _BeartypeUtilCachedFixedListException(
         f'{self._label} not clearable.')
コード例 #13
0
 def __imul__(self, value) -> NoReturn:  # type: ignore[misc]
     raise _BeartypeUtilCachedFixedListException(
         f'{self._label} not multipliable by '
         f'{get_object_representation(value)}.')
コード例 #14
0
 def __delitem__(self, index) -> NoReturn:
     raise _BeartypeUtilCachedFixedListException(
         f'{self._label} index {repr(index)} not deletable.')