⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pickle.py

📁 mallet是自然语言处理、机器学习领域的一个开源项目。
💻 PY
📖 第 1 页 / 共 2 页
字号:
        else:            stuff = getstate()            _keep_alive(stuff, memo)        save(stuff)        write(BUILD)    dispatch[InstanceType] = save_inst    def save_global(self, object, name = None):        write = self.write        memo = self.memo        if name is None:            name = object.__name__        try:            module = object.__module__        except AttributeError:            module = whichmodule(object, name)        try:            __import__(module)            mod = sys.modules[module]            klass = getattr(mod, name)        except (ImportError, KeyError, AttributeError):            raise PicklingError(                "Can't pickle %r: it's not found as %s.%s" %                (object, module, name))        else:            if klass is not object:                raise PicklingError(                    "Can't pickle %r: it's not the same object as %s.%s" %                    (object, module, name))        memo_len = len(memo)        write(GLOBAL + module + '\n' + name + '\n' +            self.put(memo_len))        memo[id(object)] = (memo_len, object)    dispatch[ClassType] = save_global    dispatch[FunctionType] = save_global    dispatch[BuiltinFunctionType] = save_global    dispatch[TypeType] = save_globaldef _keep_alive(x, memo):    """Keeps a reference to the object x in the memo.    Because we remember objects by their id, we have    to assure that possibly temporary objects are kept    alive by referencing them.    We store a reference at the id of the memo, which should    normally not be used unless someone tries to deepcopy    the memo itself...    """    try:        memo[id(memo)].append(x)    except KeyError:        # aha, this is the first one :-)        memo[id(memo)]=[x]classmap = {} # called classmap for backwards compatibilitydef whichmodule(func, funcname):    """Figure out the module in which a function occurs.    Search sys.modules for the module.    Cache in classmap.    Return a module name.    If the function cannot be found, return __main__.    """    if classmap.has_key(func):        return classmap[func]    for name, module in sys.modules.items():        if module is None:            continue # skip dummy package entries        if name != '__main__' and \            hasattr(module, funcname) and \            getattr(module, funcname) is func:            break    else:        name = '__main__'    classmap[func] = name    return nameclass Unpickler:    def __init__(self, file):        self.readline = file.readline        self.read = file.read        self.memo = {}    def load(self):        self.mark = object() # any new unique object        self.stack = []        self.append = self.stack.append        read = self.read        dispatch = self.dispatch        try:            while 1:                key = read(1)                dispatch[key](self)        except _Stop, stopinst:            return stopinst.value    def marker(self):        stack = self.stack        mark = self.mark        k = len(stack)-1        while stack[k] is not mark: k = k-1        return k    dispatch = {}    def load_eof(self):        raise EOFError    dispatch[''] = load_eof    def load_persid(self):        pid = self.readline()[:-1]        self.append(self.persistent_load(pid))    dispatch[PERSID] = load_persid    def load_binpersid(self):        stack = self.stack        pid = stack[-1]        del stack[-1]        self.append(self.persistent_load(pid))    dispatch[BINPERSID] = load_binpersid    def load_none(self):        self.append(None)    dispatch[NONE] = load_none    def load_int(self):        data = self.readline()        try:            self.append(int(data))        except ValueError:            self.append(long(data))    dispatch[INT] = load_int    def load_binint(self):        self.append(mloads('i' + self.read(4)))    dispatch[BININT] = load_binint    def load_binint1(self):        self.append(mloads('i' + self.read(1) + '\000\000\000'))    dispatch[BININT1] = load_binint1    def load_binint2(self):        self.append(mloads('i' + self.read(2) + '\000\000'))    dispatch[BININT2] = load_binint2    def load_long(self):        self.append(long(self.readline()[:-1], 0))    dispatch[LONG] = load_long    def load_float(self):        self.append(float(self.readline()[:-1]))    dispatch[FLOAT] = load_float    def load_binfloat(self, unpack=struct.unpack):        self.append(unpack('>d', self.read(8))[0])    dispatch[BINFLOAT] = load_binfloat    def load_string(self):        rep = self.readline()[:-1]        if not self._is_string_secure(rep):            raise ValueError, "insecure string pickle"        self.append(eval(rep,                         {'__builtins__': {}})) # Let's be careful    dispatch[STRING] = load_string    def _is_string_secure(self, s):        """Return true if s contains a string that is safe to eval        The definition of secure string is based on the implementation        in cPickle.  s is secure as long as it only contains a quoted        string and optional trailing whitespace.        """        q = s[0]        if q not in ("'", '"'):            return 0        # find the closing quote        offset = 1        i = None        while 1:            try:                i = s.index(q, offset)            except ValueError:                # if there is an error the first time, there is no                # close quote                if offset == 1:                    return 0            if s[i-1] != '\\':                break            # check to see if this one is escaped            nslash = 0            j = i - 1            while j >= offset and s[j] == '\\':                j = j - 1                nslash = nslash + 1            if nslash % 2 == 0:                break            offset = i + 1        for c in s[i+1:]:            if ord(c) > 32:                return 0        return 1    def load_binstring(self):        len = mloads('i' + self.read(4))        self.append(self.read(len))    dispatch[BINSTRING] = load_binstring    def load_unicode(self):        self.append(unicode(self.readline()[:-1],'raw-unicode-escape'))    dispatch[UNICODE] = load_unicode    def load_binunicode(self):        len = mloads('i' + self.read(4))        self.append(unicode(self.read(len),'utf-8'))    dispatch[BINUNICODE] = load_binunicode    def load_short_binstring(self):        len = mloads('i' + self.read(1) + '\000\000\000')        self.append(self.read(len))    dispatch[SHORT_BINSTRING] = load_short_binstring    def load_tuple(self):        k = self.marker()        self.stack[k:] = [tuple(self.stack[k+1:])]    dispatch[TUPLE] = load_tuple    def load_empty_tuple(self):        self.stack.append(())    dispatch[EMPTY_TUPLE] = load_empty_tuple    def load_empty_list(self):        self.stack.append([])    dispatch[EMPTY_LIST] = load_empty_list    def load_empty_dictionary(self):        self.stack.append({})    dispatch[EMPTY_DICT] = load_empty_dictionary    def load_list(self):        k = self.marker()        self.stack[k:] = [self.stack[k+1:]]    dispatch[LIST] = load_list    def load_dict(self):        k = self.marker()        d = {}        items = self.stack[k+1:]        for i in range(0, len(items), 2):            key = items[i]            value = items[i+1]            d[key] = value        self.stack[k:] = [d]    dispatch[DICT] = load_dict    def load_inst(self):        k = self.marker()        args = tuple(self.stack[k+1:])        del self.stack[k:]        module = self.readline()[:-1]        name = self.readline()[:-1]        klass = self.find_class(module, name)        instantiated = 0        if (not args and type(klass) is ClassType and            not hasattr(klass, "__getinitargs__")):            try:                value = _EmptyClass()                value.__class__ = klass                instantiated = 1            except RuntimeError:                # In restricted execution, assignment to inst.__class__ is                # prohibited                pass        if not instantiated:            try:                if not hasattr(klass, '__safe_for_unpickling__'):                    raise UnpicklingError('%s is not safe for unpickling' %                                          klass)                value = apply(klass, args)            except TypeError, err:                raise TypeError, "in constructor for %s: %s" % (                    klass.__name__, str(err)), sys.exc_info()[2]        self.append(value)    dispatch[INST] = load_inst    def load_obj(self):        stack = self.stack        k = self.marker()        klass = stack[k + 1]        del stack[k + 1]        args = tuple(stack[k + 1:])        del stack[k:]        instantiated = 0        if (not args and type(klass) is ClassType and            not hasattr(klass, "__getinitargs__")):            try:                value = _EmptyClass()                value.__class__ = klass                instantiated = 1            except RuntimeError:                # In restricted execution, assignment to inst.__class__ is                # prohibited                pass        if not instantiated:            value = apply(klass, args)        self.append(value)    dispatch[OBJ] = load_obj    def load_global(self):        module = self.readline()[:-1]        name = self.readline()[:-1]        klass = self.find_class(module, name)        self.append(klass)    dispatch[GLOBAL] = load_global    def find_class(self, module, name):        __import__(module)        mod = sys.modules[module]        klass = getattr(mod, name)        return klass    def load_reduce(self):        stack = self.stack        callable = stack[-2]        arg_tup  = stack[-1]        del stack[-2:]        if type(callable) is not ClassType:            if not safe_constructors.has_key(callable):                try:                    safe = callable.__safe_for_unpickling__                except AttributeError:                    safe = None                if not safe:                    raise UnpicklingError, "%s is not safe for " \                                           "unpickling" % callable        if arg_tup is None:            value = callable.__basicnew__()        else:            value = apply(callable, arg_tup)        self.append(value)    dispatch[REDUCE] = load_reduce    def load_pop(self):        del self.stack[-1]    dispatch[POP] = load_pop    def load_pop_mark(self):        k = self.marker()        del self.stack[k:]    dispatch[POP_MARK] = load_pop_mark    def load_dup(self):        self.append(self.stack[-1])    dispatch[DUP] = load_dup    def load_get(self):        self.append(self.memo[self.readline()[:-1]])    dispatch[GET] = load_get    def load_binget(self):        i = mloads('i' + self.read(1) + '\000\000\000')        self.append(self.memo[`i`])    dispatch[BINGET] = load_binget    def load_long_binget(self):        i = mloads('i' + self.read(4))        self.append(self.memo[`i`])    dispatch[LONG_BINGET] = load_long_binget    def load_put(self):        self.memo[self.readline()[:-1]] = self.stack[-1]    dispatch[PUT] = load_put    def load_binput(self):        i = mloads('i' + self.read(1) + '\000\000\000')        self.memo[`i`] = self.stack[-1]    dispatch[BINPUT] = load_binput    def load_long_binput(self):        i = mloads('i' + self.read(4))        self.memo[`i`] = self.stack[-1]    dispatch[LONG_BINPUT] = load_long_binput    def load_append(self):        stack = self.stack        value = stack[-1]        del stack[-1]        list = stack[-1]        list.append(value)    dispatch[APPEND] = load_append    def load_appends(self):        stack = self.stack        mark = self.marker()        list = stack[mark - 1]        for i in range(mark + 1, len(stack)):            list.append(stack[i])        del stack[mark:]    dispatch[APPENDS] = load_appends    def load_setitem(self):        stack = self.stack        value = stack[-1]        key = stack[-2]        del stack[-2:]        dict = stack[-1]        dict[key] = value    dispatch[SETITEM] = load_setitem    def load_setitems(self):        stack = self.stack        mark = self.marker()        dict = stack[mark - 1]        for i in range(mark + 1, len(stack), 2):            dict[stack[i]] = stack[i + 1]        del stack[mark:]    dispatch[SETITEMS] = load_setitems    def load_build(self):        stack = self.stack        value = stack[-1]        del stack[-1]        inst = stack[-1]        try:            setstate = inst.__setstate__        except AttributeError:            try:                inst.__dict__.update(value)            except RuntimeError:                # XXX In restricted execution, the instance's __dict__ is not                # accessible.  Use the old way of unpickling the instance                # variables.  This is a semantic different when unpickling in                # restricted vs. unrestricted modes.                for k, v in value.items():                    setattr(inst, k, v)        else:            setstate(value)    dispatch[BUILD] = load_build    def load_mark(self):        self.append(self.mark)    dispatch[MARK] = load_mark    def load_stop(self):        value = self.stack[-1]        del self.stack[-1]        raise _Stop(value)    dispatch[STOP] = load_stop# Helper class for load_inst/load_objclass _EmptyClass:    pass# Shorthandstry:    from cStringIO import StringIOexcept ImportError:    from StringIO import StringIOdef dump(object, file, bin = 0):    Pickler(file, bin).dump(object)def dumps(object, bin = 0):    file = StringIO()    Pickler(file, bin).dump(object)    return file.getvalue()def load(file):    return Unpickler(file).load()def loads(str):    file = StringIO(str)    return Unpickler(file).load()

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -