docs: def byteswap(self, fmt=None, start=None, end=None, repeat=True):
          """Change the endianness in-place. Return number of repeats of fmt done.
  
          fmt -- A compact structure string, an integer number of bytes or
                 an iterable of integers. Defaults to 0, which byte reverses the
                 whole bitstring.
          start -- Start bit position, defaults to 0.
          end -- End bit position, defaults to self.len.
          repeat -- If True (the default) the byte swapping pattern is repeated
                    as much as possible.
  
          """
          start, end = self._validate_slice(start, end)
          if fmt is None or fmt == 0:
              # reverse all of the whole bytes.
              bytesizes = [(end - start) // 8]
          elif isinstance(fmt, numbers.Integral):
              if fmt < 0:
                  raise ValueError("Improper byte length {0}.".format(fmt))
              bytesizes = [fmt]
          elif isinstance(fmt, basestring):
              m = STRUCT_PACK_RE.match(fmt)
              if not m:
                  raise ValueError("Cannot parse format string {0}.".format(fmt))
              # Split the format string into a list of 'q', '4h' etc.
              formatlist = re.findall(STRUCT_SPLIT_RE, m.group('fmt'))
              # Now deal with multiplicative factors, 4h -> hhhh etc.
              bytesizes = []
              for f in formatlist:
                  if len(f) == 1:
                      bytesizes.append(PACK_CODE_SIZE[f])
                  else:
                      bytesizes.extend([PACK_CODE_SIZE[f[-1]]] * int(f[:-1]))
          elif isinstance(fmt, collections.Iterable):
              bytesizes = fmt
              for bytesize in bytesizes:
                  if not isinstance(bytesize, numbers.Integral) or bytesize < 0:
                      raise ValueError("Improper byte length {0}.".format(bytesize))
          else:
              raise TypeError("Format must be an integer, string or iterable.")
  
          repeats = 0
          totalbitsize = 8 * sum(bytesizes)
          if not totalbitsize:
              return 0
          if repeat:
              # Try to repeat up to the end of the bitstring.
              finalbit = end
          else:
              # Just try one (set of) byteswap(s).
              finalbit = start + totalbitsize
          for patternend in xrange(start + totalbitsize, finalbit + 1, totalbitsize):
              bytestart = patternend - totalbitsize
              for bytesize in bytesizes:
                  byteend = bytestart + bytesize * 8
                  self._reversebytes(bytestart, byteend)
                  bytestart += bytesize * 8
              repeats += 1
          return repeats