def default(self, obj):
        """
        Encodes unknown object types (we use it to make extended types)
        """
        if isinstance(obj, datetime.datetime):
            # Serialize date-time objects. Make sure they're naive.
            if obj.tzinfo is not None:
                raise TypeError('Cannot encode time zone-aware date-times to MessagePack')
            # Then, work out the timestamp in seconds.
            seconds = (obj - datetime.datetime(1970, 1, 1)).total_seconds()
            microseconds = int(seconds * 1000000.0)
            # Then pack it into a big-endian signed 64-bit integer.
            return msgpack.ExtType(
                self.EXT_DATETIME,
                self.STRUCT_DATETIME.pack(microseconds),
            )
        elif isinstance(obj, datetime.date):
            # Serialize local-date objects by packing to a big-endian unsigned short and two big-endian unsigned chars.
            return msgpack.ExtType(
                self.EXT_DATE,
                self.STRUCT_DATE.pack(obj.year, obj.month, obj.day),
            )
        elif isinstance(obj, datetime.time):
            # Serialize dateless-time objects by packing to three big-endian unsigned chars and a big-endian unsigned
            # 32-bit integer.
            return msgpack.ExtType(
                self.EXT_TIME,
                self.STRUCT_TIME.pack(obj.hour, obj.minute, obj.second, obj.microsecond),
            )
        elif isinstance(obj, decimal.Decimal):
            obj_str = six.text_type(obj)[:65535].encode('utf-8')
            obj_len = len(obj_str)
            obj_encoder = struct.Struct(str('!H{}s'.format(obj_len)))
            return msgpack.ExtType(
                self.EXT_DECIMAL,
                obj_encoder.pack(obj_len, obj_str),
            )
        elif isinstance(obj, currint.Amount):
            # Serialize Amount objects. Start with the lowercased currency code as bytes.
            code = obj.currency.code.upper()
            if isinstance(code, six.text_type):
                code = code.encode('ascii')
            # Then pack it in with the minor value.
            return msgpack.ExtType(
                self.EXT_CURRINT,
                self.STRUCT_CURRINT.pack(code, obj.value),
            )
        else:
            # Wuh-woh
            raise TypeError('Cannot encode value of type {} to MessagePack: {}'.format(type(obj).__name__, obj))
def default ( self , obj ) : if isinstance ( obj , datetime . datetime ) : if obj . tzinfo is not None : raise TypeError ( 'Cannot encode time zone-aware date-times to MessagePack' ) seconds = ( obj - datetime . datetime ( 1970 , 1 , 1 ) ) . total_seconds ( ) microseconds = int ( seconds * 1000000.0 ) return msgpack . ExtType ( self . EXT_DATETIME , self . STRUCT_DATETIME . pack ( microseconds ) , ) elif isinstance ( obj , datetime . date ) : return msgpack . ExtType ( self . EXT_DATE , self . STRUCT_DATE . pack ( obj . year , obj . month , obj . day ) , ) elif isinstance ( obj , datetime . time ) : return msgpack . ExtType ( self . EXT_TIME , self . STRUCT_TIME . pack ( obj . hour , obj . minute , obj . second , obj . microsecond ) , ) elif isinstance ( obj , decimal . Decimal ) : obj_str = six . text_type ( obj ) [ : 65535 ] . encode ( 'utf-8' ) obj_len = len ( obj_str ) obj_encoder = struct . Struct ( str ( '!H{}s' . format ( obj_len ) ) ) return msgpack . ExtType ( self . EXT_DECIMAL , obj_encoder . pack ( obj_len , obj_str ) , ) elif isinstance ( obj , currint . Amount ) : code = obj . currency . code . upper ( ) if isinstance ( code , six . text_type ) : code = code . encode ( 'ascii' ) return msgpack . ExtType ( self . EXT_CURRINT , self . STRUCT_CURRINT . pack ( code , obj . value ) , ) else : raise TypeError ( 'Cannot encode value of type {} to MessagePack: {}' . format ( type ( obj ) . __name__ , obj ) )
Encodes unknown object types (we use it to make extended types)