Convert arbitrary pickable Python Object to bytes.

    **中文文档**

    将可Pickle化的Python对象转化为bytestr
499271
def obj2bytes(obj, pk_protocol=py23.pk_protocol):
    
    return pickle.dumps(obj, protocol=pk_protocol)
235103
def to_bytes(s, encoding=None, errors='strict'):
    """"""
    encoding = encoding or 'utf-8'

    if is_unicode(s):
        return s.encode(encoding, errors)
    elif is_strlike(s):
        return s
    else:
        if six.PY2:
            return str(s)
        else:
            return str(s).encode(encoding, errors)
404518
def loads(astring):
        """"""
        try:
            return pickle.loads(lzma.decompress(astring))
        except lzma.LZMAError as e:
            raise SerializerError(
                'Cannot decompress object ("{}")'.format(str(e))
            )
        except pickle.UnpicklingError as e:
            raise SerializerError(
                'Cannot restore object ("{}")'.format(str(e))
            )
869952
def convert_bytes(value):
    
    n = np.rint(len(str(value))/4).astype(int)
    return value/(1024**n), sizes[n]
695326
def dump(self, obj):
        ''''''
        pickle.dump(obj, self._file, protocol=self._protocol)
600675
def from_data(cls, data):
        
        obj = cls()
        with contextlib.closing(BytesIO(data)) as file_handle:
            obj.load_file(file_handle)
        return obj
630512
def save_pickle(obj, outfile, protocol=2):
    
    with open(outfile, 'wb') as f:
        pickle.dump(obj, f, protocol=protocol)

    return outfile
621938
def pickled(cls, obj, **kwargs):
        
        self = cls(**kwargs)
        try:
            self.data = pickle__dumps(obj, protocol=2)
        except pickle.PicklingError:
            e = sys.exc_info()[1]
            self.data = pickle__dumps(CallError(e), protocol=2)
        return self
883039
def pickle_to_param(obj, name):
	
	return from_pyvalue(u"pickle:%s" % name, unicode(pickle.dumps(obj)))
408107
def decompress(obj, return_type="bytes"):
    
    if isinstance(obj, binary_type):
        b = zlib.decompress(obj)
    elif isinstance(obj, string_types):
        b = zlib.decompress(base64.b64decode(obj.encode("utf-8")))
    else:
        raise TypeError("input cannot be anything other than str and bytes!")

    if return_type == "bytes":
        return b
    elif return_type == "str":
        return b.decode("utf-8")
    elif return_type == "obj":
        return pickle.loads(b)
    else:
        raise ValueError(
            "'return_type' has to be one of 'bytes', 'str' or 'obj'!")
420336
def pickle(self, f, protocol=-1):
        
        try: #Py2
            import cPickle as pickle
            if isinstance(f, basestring):
                with open(f, 'wb') as f:
                    pickle.dump(self, f, protocol)
            else:
                pickle.dump(self, f, protocol)
        except ImportError: #python3
            import pickle
            if isinstance(f, str):
                with open(f, 'wb') as f:
                    pickle.dump(self, f, protocol)
            else:
                pickle.dump(self, f, protocol)
451008
def to_bytes(s):
    
    if bytes != str:
        if type(s) == str:
            return s.encode('utf-8')
    return s
188615
def to_bytes(s, encoding="utf-8"):
    """"""
    if isinstance(s, six.binary_type):
        return s
    if six.PY3:
        return bytes(s, encoding)
    return s.encode(encoding)
253060
def to_bytes(s, encoding="utf8"):
    """"""
    if PY_VERSION == 2:
        b = bytes(s)
    elif PY_VERSION == 3:
        b = bytes(s, encoding)
    else:
        raise ValueError("Is Python 4 out already?")

    return b
557783
def obj_to_string(obj):
    ''''''
    if not obj:
        return None
    elif isinstance(obj, bytes):
        return obj.decode('utf-8')
    elif isinstance(obj, basestring):
        return obj
    elif is_lazy_string(obj):
        return obj.value
    elif hasattr(obj, '__html__'):
        return obj.__html__()
    else:
        return str(obj)
1331774
def to_bytes(s, encoding=None, errors=None):
    ''''''
    if not isinstance(s, bytes):
        return ('%s' % s).encode(encoding or 'utf-8', errors or 'strict')
    elif not encoding or encoding == 'utf-8':
        return s
    else:
        d = s.decode('utf-8')
        return d.encode(encoding, errors or 'strict')
1113636
def to_bytes(s):  # pragma: nocover
    
    if isinstance(s, bytes):
        return s
    if isinstance(s, str) or is_unicode(s):
        return s.encode("utf-8")
    try:
        return unicode(s).encode("utf-8")
    except NameError:
        return str(s).encode("utf-8")