docs: def make_segment(data, mode, encoding=None):
      """\
      Creates a :py:class:`Segment`.
  
      :param data: The segment data
      :param mode: The mode.
      :param encoding: The encoding.
      :rtype: _Segment
      """
      segment_data, segment_length, segment_encoding = data_to_bytes(data, encoding)
      segment_mode = mode
      # If the user prefers BYTE, use BYTE and do not try to find a better mode
      # Necessary since BYTE < KANJI and find_mode may return KANJI as (more)
      # appropriate mode and the encoder throws an exception
      # if "user provided mode" < "found mode"
      guessed_mode = find_mode(segment_data) if segment_mode != consts.MODE_BYTE else consts.MODE_BYTE
      if segment_mode is not None:
          # Check if user provided mode is applicable for the given segment_data
          if segment_mode < guessed_mode:
              raise ModeError('The provided mode "{0}" is not applicable '
                              'for {1}. Proposal: {2}'
                              .format(get_mode_name(segment_mode),
                                      repr(segment_data),
                                      get_mode_name(guessed_mode)))
      else:
          segment_mode = guessed_mode
      if segment_mode != consts.MODE_BYTE:
          segment_encoding = None
      buff = Buffer()
      append_bits = buff.append_bits
      if segment_mode == consts.MODE_NUMERIC:
          # ISO/IEC 18004:2015(E) -- 7.4.3 Numeric mode (page 25)
          # The input data string is divided into groups of three digits, and
          # each group is converted to its 10-bit binary equivalent. If the number
          # of input digits is not an exact multiple of three, the final one or
          # two digits are converted to 4 or 7 bits respectively.
          for i in range(0, segment_length, 3):
              chunk = segment_data[i:i + 3]
              append_bits(int(chunk), len(chunk) * 3 + 1)
      elif segment_mode == consts.MODE_ALPHANUMERIC:
          # ISO/IEC 18004:2015(E) -- 7.4.4 Alphanumeric mode (page 26)
          to_byte = consts.ALPHANUMERIC_CHARS.find
          for i in range(0, segment_length, 2):
              chunk = segment_data[i:i + 2]
              # Input data characters are divided into groups of two characters
              # which are encoded as 11-bit binary codes. The character value of
              # the first character is multiplied by 45 and the character value
              # of the second digit is added to the product. The sum is then
              # converted to an 11-bit binary number.
              if len(chunk) > 1:
                  append_bits(to_byte(chunk[0]) * 45 + to_byte(chunk[1]), 11)
              else:
                  # If the number of input data characters is not a multiple of
                  # two, the character value of the final character is encoded
                  # as a 6-bit binary number.
                  append_bits(to_byte(chunk), 6)
      elif segment_mode == consts.MODE_BYTE:
          # ISO/IEC 18004:2015(E) -- 7.4.5 Byte mode (page 27)
          if _PY2:
              segment_data = (ord(b) for b in segment_data)
          for b in segment_data:
              append_bits(b, 8)
      else:
          # ISO/IEC 18004:2015(E) -- 7.4.6 Kanji mode (page 29)
          if _PY2:
              segment_data = [ord(b) for b in segment_data]
          # Note: len(segment.data)! segment.data_length = len(segment.data) / 2!!
          for i in range(0, len(segment_data), 2):
              code = (segment_data[i] << 8) | segment_data[i + 1]
              if 0x8140 <= code <= 0x9ffc:
                  # For characters with Shift JIS values from 8140HEX to 9FFCHEX:
                  # a) Subtract 8140HEX from Shift JIS value;
                  diff = code - 0x8140
              elif 0xe040 <= code <= 0xebbf:
                  # For characters with Shift JIS values from E040HEX to EBBFHEX:
                  # a) Subtract C140HEX from Shift JIS value;
                  diff = code - 0xc140
              else:  # pragma: no cover
                  raise QRCodeError('Invalid Kanji bytes: {0}'.format(code))
              # b) Multiply most significant byte of result by C0HEX;
              # c) Add least significant byte to product from b);
              # d) Convert result to a 13-bit binary string.
              append_bits(((diff >> 8) * 0xc0) + (diff & 0xff), 13)
      return _Segment(buff.getbits(), segment_length, segment_mode, segment_encoding)