Encode data - list, dict, string, bool or int (and nested)

    :param data: data to encode
    :return: encoded object of the same type
190547
def encode_data(data):
    '''
    
    '''
    if isinstance(data, (six.string_types, bytes)):
        return encode_string(data)
    elif isinstance(data, (six.integer_types, bool, float)):
        return data
    elif data is None:
        return data
    elif isinstance(data, list):
        return [encode_data(x) for x in data]
    elif isinstance(data, dict):
        return {k: encode_data(v) for k, v in data.items()}
    else:
        raise ValueError('Cannot encode data of type %s' % type(data))
537226
def to_python(self, data):
        
        if data is None:
            return data
        if self.datatype:
            return self.datatype(data)
        else:
            return data
362662
def _asciify_dict(data):
    
    ret = {}
    for key, value in data.iteritems():
        if isinstance(key, unicode):
            key = _remove_accents(key)
            key = key.encode('utf-8')
            # # note new if
        if isinstance(value, unicode):
            value = _remove_accents(value)
            value = value.encode('utf-8')
        elif isinstance(value, list):
            value = _asciify_list(value)
        elif isinstance(value, dict):
            value = _asciify_dict(value)
        ret[key] = value
    return ret
28805
def data_to_bytes(self, data_element, encoding=None):
        
        if encoding is None:
            encoding = self.encoding
        text = self.data_to_string(data_element)
        return bytes_(text, encoding=encoding)
114661
def serialize_unicode(self, data):
        
        try:
            return data.value
        except AttributeError:
            pass
        try:
            if isinstance(data, unicode):
                return data.encode(encoding='utf-8')
        except NameError:
            return str(data)
        else:
            return str(data)
993979
def encode_data(self):
        
        assert self.get_empty() is False
        self._data_encoded = True
        if self._block_allowed:
            data = self._encode_transfer_block_data()
        else:
            data = self._encode_transfer_data()
        return data
1227845
def _asciify_list(data):
    
    ret = []
    for item in data:
        if isinstance(item, unicode):
            item = _remove_accents(item)
            item = item.encode('utf-8')
        elif isinstance(item, list):
            item = _asciify_list(item)
        elif isinstance(item, dict):
            item = _asciify_dict(item)
        ret.append(item)
    return ret
110032
def to_python(self, data):
        
        if data is None:
            return ''
        if isinstance(data, unicode):
            data = data.encode(DEFAULT_ENCODING)
        else:
            data = str(data)
        return data
356556
def encode_dict(data, encoding=None, errors='strict', keep=False,
                preserve_dict_class=False, preserve_tuples=False):
    '''
    
    '''
    rv = data.__class__() if preserve_dict_class else {}
    for key, value in six.iteritems(data):
        if isinstance(key, tuple):
            key = encode_tuple(key, encoding, errors, keep, preserve_dict_class) \
                if preserve_tuples \
                else encode_list(key, encoding, errors, keep,
                                 preserve_dict_class, preserve_tuples)
        else:
            try:
                key = salt.utils.stringutils.to_bytes(key, encoding, errors)
            except TypeError:
                # to_bytes raises a TypeError when input is not a
                # string/bytestring/bytearray. This is expected and simply
                # means we are going to leave the value as-is.
                pass
            except UnicodeEncodeError:
                if not keep:
                    raise

        if isinstance(value, list):
            value = encode_list(value, encoding, errors, keep,
                                preserve_dict_class, preserve_tuples)
        elif isinstance(value, tuple):
            value = encode_tuple(value, encoding, errors, keep, preserve_dict_class) \
                if preserve_tuples \
                else encode_list(value, encoding, errors, keep,
                                 preserve_dict_class, preserve_tuples)
        elif isinstance(value, Mapping):
            value = encode_dict(value, encoding, errors, keep,
                                preserve_dict_class, preserve_tuples)
        else:
            try:
                value = salt.utils.stringutils.to_bytes(value, encoding, errors)
            except TypeError:
                # to_bytes raises a TypeError when input is not a
                # string/bytestring/bytearray. This is expected and simply
                # means we are going to leave the value as-is.
                pass
            except UnicodeEncodeError:
                if not keep:
                    raise

        rv[key] = value
    return rv
1025333
def decode_tuple(data, encoding=None, errors='strict', keep=False,
                 normalize=False, preserve_dict_class=False, to_str=False):
    '''
    
    '''
    return tuple(
        decode_list(data, encoding, errors, keep, normalize,
                    preserve_dict_class, True, to_str)
    )
368075
def cdata (self, data):
        
        data = data.encode(self.encoding, "ignore")
        self.fd.write("<![CDATA[%s]]>" % data)
1131456
function encode(data) {
  $.checkArgument(data instanceof Array, 'Must be Array');
  var base32 = '';
  for (var i=0; i<data.length; i++) {
    var value = data[i];
    $.checkArgument(0 <= value && value < 32, 'Non-base32, value ' + value);
    base32 += CHARSET[value];
  }
  return base32;
}
187984
def encode(data, mime_type='', charset='utf-8', base64=True):
    
    if isinstance(data, six.text_type):
        data = data.encode(charset)
    else:
        charset = None
    if base64:
        data = utils.text(b64encode(data))
    else:
        data = utils.text(quote(data))

    result = ['data:', ]
    if mime_type:
        result.append(mime_type)
    if charset:
        result.append(';charset=')
        result.append(charset)
    if base64:
        result.append(';base64')
    result.append(',')
    result.append(data)

    return ''.join(result)
1091354
def _normalize_keys(self, data):
        
        encode = self.encoder.encode
        decode = self.encoder.decode
        return {decode(encode(k)): v for k, v in iteritems(data)}
1246226
def _b64encode_to_str(data):
    
    ret = b64encode(data)
    if not isinstance(ret, str):  # Python3
        return ret.decode('ascii')
    else:
        return ret
277951
def encode(B):
    
    B = array(B)

    flatten = False
    if len(B.shape) == 1:
        flatten = True
        B = B.reshape(1, -1)

    if B.shape[1] != data_size:
        raise ValueError('Data must be shaped as (4, ) or (-1, 4)')

    C = dot(G, B.T).T % 2

    if flatten:
        C = C.flatten()

    return C
266428
def _pure_data(self, data: Any) -> Any:
        
        if not isinstance(data, dict) and not isinstance(data, list):
            try:
                return dict(data)
            except TypeError:
                ...
        return data