Encode a dict for the DynamoDB format
509804
def encode_dict(dynamizer, value):
    
    encoded_dict = {}
    for k, v in six.iteritems(value):
        encoded_type, encoded_value = dynamizer.raw_encode(v)
        encoded_dict[k] = {
            encoded_type: encoded_value,
        }
    return 'M', encoded_dict
627859
def convert_dict(obj, ids, parent, attr_type, item_func, cdata):
    """"""
    LOG.info('Inside convert_dict(): obj type is: "%s", obj="%s"' % (
        type(obj).__name__, unicode_me(obj))
    )
    output = []
    addline = output.append
    
    item_name = item_func(parent)
    
    for key, val in obj.items():
        LOG.info('Looping inside convert_dict(): key="%s", val="%s", type(val)="%s"' % (
            unicode_me(key), unicode_me(val), type(val).__name__)
        )

        attr = {} if not ids else {'id': '%s' % (get_unique_id(parent)) }

        key, attr = make_valid_xml_name(key, attr)

        if isinstance(val, numbers.Number) or type(val) in (str, unicode):
            addline(convert_kv(key, val, attr_type, attr, cdata))

        elif hasattr(val, 'isoformat'): # datetime
            addline(convert_kv(key, val.isoformat(), attr_type, attr, cdata))

        elif type(val) == bool:
            addline(convert_bool(key, val, attr_type, attr, cdata))

        elif isinstance(val, dict):
            if attr_type:
                attr['type'] = get_xml_type(val)
            addline('<%s%s>%s</%s>' % (
                key, make_attrstring(attr), 
                convert_dict(val, ids, key, attr_type, item_func, cdata), 
                key
                )
            )

        elif isinstance(val, collections.Iterable):
            if attr_type:
                attr['type'] = get_xml_type(val)
            addline('<%s%s>%s</%s>' % (
                key, 
                make_attrstring(attr), 
                convert_list(val, ids, key, attr_type, item_func, cdata), 
                key
                )
            )

        elif val is None:
            addline(convert_none(key, val, attr_type, attr, cdata))

        else:
            raise TypeError('Unsupported data type: %s (%s)' % (
                val, type(val).__name__)
            )

    return ''.join(output)
1262084
def to_serializable_dict(self, attrs_to_serialize=None,
                             rels_to_expand=None,
                             rels_to_serialize=None,
                             key_modifications=None):
        
        return self.todict(
            attrs_to_serialize=attrs_to_serialize,
            rels_to_expand=rels_to_expand, rels_to_serialize=rels_to_serialize,
            key_modifications=key_modifications)
210112
def _encode_write(dynamizer, data, action, key):
    
    # Strip null values out of data
    data = dict(((k, dynamizer.encode(v)) for k, v in six.iteritems(data) if
                 not is_null(v)))
    return {
        action: {
            key: data,
        }
    }
354084
def _encode_key(self, obj):
        """"""

        if obj.__class__ is str:
            return self._encode_str(obj)

        if obj.__class__ is UUID:
            return '"' + str(obj) + '"'

        # __mm_serialize__ is called before any isinstance checks (but after exact type checks)
        try:
            sx_encoder = obj.__mm_serialize__
        except AttributeError:
            pass
        else:
            try:
                data = sx_encoder()
            except NotImplementedError:
                pass
            else:
                return self._encode_key(data)

        if isinstance(obj, UUID):
            return '"' + str(obj) + '"'

        if isinstance(obj, str):
            return self._encode_str(obj)

        # if everything else failed try the default() method and re-raise any TypeError
        # exceptions as more specific "not a valid dict key" TypeErrors
        try:
            value = self.default(obj)
        except TypeError:
            raise TypeError('{!r} is not a valid dictionary key'.format(obj))

        return self._encode_key(value)
1095637
def to_api(in_dict, int_keys=None, date_keys=None, bool_keys=None):
    """"""

    # Cast all int_keys to int()
    if int_keys:
        for in_key in int_keys:
            if (in_key in in_dict) and (in_dict.get(in_key, None) is not None):
                in_dict[in_key] = int(in_dict[in_key])

    # Cast all date_keys to datetime.isoformat
    if date_keys:
        for in_key in date_keys:
            if (in_key in in_dict) and (in_dict.get(in_key, None) is not None):

                _from = in_dict[in_key]

                if isinstance(_from, basestring):
                    dtime = parse_datetime(_from)

                elif isinstance(_from, datetime):
                    dtime = _from

                in_dict[in_key] = dtime.isoformat()

            elif (in_key in in_dict) and in_dict.get(in_key, None) is None:
                del in_dict[in_key]

    # Remove all Nones
    for k, v in in_dict.items():
        if v is None:
            del in_dict[k]

    return in_dict
285843
def pickle_encode(session_dict):
    ""
    pickled = pickle.dumps(session_dict, pickle.HIGHEST_PROTOCOL)
    return base64.encodestring(pickled + get_query_hash(pickled).encode())
127377
def serialize_attribute(attribute):  # noqa: C901 pylint: disable=too-many-locals
    # type: (dynamodb_types.RAW_ATTRIBUTE) -> bytes
    

    def _transform_binary_value(value):
        # type: (dynamodb_types.BINARY) -> bytes
        """
        :param value: Input value
        :type value: boto3.dynamodb.types.Binary
        :returns: bytes value
        :rtype: bytes
        """
        if isinstance(value, Binary):
            return bytes(value.value)
        return bytes(value)

    def _serialize_binary(_attribute):
        # type: (dynamodb_types.BINARY) -> bytes
        """
        :param _attribute: Attribute to serialize
        :type _attribute: boto3.dynamodb.types.Binary
        :returns: Serialized _attribute
        :rtype: bytes
        """
        return _RESERVED + Tag.BINARY.tag + encode_value(_transform_binary_value(_attribute))

    def _transform_number_value(value):
        # type: (str) -> bytes
        """
        :param value: Input value
        :type value: numbers.Number
        :returns: bytes value
        :rtype: bytes
        """
        # At this point we are receiving values which have already been transformed
        # by dynamodb.TypeSerializer, so all numbers are str. However, TypeSerializer
        # leaves trailing zeros if they are defined in the Decimal call, but we need to
        # strip all trailing zeros.
        decimal_value = DYNAMODB_CONTEXT.create_decimal(value).normalize()
        return "{0:f}".format(decimal_value).encode("utf-8")

    def _serialize_number(_attribute):
        # type: (str) -> bytes
        """
        :param _attribute: Attribute to serialize
        :type _attribute: numbers.Number
        :returns: Serialized _attribute
        :rtype: bytes
        """
        return _RESERVED + Tag.NUMBER.tag + encode_value(_transform_number_value(_attribute))

    def _transform_string_value(value):
        # type: (dynamodb_types.STRING) -> bytes
        """
        :param value: Input value
        :type value: bytes or str
        :returns: bytes value
        :rtype: bytes
        """
        return to_bytes(value)

    def _serialize_string(_attribute):
        # type: (dynamodb_types.STRING) -> bytes
        """
        :param _attribute: Attribute to serialize
        :type _attribute: six.string_types
        :returns: Serialized _attribute
        :rtype: bytes
        """
        return _RESERVED + Tag.STRING.tag + encode_value(_transform_string_value(_attribute))

    def _serialize_boolean(_attribute):
        # type: (dynamodb_types.BOOLEAN) -> bytes
        """
        :param bool _attribute: Attribute to serialize
        :returns: Serialized _attribute
        :rtype: bytes
        """
        _attribute_value = TagValues.TRUE.value if _attribute else TagValues.FALSE.value
        return _RESERVED + Tag.BOOLEAN.tag + _attribute_value

    def _serialize_null(_attribute):
        # type: (dynamodb_types.NULL) -> bytes
        """
        :param _attribute: Attribute to serialize
        :type _attribute: types.NoneType
        :returns: Serialized _attribute
        :rtype: bytes
        """
        return _RESERVED + Tag.NULL.tag

    def _serialize_set(tag, _attribute, member_function):
        # type: (Tag, dynamodb_types.SET[dynamodb_types.ATTRIBUTE], Callable) -> bytes
        """
        :param bytes tag: Tag to identify this set
        :param set _attribute: Attribute to serialize
        :param member_function: Serialization function for members
        :returns: Serialized _attribute
        :rtype: bytes
        """
        serialized_attribute = io.BytesIO()
        serialized_attribute.write(_RESERVED)
        serialized_attribute.write(tag.tag)
        serialized_attribute.write(encode_length(_attribute))

        encoded_members = []
        for member in _attribute:
            encoded_members.append(member_function(member))
        for member in sorted(encoded_members):
            serialized_attribute.write(encode_value(member))

        return serialized_attribute.getvalue()

    def _serialize_binary_set(_attribute):
        # type: (dynamodb_types.SET[dynamodb_types.ATTRIBUTE]) -> bytes
        """
        :param set _attribute: Attribute to serialize
        :returns: Serialized _attribute
        :rtype: bytes
        """
        return _serialize_set(Tag.BINARY_SET, _attribute, _transform_binary_value)

    def _serialize_number_set(_attribute):
        # type: (dynamodb_types.SET[dynamodb_types.ATTRIBUTE]) -> bytes
        """
        :param set _attribute: Attribute to serialize
        :returns: Serialized _attribute
        :rtype: bytes
        """
        return _serialize_set(Tag.NUMBER_SET, _attribute, _transform_number_value)

    def _serialize_string_set(_attribute):
        # type: (dynamodb_types.SET[dynamodb_types.ATTRIBUTE]) -> bytes
        """
        :param set _attribute: Attribute to serialize
        :returns: Serialized _attribute
        :rtype: bytes
        """
        return _serialize_set(Tag.STRING_SET, _attribute, _transform_string_value)

    def _serialize_list(_attribute):
        # type: (dynamodb_types.LIST) -> bytes
        """
        :param list _attribute: Attribute to serialize
        :returns: Serialized _attribute
        :rtype: bytes
        """
        serialized_attribute = io.BytesIO()
        serialized_attribute.write(_RESERVED)
        serialized_attribute.write(Tag.LIST.tag)
        serialized_attribute.write(encode_length(_attribute))
        for member in _attribute:
            serialized_attribute.write(serialize_attribute(member))

        return serialized_attribute.getvalue()

    def _serialize_map(_attribute):
        # type: (dynamodb_types.MAP) -> bytes
        """
        :param list _attribute: Attribute to serialize
        :returns: Serialized _attribute
        :rtype: bytes
        """
        serialized_attribute = io.BytesIO()
        serialized_attribute.write(_RESERVED)
        serialized_attribute.write(Tag.MAP.tag)
        serialized_attribute.write(encode_length(_attribute))

        sorted_items = _sorted_key_map(item=_attribute, transform=_transform_string_value)

        for key, value, _original_key in sorted_items:
            serialized_attribute.write(_serialize_string(key))
            serialized_attribute.write(serialize_attribute(value))

        return serialized_attribute.getvalue()

    def _serialize_function(dynamodb_tag):
        # type: (str) -> Callable[[dynamodb_types.ATTRIBUTE], bytes]
        """Locates the appropriate serialization function for the specified DynamoDB attribute tag."""
        serialize_functions = {
            Tag.BINARY.dynamodb_tag: _serialize_binary,
            Tag.BINARY_SET.dynamodb_tag: _serialize_binary_set,
            Tag.NUMBER.dynamodb_tag: _serialize_number,
            Tag.NUMBER_SET.dynamodb_tag: _serialize_number_set,
            Tag.STRING.dynamodb_tag: _serialize_string,
            Tag.STRING_SET.dynamodb_tag: _serialize_string_set,
            Tag.BOOLEAN.dynamodb_tag: _serialize_boolean,
            Tag.NULL.dynamodb_tag: _serialize_null,
            Tag.LIST.dynamodb_tag: _serialize_list,
            Tag.MAP.dynamodb_tag: _serialize_map,
        }
        try:
            return serialize_functions[dynamodb_tag]
        except KeyError:
            raise SerializationError('Unsupported DynamoDB data type: "{}"'.format(dynamodb_tag))

    if not isinstance(attribute, dict):
        raise TypeError('Invalid attribute type "{}": must be dict'.format(type(attribute)))

    if len(attribute) != 1:
        raise SerializationError(
            "cannot serialize attribute: incorrect number of members {} != 1".format(len(attribute))
        )
    key, value = list(attribute.items())[0]
    return _serialize_function(key)(value)
1078395
def encode_w_salt(self, salt, key):
        
        enc_key = '%s%s' % (salt, key)
        enc_val = hashlib.sha1(enc_key).hexdigest()
        return "sha1:%s$%s" % (salt, enc_val)
668662
def serialize_dict_keys(d, prefix=""):
    
    keys = []
    for k, v in d.iteritems():
        fqk = '%s%s' % (prefix, k)
        keys.append(fqk)
        if isinstance(v, dict):
            keys.extend(serialize_dict_keys(v, prefix="%s." % fqk))

    return keys
787017
def _serialize_dict(cls, data):
        '''
        
        '''
        return b64encode(zlib.compress(cPickle.dumps(data, protocol=2))).decode()
690607
def as_dict(self, decode=False):
        
        res = self.database.hgetall(self.key)
        return decode_dict(res) if decode else res
835442
def _encode_params(data):
        

        if isinstance(data, (str, bytes)):
            return data
        elif hasattr(data, 'read'):
            return data
        elif hasattr(data, '__iter__'):
            result = []
            for k, vs in to_key_val_list(data):
                if isinstance(vs, basestring) or not hasattr(vs, '__iter__'):
                    vs = [vs]
                for v in vs:
                    if v is not None:
                        result.append(
                            (k.encode('utf-8') if isinstance(k, str) else k,
                             v.encode('utf-8') if isinstance(v, str) else v))
            return urlencode(result, doseq=True)
        else:
            return data
894464
def __compress_attributes(self, dic):
        
        result = {}
        for k, v in dic.iteritems():
            if isinstance(v, types.ListType) and len(v) == 1:
                if k not in ('msExchMailboxSecurityDescriptor', 'msExchSafeSendersHash', 'msExchBlockedSendersHash',
                             'replicationSignature', 'msExchSafeRecipientsHash', 'sIDHistory',
                             'msRTCSIP-UserRoutingGroupId', 'mSMQDigests', 'mSMQSignCertificates',
                             'msExchMasterAccountSid', 'msExchPreviousAccountSid', 'msExchUMPinChecksum',
                             'userSMIMECertificate', 'userCertificate', 'userCert',
                             'msExchDisabledArchiveGUID', 'msExchUMPinChecksum', 'msExchUMSpokenName',
                             'objectSid', 'objectGUID', 'msExchArchiveGUID', 'thumbnailPhoto', 'msExchMailboxGuid'):
                    try:
                        result[k] = v[0].decode('utf-8')
                    except Exception as e:
                        logging. error("Failed to decode attribute: %s -- %s" % (k, e))
                        result[k] = v[0]
        return result
232255
def encode_metadata(self):
        
        encoded_list = []
        for key, value in iteritems(self.metadata):
            key_str = str(key)  # dict keys may be of any object type.

            # confirm that the key does not contain unwanted characters.
            if re.search(r'^$|[\s,]+', key_str):
                msg = 'Upload-metadata key "{}" cannot be empty nor contain spaces or commas.'
                raise ValueError(msg.format(key_str))

            value_bytes = b(value)  # python 3 only encodes bytes
            encoded_list.append('{} {}'.format(key_str, b64encode(value_bytes).decode('ascii')))
        return encoded_list
644782
def encode_byte_values(map_: Dict):
    
    for k, v in map_.items():
        if isinstance(v, bytes):
            map_[k] = encode_hex(v)
981278
def Encode(self, attribute, value):
    """"""
    required_type = self._attribute_types.get(attribute, "bytes")

    if required_type == "integer":
      return rdf_structs.SignedVarintEncode(int(value))
    elif required_type == "unsigned_integer":
      return rdf_structs.VarintEncode(int(value))
    elif hasattr(value, "SerializeToString"):
      return value.SerializeToString()
    elif isinstance(value, bytes):
      return value
    elif isinstance(value, Text):
      return value.encode("utf-8")
    else:
      return str(value).encode("utf-8")