# Path: aredis/utils.py
# def b(x):
#     return x.encode('latin-1') if not isinstance(x, bytes) else x
# 
# Path: aredis/exceptions.py
# class SerializeError(CacheError):
#     pass
# 
# class CompressError(CacheError):
#     pass


import hashlib
import time
import zlib
    import ujson as json
    import json
from aredis.utils import b
from aredis.exceptions import (SerializeError,
                               CompressError)

class Compressor:
    """
    Uses zlib to compress and decompress Redis cache. You may implement your
    own Compressor implementing `compress` and `decompress` methods
    """

    min_length = 15
    preset = 6

    def __init__(self, encoding='utf-8'):
        self.encoding = encoding

    def _trans_type(self, content):
        if isinstance(content, str):
            content = content.encode(self.encoding)
        elif isinstance(content, int):
            content = b(str(content))
        elif isinstance(content, float):
            content = b(repr(content))
        if not isinstance(content, bytes):
            raise TypeError('Wrong data type({}) to compress'.format(type(content)))
        return content

    def compress(self, content):
        content = self._trans_type(content)
        if len(content) > self.min_length:
            try:
                return zlib.compress(content, self.preset)
            except zlib.error as exc:

# Path: aredis/utils.py
# def b(x):
#     return x.encode('latin-1') if not isinstance(x, bytes) else x
# 
# Path: aredis/exceptions.py
# class SerializeError(CacheError):
#     pass
# 
# class CompressError(CacheError):
#     pass


# Path: aredis/cache.py
import hashlib
import time
import zlib
    import ujson as json
    import json
from aredis.utils import b
from aredis.exceptions import (SerializeError,
                               CompressError)


class Compressor:
    """
    Uses zlib to compress and decompress Redis cache. You may implement your
    own Compressor implementing `compress` and `decompress` methods
    """

    min_length = 15
    preset = 6

    def __init__(self, encoding='utf-8'):
        self.encoding = encoding

    def _trans_type(self, content):
        if isinstance(content, str):
            content = content.encode(self.encoding)
        elif isinstance(content, int):
            content = b(str(content))
        elif isinstance(content, float):
            content = b(repr(content))
        if not isinstance(content, bytes):
            raise TypeError('Wrong data type({}) to compress'.format(type(content)))
        return content

    def compress(self, content):
        content = self._trans_type(content)
        if len(content) > self.min_length:
            try:
                return zlib.compress(content, self.preset)
            except zlib.error as exc:

                raise CompressError('Content can not be compressed.')