当前位置: 首页>>代码示例>>Python>>正文


Python Unpickler.load方法代码示例

本文整理汇总了Python中cPickle.Unpickler.load方法的典型用法代码示例。如果您正苦于以下问题:Python Unpickler.load方法的具体用法?Python Unpickler.load怎么用?Python Unpickler.load使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在cPickle.Unpickler的用法示例。


在下文中一共展示了Unpickler.load方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: deserialize

# 需要导入模块: from cPickle import Unpickler [as 别名]
# 或者: from cPickle.Unpickler import load [as 别名]
    def deserialize(self, event, state):
        assert IFullDeserializationEvent.isImplementedBy(event)
        assert isinstance(event.obj, Persistent)

        # Set up to resolve cyclic references to the object.
        event.deserialized('self', event.obj)

        state = state.strip()
        if state:
            if state.startswith('#'):
                # Text-encoded pickles start with a pound sign.
                # (A pound sign is not a valid pickle opcode.)
                data = decode_from_text(state)
            else:
                data = state
            infile = StringIO(data)
            u = Unpickler(infile)
            u.persistent_load = event.resolve_internal
            s = u.load()
            if not hasattr(s, 'items'):
                # Turn the list back into a dictionary
                s_list = s
                s = {}
                for key, value in s_list:
                    s[key] = value
            event.obj.__dict__.update(s)
            try:
                unmanaged = u.load()
            except EOFError:
                # old pickle with no list of unmanaged objects
                pass
            else:
                event.upos.extend(unmanaged)
开发者ID:goschtl,项目名称:zope,代码行数:35,代码来源:serializers.py

示例2: zodb_unpickle

# 需要导入模块: from cPickle import Unpickler [as 别名]
# 或者: from cPickle.Unpickler import load [as 别名]
def zodb_unpickle(data):
    """Unpickle an object stored using the format expected by ZODB."""
    f = StringIO(data)
    u = Unpickler(f)
    klass_info = u.load()
    if isinstance(klass_info, types.TupleType):
        if isinstance(klass_info[0], types.TupleType):
            modname, klassname = klass_info[0]
            args = klass_info[1]
        else:
            modname, klassname = klass_info
            args = None
        if modname == "__main__":
            ns = globals()
        else:
            mod = import_helper(modname)
            ns = mod.__dict__
        try:
            klass = ns[klassname]
        except KeyError:
            sys.stderr.write("can't find %s in %s" % (klassname,
                                                      repr(ns)))
        inst = klass()
    else:
        raise ValueError, "expected class info: %s" % repr(klass_info)
    state = u.load()
    inst.__setstate__(state)
    return inst
开发者ID:OS2World,项目名称:APP-SERVER-Zope,代码行数:30,代码来源:StorageTestBase.py

示例3: test_config_and_collector_pickling

# 需要导入模块: from cPickle import Unpickler [as 别名]
# 或者: from cPickle.Unpickler import load [as 别名]
 def test_config_and_collector_pickling(self):
     from cPickle import Pickler, Unpickler
     dir1 = self.tmpdir.ensure("somedir", dir=1)
     config = py.test.config._reparse([self.tmpdir])
     col = config.getfsnode(config.topdir)
     col1 = col.join(dir1.basename)
     assert col1.parent is col 
     io = py.std.cStringIO.StringIO()
     pickler = Pickler(io)
     pickler.dump(config)
     pickler.dump(col)
     pickler.dump(col1)
     pickler.dump(col)
     io.seek(0) 
     unpickler = Unpickler(io)
     newconfig = unpickler.load()
     topdir = self.tmpdir.ensure("newtopdir", dir=1)
     newconfig._initafterpickle(topdir)
     topdir.ensure("somedir", dir=1)
     newcol = unpickler.load()
     newcol2 = unpickler.load()
     newcol3 = unpickler.load()
     assert newcol2._config is newconfig 
     assert newcol2.parent == newcol 
     assert newcol._config is newconfig
     assert newconfig.topdir == topdir
     assert newcol3 is newcol
     assert newcol.fspath == topdir 
     assert newcol2.fspath.basename == dir1.basename
     assert newcol2.fspath.relto(topdir)
开发者ID:mickg10,项目名称:DARLAB,代码行数:32,代码来源:test_config.py

示例4: load_weights

# 需要导入模块: from cPickle import Unpickler [as 别名]
# 或者: from cPickle.Unpickler import load [as 别名]
 def load_weights(self):
     ''' Loads the stored data from previous sessions, if possible.'''
     valid = False
     try: fp = open(self.filename, 'r')
     except IOError:
         self.log_debug(11, "Couldn't read stats file '%s'", self.filename)
     else:
         self.log_debug(11, "Loading stats file '%s'", self.filename)
         try:
             pickler = Unpickler(fp)
             self.input_headers = pickler.load()
             wi = pickler.load()
             self.output_headers = pickler.load()
             wo = pickler.load()
             #self.seasons = pickler.load()
             #self.powers = pickler.load()
             #self.locs = pickler.load()
             #self.provinces = pickler.load()
             #self.centers = pickler.load()
             #self.coastals = pickler.load()
             #self.coastlines = pickler.load()
             #self.borders = pickler.load()
         finally:
             fp.close()
         
         ni = len(self.input_headers)
         no = len(self.output_headers)
         nh = len(wo)
         self.log_debug(7, "%d inputs => %d hidden => %d outputs",
                 ni, nh, no)
         self.net = NN(ni, nh, no, wi, wo)
         valid = True
     return valid
开发者ID:eswald,项目名称:parlance,代码行数:35,代码来源:neurotic.py

示例5: zodb_unpickle

# 需要导入模块: from cPickle import Unpickler [as 别名]
# 或者: from cPickle.Unpickler import load [as 别名]
def zodb_unpickle(data):
    """Unpickle an object stored using the format expected by ZODB."""
    f = StringIO(data)
    u = Unpickler(f)
    u.persistent_load = persistent_load
    klass_info = u.load()
    if isinstance(klass_info, tuple):
        if isinstance(klass_info[0], type):
            # Unclear:  what is the second part of klass_info?
            klass, xxx = klass_info
            assert not xxx
        else:
            if isinstance(klass_info[0], tuple):
                modname, klassname = klass_info[0]
            else:
                modname, klassname = klass_info
            if modname == "__main__":
                ns = globals()
            else:
                mod = import_helper(modname)
                ns = mod.__dict__
            try:
                klass = ns[klassname]
            except KeyError:
                print >> sys.stderr, "can't find %s in %r" % (klassname, ns)
        inst = klass()
    else:
        raise ValueError("expected class info: %s" % repr(klass_info))
    state = u.load()
    inst.__setstate__(state)
    return inst
开发者ID:grodniewicz,项目名称:oship,代码行数:33,代码来源:StorageTestBase.py

示例6: setstate

# 需要导入模块: from cPickle import Unpickler [as 别名]
# 或者: from cPickle.Unpickler import load [as 别名]
 def setstate(self, object):
     """
     Unlike the 'stock' Connection class' setstate, this method
     doesn't raise ConflictErrors.  This is potentially dangerous
     for applications that need absolute consistency, but
     sessioning is not one of those.
     """
     oid=object._p_oid
     invalid = self._invalid
     if invalid(None):
         # only raise a conflict if there was
         # a mass invalidation, but not if we see this
         # object's oid as invalid
         raise ConflictError, `oid`
     p, serial = self._storage.load(oid, self._version)
     file=StringIO(p)
     unpickler=Unpickler(file)
     unpickler.persistent_load=self._persistent_load
     unpickler.load()
     state = unpickler.load()
     if hasattr(object, '__setstate__'):
         object.__setstate__(state)
     else:
         d=object.__dict__
         for k,v in state.items(): d[k]=v
     object._p_serial=serial
开发者ID:Andyvs,项目名称:TrackMonthlyExpenses,代码行数:28,代码来源:LowConflictConnection.py

示例7: __init__

# 需要导入模块: from cPickle import Unpickler [as 别名]
# 或者: from cPickle.Unpickler import load [as 别名]
 def __init__(self, inp_filename):
   self.filename = inp_filename
   with open(self.filename, 'rb') as inpfile:
     reader = Unpickler(inpfile)
     self.numgroups = reader.load()
     self.tot_msgs = reader.load()
     self.num_msgs = reader.load()
开发者ID:bharcode,项目名称:Programming,代码行数:9,代码来源:message_iterators.py

示例8: load_state

# 需要导入模块: from cPickle import Unpickler [as 别名]
# 或者: from cPickle.Unpickler import load [as 别名]
    def load_state(self, state):
        """Load an image_set_list's state from the string returned from save_state"""

        self.__image_sets = []
        self.__image_sets_by_key = {}

        # Make a safe unpickler
        p = Unpickler(StringIO(state))

        def find_global(module_name, class_name):
            logger.debug("Pickler wants %s:%s", module_name, class_name)
            if module_name not in ("numpy", "numpy.core.multiarray"):
                logger.critical(
                    "WARNING WARNING WARNING - your batch file has asked to load %s.%s."
                    " If this looks in any way suspicious please contact us at www.cellprofiler.org",
                    module_name,
                    class_name,
                )
                raise ValueError("Illegal attempt to unpickle class %s.%s", (module_name, class_name))
            __import__(module_name)
            mod = sys.modules[module_name]
            return getattr(mod, class_name)

        p.find_global = find_global

        count = p.load()
        all_keys = [p.load() for i in range(count)]
        self.__legacy_fields = p.load()
        #
        # Have to do in this order in order for the image set's
        # legacy_fields property to hook to the right legacy_fields
        #
        for i in range(count):
            self.get_image_set(all_keys[i])
开发者ID:dettmering,项目名称:CellProfiler,代码行数:36,代码来源:cpimage.py

示例9: test_config_and_collector_pickling

# 需要导入模块: from cPickle import Unpickler [as 别名]
# 或者: from cPickle.Unpickler import load [as 别名]
 def test_config_and_collector_pickling(self, testdir):
     from cPickle import Pickler, Unpickler
     tmpdir = testdir.tmpdir
     dir1 = tmpdir.ensure("somedir", dir=1)
     config = testdir.parseconfig()
     col = config.getfsnode(config.topdir)
     col1 = col.join(dir1.basename)
     assert col1.parent is col 
     io = py.std.cStringIO.StringIO()
     pickler = Pickler(io)
     pickler.dump(col)
     pickler.dump(col1)
     pickler.dump(col)
     io.seek(0) 
     unpickler = Unpickler(io)
     topdir = tmpdir.ensure("newtopdir", dir=1)
     topdir.ensure("somedir", dir=1)
     old = topdir.chdir()
     try:
         newcol = unpickler.load()
         newcol2 = unpickler.load()
         newcol3 = unpickler.load()
         assert newcol2.config is newcol.config
         assert newcol2.parent == newcol 
         assert newcol2.config.topdir.realpath() == topdir.realpath()
         assert newcol.fspath.realpath() == topdir.realpath()
         assert newcol2.fspath.basename == dir1.basename
         assert newcol2.fspath.relto(newcol2.config.topdir)
     finally:
         old.chdir() 
开发者ID:enyst,项目名称:plexnet,代码行数:32,代码来源:test_pickling.py

示例10: oldstate

# 需要导入模块: from cPickle import Unpickler [as 别名]
# 或者: from cPickle.Unpickler import load [as 别名]
 def oldstate(self, object, serial):
     oid=object._p_oid
     p = self._storage.loadSerial(oid, serial)
     file=StringIO(p)
     unpickler=Unpickler(file)
     unpickler.persistent_load=self._persistent_load
     unpickler.load()
     return  unpickler.load()
开发者ID:OS2World,项目名称:APP-SERVER-Zope,代码行数:10,代码来源:Connection.py

示例11: state

# 需要导入模块: from cPickle import Unpickler [as 别名]
# 或者: from cPickle.Unpickler import load [as 别名]
def state(self, oid, serial, prfactory, p=''):
    p = p or self.loadSerial(oid, serial)
    file = StringIO(p)
    unpickler = Unpickler(file)
    unpickler.find_global = find_global
    unpickler.persistent_load = prfactory.persistent_load
    unpickler.load() # skip the class tuple
    return unpickler.load()
开发者ID:grodniewicz,项目名称:oship,代码行数:10,代码来源:ConflictResolution.py

示例12: state

# 需要导入模块: from cPickle import Unpickler [as 别名]
# 或者: from cPickle.Unpickler import load [as 别名]
def state(self, oid, serial, prfactory, p=''):
    p = p or self.loadSerial(oid, serial)
    file = StringIO(p)
    unpickler = Unpickler(file)
    unpickler.persistent_load = prfactory.persistent_load
    class_tuple = unpickler.load()
    state = unpickler.load()
    return state
开发者ID:OS2World,项目名称:APP-SERVER-Zope,代码行数:10,代码来源:ConflictResolution.py

示例13: state

# 需要导入模块: from cPickle import Unpickler [as 别名]
# 或者: from cPickle.Unpickler import load [as 别名]
def state(self, oid, serial, prfactory, p=""):
    p = p or self.loadSerial(oid, serial)
    p = self._crs_untransform_record_data(p)
    file = StringIO(p)
    unpickler = Unpickler(file)
    unpickler.find_global = find_global
    unpickler.persistent_load = prfactory.persistent_load
    unpickler.load()  # skip the class tuple
    return unpickler.load()
开发者ID:toutpt,项目名称:ZODB,代码行数:11,代码来源:ConflictResolution.py

示例14: tryToResolveConflict

# 需要导入模块: from cPickle import Unpickler [as 别名]
# 或者: from cPickle.Unpickler import load [as 别名]
def tryToResolveConflict(self, oid, committedSerial, oldSerial, newpickle,
                         committedData=''):
    # class_tuple, old, committed, newstate = ('',''), 0, 0, 0
    try:
        prfactory = PersistentReferenceFactory()
        newpickle = self._crs_untransform_record_data(newpickle)
        file = StringIO(newpickle)
        unpickler = Unpickler(file)
        unpickler.find_global = find_global
        unpickler.persistent_load = prfactory.persistent_load
        meta = unpickler.load()
        if isinstance(meta, tuple):
            klass = meta[0]
            newargs = meta[1] or ()
            if isinstance(klass, tuple):
                klass = find_global(*klass)
        else:
            klass = meta
            newargs = ()

        if klass in _unresolvable:
            raise ConflictError

        newstate = unpickler.load()
        inst = klass.__new__(klass, *newargs)

        try:
            resolve = inst._p_resolveConflict
        except AttributeError:
            _unresolvable[klass] = 1
            raise ConflictError

        old = state(self, oid, oldSerial, prfactory)
        committed = state(self, oid, committedSerial, prfactory, committedData)

        resolved = resolve(old, committed, newstate)

        file = StringIO()
        pickler = Pickler(file,1)
        pickler.inst_persistent_id = persistent_id
        pickler.dump(meta)
        pickler.dump(resolved)
        return self._crs_transform_record_data(file.getvalue(1))
    except (ConflictError, BadClassName):
        pass
    except:
        # If anything else went wrong, catch it here and avoid passing an
        # arbitrary exception back to the client.  The error here will mask
        # the original ConflictError.  A client can recover from a
        # ConflictError, but not necessarily from other errors.  But log
        # the error so that any problems can be fixed.
        logger.error("Unexpected error", exc_info=True)

    raise ConflictError(oid=oid, serials=(committedSerial, oldSerial),
                        data=newpickle)
开发者ID:aberke,项目名称:search_engine_2,代码行数:57,代码来源:ConflictResolution.py

示例15: IndexFile

# 需要导入模块: from cPickle import Unpickler [as 别名]
# 或者: from cPickle.Unpickler import load [as 别名]
class IndexFile(object):
    '''Open an index file for reading.

    filename - the file containing the index.

    Use the get and get_all method to return objects in the index.
    '''
    def __init__(self, filename):
        if not os.path.exists(filename):
            raise IndexFileMissingError(filename)
        self.filename = filename
        self.handle = open(self.filename)
        self.unpickler = Unpickler(self.handle)
        magic = self.handle.read(8)
        expected_magic = 'pdko\x00\x00\x00\x01'
        if magic != expected_magic:
            message = 'Magic bytes incorrect. Is %s really a pdko file?' \
                      % self.filename
            raise IndexFormatError, message
        table_offset = read_offset(self.handle)
        self.handle.seek(table_offset)
        self.key_dict = self.unpickler.load()

    def iter_addresses(self, key):
        '''Get a list of pickle addresses for the given key.'''
        try:
            list_offset = self.key_dict[key]
            self.handle.seek(list_offset)
            address_list = self.unpickler.load()
            for addresses in address_list:
                yield addresses
        except KeyError:
            return

    def get(self, key, column):
        '''The columnth object for all object groups under they key.'''
        for addresses in self.iter_addresses(key):
            offset = addresses[column]
            self.handle.seek(offset)
            yield self.unpickler.load()

    def get_all(self, key):
        '''Get the full object group count for the key.'''
        for addresses in self.iter_addresses(key):
            objects = []
            for offset in addresses:
                self.handle.seek(offset)
                objects.append(self.unpickler.load())
            yield tuple(objects)

    def count(self, key):
        '''Get the object group count for the given key.'''
        return len(list(self.iter_addresses(key)))
开发者ID:64studio,项目名称:pdk,代码行数:55,代码来源:index_file.py


注:本文中的cPickle.Unpickler.load方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。