def deserialize_object(buffers, g=None): """reconstruct an object serialized by serialize_object from data buffers. Parameters ---------- bufs : list of buffers/bytes g : globals to be used when uncanning Returns ------- (newobj, bufs) : unpacked object, and the list of remaining unused buffers. """ bufs = list(buffers) pobj = buffer_to_bytes_py2(bufs.pop(0)) canned = pickle.loads(pobj) if istype(canned, sequence_types) and len(canned) < MAX_ITEMS: for c in canned: _restore_buffers(c, bufs) newobj = uncan_sequence(canned, g) elif istype(canned, dict) and len(canned) < MAX_ITEMS: newobj = {} for k in sorted(canned): c = canned[k] _restore_buffers(c, bufs) newobj[k] = uncan(c, g) else: _restore_buffers(canned, bufs) newobj = uncan(canned, g) return newobj, bufs
def set_unique_id_int(data: Series, autoname_bolean_col: bool = True, return_map: bool = False) -> Series: """ Mengambil `Series` dengan nilai membernya menjadi id unik int. :param data: pandas.Series. :param return_map: `True` maka fungsi ini akan me-return map uniqeId-nya dari `unique_id_int` juga. :param autoname_bolean_col: `True` -> jika kolom tipe `object` dengan nilai id hanya 2, maka nama kolom tersebut akan diubah menjadi nilai yang memiliki nilai 1. :return: pandas.Series dengan membernya id unik int. """ #print("afhaiuhgphagha") if istype(data, DataFrame): raise TypeError("`data` merupakan `DataFrame` yang dapat menyebabkan error karena tiap elemen iterasi " "mengembalikan `Series`.") ids = unique_id_int(data) if autoname_bolean_col and len(ids.keys()) == 2: data = data.copy() new = None for k in ids: if ids[k] == 1: new = k break if new is not None: data.name = new # noinspection PyTypeChecker res = data.apply(lambda x: ids[x]) if not return_map: return res else: return res, ids
def serialize_object(obj, buffer_threshold=MAX_BYTES, item_threshold=MAX_ITEMS): """Serialize an object into a list of sendable buffers. Parameters ---------- obj : object The object to be serialized buffer_threshold : int The threshold (in bytes) for pulling out data buffers to avoid pickling them. item_threshold : int The maximum number of items over which canning will iterate. Containers (lists, dicts) larger than this will be pickled without introspection. Returns ------- [bufs] : list of buffers representing the serialized object. """ buffers = [] if istype(obj, sequence_types) and len(obj) < item_threshold: cobj = can_sequence(obj) for c in cobj: buffers.extend(_extract_buffers(c, buffer_threshold)) elif istype(obj, dict) and len(obj) < item_threshold: cobj = {} for k in sorted(obj): c = can(obj[k]) buffers.extend(_extract_buffers(c, buffer_threshold)) cobj[k] = c else: cobj = can(obj) buffers.extend(_extract_buffers(cobj, buffer_threshold)) buffers.insert(0, pickle.dumps(cobj, PICKLE_PROTOCOL)) return buffers