Пример #1
0
 def __setitem__(self, key, item):
     if isinstance(key, slice):
         if isinstance(item, bitarray):
             bitarray.__setitem__(self, self._swap_slice_indices(key), item)
         elif isinstance(item, (int, long)):
             slc = self._swap_slice_indices(key)
             bl = BitLogic.from_value(value=item, size=slc.stop - slc.start)
             bitarray.__setitem__(self, slc, bl)
         else:
             raise TypeError("Invalid argument type")
     elif isinstance(key, (int, long)):
         return bitarray.__setitem__(self, key, item)
     else:
         raise TypeError("Invalid argument type")
Пример #2
0
    def __setitem__(self, key, item):
        '''Indexing and slicing

        Note: the length must not be changed
        '''
        length = self.length()
        try:
            # item is bit string
            _ = int(item, base=2)
        except TypeError:
            if type(item) in (int, long):
                # item is number, bool
                slc = self._swap_slice_indices(key, make_slice=True)
                size = slc.stop - slc.start
                bl = BitLogic.from_value(value=item, size=size)
                bitarray.__setitem__(self, slc, bl)
            else:
                # item is bitarray, list, tuple, bool
                # make slice if item is no bool, otherwise assignment will be casted to bool, and is most likely True
                slc = self._swap_slice_indices(key, make_slice=True if (type(item) not in (bool, )) else False)
                bitarray.__setitem__(self, slc, item)
        else:
            slc = self._swap_slice_indices(key, make_slice=True)
            bl = BitLogic(item)
            bitarray.__setitem__(self, slc, bl)
        if self.length() != length:
            raise ValueError('Unexpected length for slice assignment')
Пример #3
0
    def __setitem__(self, key, item):
        '''Indexing and slicing

        Note: the length must not be changed
        '''
        length = len(self)
        try:
            # item is bit string
            _ = int(item, base=2)
        except TypeError:
            if isinstance(item, integer_types):
                # item is number, bool
                slc = self._swap_slice_indices(key, make_slice=True)
                size = slc.stop - slc.start
                bl = BitLogic.from_value(value=item, size=size)
                bitarray.__setitem__(self, slc, bl)
            else:
                # item is bitarray, list, tuple, bool
                # make slice if item is no bool, otherwise assignment will be casted to bool, and is most likely True
                slc = self._swap_slice_indices(key, make_slice=True if (type(item) not in (bool, )) else False)
                bitarray.__setitem__(self, slc, item)
        else:
            slc = self._swap_slice_indices(key, make_slice=True)
            bl = BitLogic(item)
            bitarray.__setitem__(self, slc, bl)
        if len(self) != length:
            raise ValueError('Unexpected length for slice assignment')
Пример #4
0
 def set_slice_ba(self, start, stop, item):
     bitarray.__setitem__(self, slice(stop, start + 1), item)
Пример #5
0
 def set_slice_ba(self, start, stop, item):
     bitarray.__setitem__(self, slice(stop, start + 1), item)