Python pickle.Pickler() Examples
The following are 30
code examples of pickle.Pickler().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
pickle
, or try the search function
.
Example #1
Source File: pickletester.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def test_priming_pickler_memo(self): # Verify that we can set the Pickler's memo attribute. data = ["abcdefg", "abcdefg", 44] f = io.BytesIO() pickler = self.pickler_class(f) pickler.dump(data) first_pickled = f.getvalue() f = io.BytesIO() primed = self.pickler_class(f) primed.memo = pickler.memo primed.dump(data) primed_pickled = f.getvalue() self.assertNotEqual(first_pickled, primed_pickled)
Example #2
Source File: hashing.py From Splunking-Crime with GNU Affero General Public License v3.0 | 6 votes |
def save_global(self, obj, name=None, pack=struct.pack): # We have to override this method in order to deal with objects # defined interactively in IPython that are not injected in # __main__ kwargs = dict(name=name, pack=pack) if sys.version_info >= (3, 4): del kwargs['pack'] try: Pickler.save_global(self, obj, **kwargs) except pickle.PicklingError: Pickler.save_global(self, obj, **kwargs) module = getattr(obj, "__module__", None) if module == '__main__': my_name = name if my_name is None: my_name = obj.__name__ mod = sys.modules[module] if not hasattr(mod, my_name): # IPython doesn't inject the variables define # interactively in __main__ setattr(mod, my_name, obj)
Example #3
Source File: hashing.py From Splunking-Crime with GNU Affero General Public License v3.0 | 6 votes |
def save(self, obj): if isinstance(obj, (types.MethodType, type({}.pop))): # the Pickler cannot pickle instance methods; here we decompose # them into components that make them uniquely identifiable if hasattr(obj, '__func__'): func_name = obj.__func__.__name__ else: func_name = obj.__name__ inst = obj.__self__ if type(inst) == type(pickle): obj = _MyHash(func_name, inst.__name__) elif inst is None: # type(None) or type(module) do not pickle obj = _MyHash(func_name, inst) else: cls = obj.__self__.__class__ obj = _MyHash(func_name, inst, cls) Pickler.save(self, obj)
Example #4
Source File: pkl_utils.py From D-VAE with MIT License | 6 votes |
def save(self, obj): # Remove the tag.trace attribute from Variable and Apply nodes if isinstance(obj, theano.gof.utils.scratchpad): for tag in self.tag_to_remove: if hasattr(obj, tag): del obj.__dict__[tag] # Remove manually-added docstring of Elemwise ops elif (isinstance(obj, theano.tensor.Elemwise)): if '__doc__' in obj.__dict__: del obj.__dict__['__doc__'] return Pickler.save(self, obj) # Make an unpickler that tries encoding byte streams before raising TypeError. # This is useful with python 3, in order to unpickle files created with # python 2. # This code is taken from Pandas, https://github.com/pydata/pandas, # under the same 3-clause BSD license.
Example #5
Source File: hashing.py From mlens with MIT License | 6 votes |
def save_global(self, obj, name=None, pack=struct.pack): # We have to override this method in order to deal with objects # defined interactively in IPython that are not injected in # __main__ kwargs = dict(name=name, pack=pack) if sys.version_info >= (3, 4): del kwargs['pack'] try: Pickler.save_global(self, obj, **kwargs) except pickle.PicklingError: Pickler.save_global(self, obj, **kwargs) module = getattr(obj, "__module__", None) if module == '__main__': my_name = name if my_name is None: my_name = obj.__name__ mod = sys.modules[module] if not hasattr(mod, my_name): # IPython doesn't inject the variables define # interactively in __main__ setattr(mod, my_name, obj)
Example #6
Source File: hashing.py From estimators with MIT License | 6 votes |
def save(self, obj): if isinstance(obj, (types.MethodType, type({}.pop))): # the Pickler cannot pickle instance methods; here we decompose # them into components that make them uniquely identifiable if hasattr(obj, '__func__'): func_name = obj.__func__.__name__ else: func_name = obj.__name__ inst = obj.__self__ if type(inst) == type(pickle): obj = _MyHash(func_name, inst.__name__) elif inst is None: # type(None) or type(module) do not pickle obj = _MyHash(func_name, inst) else: cls = obj.__self__.__class__ obj = _MyHash(func_name, inst, cls) Pickler.save(self, obj)
Example #7
Source File: hashing.py From estimators with MIT License | 6 votes |
def save_global(self, obj, name=None, pack=struct.pack): # We have to override this method in order to deal with objects # defined interactively in IPython that are not injected in # __main__ kwargs = dict(name=name, pack=pack) if sys.version_info >= (3, 4): del kwargs['pack'] try: Pickler.save_global(self, obj, **kwargs) except pickle.PicklingError: Pickler.save_global(self, obj, **kwargs) module = getattr(obj, "__module__", None) if module == '__main__': my_name = name if my_name is None: my_name = obj.__name__ mod = sys.modules[module] if not hasattr(mod, my_name): # IPython doesn't inject the variables define # interactively in __main__ setattr(mod, my_name, obj)
Example #8
Source File: hashing.py From mlens with MIT License | 6 votes |
def save(self, obj): if isinstance(obj, (types.MethodType, type({}.pop))): # the Pickler cannot pickle instance methods; here we decompose # them into components that make them uniquely identifiable if hasattr(obj, '__func__'): func_name = obj.__func__.__name__ else: func_name = obj.__name__ inst = obj.__self__ if type(inst) == type(pickle): obj = _MyHash(func_name, inst.__name__) elif inst is None: # type(None) or type(module) do not pickle obj = _MyHash(func_name, inst) else: cls = obj.__self__.__class__ obj = _MyHash(func_name, inst, cls) Pickler.save(self, obj)
Example #9
Source File: hashing.py From django-estimators with MIT License | 6 votes |
def save_global(self, obj, name=None, pack=struct.pack): # We have to override this method in order to deal with objects # defined interactively in IPython that are not injected in # __main__ kwargs = dict(name=name, pack=pack) if sys.version_info >= (3, 4): del kwargs['pack'] try: Pickler.save_global(self, obj, **kwargs) except pickle.PicklingError: Pickler.save_global(self, obj, **kwargs) module = getattr(obj, "__module__", None) if module == '__main__': my_name = name if my_name is None: my_name = obj.__name__ mod = sys.modules[module] if not hasattr(mod, my_name): # IPython doesn't inject the variables define # interactively in __main__ setattr(mod, my_name, obj)
Example #10
Source File: hashing.py From django-estimators with MIT License | 6 votes |
def save(self, obj): if isinstance(obj, (types.MethodType, type({}.pop))): # the Pickler cannot pickle instance methods; here we decompose # them into components that make them uniquely identifiable if hasattr(obj, '__func__'): func_name = obj.__func__.__name__ else: func_name = obj.__name__ inst = obj.__self__ if type(inst) == type(pickle): obj = _MyHash(func_name, inst.__name__) elif inst is None: # type(None) or type(module) do not pickle obj = _MyHash(func_name, inst) else: cls = obj.__self__.__class__ obj = _MyHash(func_name, inst, cls) Pickler.save(self, obj)
Example #11
Source File: memcacheclient.py From ccs-calendarserver with Apache License 2.0 | 6 votes |
def getClient( cls, servers, debug=0, pickleProtocol=0, pickler=pickle.Pickler, unpickler=pickle.Unpickler, pload=None, pid=None ): if cls.allowTestCache: return TestClient( servers, debug=debug, pickleProtocol=pickleProtocol, pickler=pickler, unpickler=unpickler, pload=pload, pid=pid) elif config.Memcached.Pools.Default.ClientEnabled: return Client( servers, debug=debug, pickleProtocol=pickleProtocol, pickler=pickler, unpickler=unpickler, pload=pload, pid=pid) else: return None
Example #12
Source File: pickletester.py From ironpython3 with Apache License 2.0 | 6 votes |
def test_priming_pickler_memo(self): # Verify that we can set the Pickler's memo attribute. data = ["abcdefg", "abcdefg", 44] f = io.BytesIO() pickler = self.pickler_class(f) pickler.dump(data) first_pickled = f.getvalue() f = io.BytesIO() primed = self.pickler_class(f) primed.memo = pickler.memo primed.dump(data) primed_pickled = f.getvalue() self.assertNotEqual(first_pickled, primed_pickled)
Example #13
Source File: pkl_utils.py From attention-lvcsr with MIT License | 6 votes |
def save(self, obj): # Remove the tag.trace attribute from Variable and Apply nodes if isinstance(obj, theano.gof.utils.scratchpad): for tag in self.tag_to_remove: if hasattr(obj, tag): del obj.__dict__[tag] # Remove manually-added docstring of Elemwise ops elif (isinstance(obj, theano.tensor.Elemwise)): if '__doc__' in obj.__dict__: del obj.__dict__['__doc__'] return Pickler.save(self, obj) # Make an unpickler that tries encoding byte streams before raising TypeError. # This is useful with python 3, in order to unpickle files created with # python 2. # This code is taken from Pandas, https://github.com/pydata/pandas, # under the same 3-clause BSD license.
Example #14
Source File: resource_snapshot.py From citest with Apache License 2.0 | 5 votes |
def process_commands(self, options): """Run all the commands.""" foreach_api = self.__investigator.foreach_api if options.catalog: foreach_api(self.do_command_print_catalog) if options.print_api_spec: foreach_api(self.do_command_print_api_spec) if options.list: foreach_api(self.do_command_collect_api) if options.delete_list: foreach_api(self.do_command_delete_list) if options.output_path and self.__aggregated_listings: with open(options.output_path, 'wb+') as sink: pickler = pickle.Pickler(sink) pickler.dump(self.__aggregated_listings) before = None after = None num_diffs = 0 if options.compare: with open(options.compare[0], 'rb') as source: unpickler = pickle.Unpickler(source) before = unpickler.load() with open(options.compare[1], 'rb') as source: unpickler = pickle.Unpickler(source) after = unpickler.load() num_diffs = self.__do_compare_snapshots(options, before, after) if options.delete_added: for api in set(before.keys()).intersection(after.keys()): self.__actuator.delete_added( api, before[api], after[api], self.__scanner.investigator.get_api_resource_filter(api)) return num_diffs
Example #15
Source File: database_module.py From BlenderAndMBDyn with GNU General Public License v3.0 | 5 votes |
def pickle(self): if not self.scene: self.scene = bpy.context.scene bpy.context.scene.mbdyn_name = bpy.context.scene.name for obj in bpy.context.scene.objects: obj.mbdyn_name = obj.name with BytesIO() as f: p = Pickler(f) p.dump(self) self.scene.pickled_database = b64encode(f.getvalue()).decode() del p gc.collect()
Example #16
Source File: hashing.py From django-estimators with MIT License | 5 votes |
def _batch_setitems(self, items): # forces order of keys in dict to ensure consistent hash. try: # Trying first to compare dict assuming the type of keys is # consistent and orderable. # This fails on python 3 when keys are unorderable # but we keep it in a try as it's faster. Pickler._batch_setitems(self, iter(sorted(items))) except TypeError: # If keys are unorderable, sorting them using their hash. This is # slower but works in any case. Pickler._batch_setitems(self, iter(sorted((hash(k), v) for k, v in items)))
Example #17
Source File: memcacheclient.py From ccs-calendarserver with Apache License 2.0 | 5 votes |
def __init__(self, servers, debug=0, pickleProtocol=0, pickler=pickle.Pickler, unpickler=pickle.Unpickler, pload=None, pid=None): """ Create a new Client object with the given list of servers. @param servers: C{servers} is passed to L{set_servers}. @param debug: whether to display error messages when a server can't be contacted. @param pickleProtocol: number to mandate protocol used by (c)Pickle. @param pickler: optional override of default Pickler to allow subclassing. @param unpickler: optional override of default Unpickler to allow subclassing. @param pload: optional persistent_load function to call on pickle loading. Useful for cPickle since subclassing isn't allowed. @param pid: optional persistent_id function to call on pickle storing. Useful for cPickle since subclassing isn't allowed. """ local.__init__(self) self.set_servers(servers) self.debug = debug self.stats = {} # Allow users to modify pickling/unpickling behavior self.pickleProtocol = pickleProtocol self.pickler = pickler self.unpickler = unpickler self.persistent_load = pload self.persistent_id = pid # figure out the pickler style file = StringIO() try: pickler = self.pickler(file, protocol=self.pickleProtocol) self.picklerIsKeyword = True except TypeError: self.picklerIsKeyword = False
Example #18
Source File: helpers.py From ImageSimilarityUsingCntk with MIT License | 5 votes |
def saveToPickle(outputFile, data): p = pickle.Pickler(open(outputFile,"wb")) p.fast = True p.dump(data)
Example #19
Source File: hashing.py From Splunking-Crime with GNU Affero General Public License v3.0 | 5 votes |
def memoize(self, obj): # We want hashing to be sensitive to value instead of reference. # For example we want ['aa', 'aa'] and ['aa', 'aaZ'[:2]] # to hash to the same value and that's why we disable memoization # for strings if isinstance(obj, _bytes_or_unicode): return Pickler.memoize(self, obj) # The dispatch table of the pickler is not accessible in Python # 3, as these lines are only bugware for IPython, we skip them.
Example #20
Source File: test_pickle.py From gcblue with BSD 3-Clause "New" or "Revised" License | 5 votes |
def dumps(self, arg, proto=0, fast=0): f = StringIO() p = pickle.Pickler(f, proto) if fast: p.fast = fast p.dump(arg) f.seek(0) return f.read()
Example #21
Source File: pickletester.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_clear_pickler_memo(self): # To test whether clear_memo() has any effect, we pickle an object, # then pickle it again without clearing the memo; the two serialized # forms should be different. If we clear_memo() and then pickle the # object again, the third serialized form should be identical to the # first one we obtained. data = ["abcdefg", "abcdefg", 44] f = io.BytesIO() pickler = self.pickler_class(f) pickler.dump(data) first_pickled = f.getvalue() # Reset BytesIO object. f.seek(0) f.truncate() pickler.dump(data) second_pickled = f.getvalue() # Reset the Pickler and BytesIO objects. pickler.clear_memo() f.seek(0) f.truncate() pickler.dump(data) third_pickled = f.getvalue() self.assertNotEqual(first_pickled, second_pickled) self.assertEqual(first_pickled, third_pickled)
Example #22
Source File: pickletester.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_bad_init(self): # Test issue3664 (pickle can segfault from a badly initialized Pickler). # Override initialization without calling __init__() of the superclass. class BadPickler(pickle.Pickler): def __init__(self): pass class BadUnpickler(pickle.Unpickler): def __init__(self): pass self.assertRaises(pickle.PicklingError, BadPickler().dump, 0) self.assertRaises(pickle.UnpicklingError, BadUnpickler().load)
Example #23
Source File: pickletester.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_callapi(self): f = io.BytesIO() # With and without keyword arguments pickle.dump(123, f, -1) pickle.dump(123, file=f, protocol=-1) pickle.dumps(123, -1) pickle.dumps(123, protocol=-1) pickle.Pickler(f, -1) pickle.Pickler(f, protocol=-1)
Example #24
Source File: shelve.py From ironpython3 with Apache License 2.0 | 5 votes |
def __setitem__(self, key, value): if self.writeback: self.cache[key] = value f = BytesIO() p = Pickler(f, self._protocol) p.dump(value) self.dict[key.encode(self.keyencoding)] = f.getvalue()
Example #25
Source File: data_manager.py From automl-phase-2 with MIT License | 5 votes |
def loadLabel(self, filename, verbose=True): ''' Get the solution/truth values''' if verbose: print("========= Reading " + filename) start = time.time() if self.use_pickle and os.path.exists(os.path.join(self.tmp_dir, os.path.basename(filename) + ".pickle")): with open(os.path.join(self.tmp_dir, os.path.basename(filename) + ".pickle"), "r") as pickle_file: vprint(verbose, "Loading pickle file : " + os.path.join(self.tmp_dir, os.path.basename(filename) + ".pickle")) return pickle.load(pickle_file) if 'task' not in self.info.keys(): self.getTypeProblem(filename) # IG: Here change to accommodate the new multiclass label format if self.info['task'] == 'multilabel.classification': label = data_io.data(filename) elif self.info['task'] == 'multiclass.classification': label = data_converter.convert_to_num(data_io.data(filename)) else: label = np.ravel(data_io.data(filename)) # get a column vector # label = np.array([np.ravel(data_io.data(filename))]).transpose() # get a column vector if self.use_pickle: with open(os.path.join(self.tmp_dir, os.path.basename(filename) + ".pickle"), "wb") as pickle_file: vprint(verbose, "Saving pickle file : " + os.path.join(self.tmp_dir, os.path.basename(filename) + ".pickle")) p = pickle.Pickler(pickle_file) p.fast = True p.dump(label) end = time.time() if verbose: print( "[+] Success in %5.2f sec" % (end - start)) return label
Example #26
Source File: data_manager.py From automl-phase-2 with MIT License | 5 votes |
def loadData(self, filename, verbose=True, replace_missing=True): ''' Get the data from a text file in one of 3 formats: matrix, sparse, binary_sparse''' if verbose: print("========= Reading " + filename) start = time.time() if self.use_pickle and os.path.exists(os.path.join(self.tmp_dir, os.path.basename(filename) + ".pickle")): with open(os.path.join(self.tmp_dir, os.path.basename(filename) + ".pickle"), "r") as pickle_file: vprint(verbose, "Loading pickle file : " + os.path.join(self.tmp_dir, os.path.basename(filename) + ".pickle")) return pickle.load(pickle_file) if not os.path.exists(filename): return None if 'format' not in self.info.keys(): self.getFormatData(filename) if 'feat_num' not in self.info.keys(): self.getNbrFeatures(filename) data_func = {'dense': data_io.data, 'sparse': data_io.data_sparse, 'sparse_binary': data_io.data_binary_sparse} data = data_func[self.info['format']](filename, self.info['feat_num']) # INPORTANT: when we replace missing values we double the number of variables if self.info['format'] == 'dense' and replace_missing and np.any(map(np.isnan, data)): vprint(verbose, "Replace missing values by 0 (slow, sorry)") data = data_converter.replace_missing(data) if self.use_pickle: with open(os.path.join(self.tmp_dir, os.path.basename(filename) + ".pickle"), "wb") as pickle_file: vprint(verbose, "Saving pickle file : " + os.path.join(self.tmp_dir, os.path.basename(filename) + ".pickle")) p = pickle.Pickler(pickle_file) p.fast = True p.dump(data) end = time.time() if verbose: print( "[+] Success in %5.2f sec" % (end - start)) return data
Example #27
Source File: serial_pickle.py From krnnt with GNU Lesser General Public License v3.0 | 5 votes |
def __init__(self, file: BinaryIO, mode=3): # don't work with protocol 4 self.file = file self.p = pickle.Pickler(file, mode)
Example #28
Source File: shelve.py From Imogen with MIT License | 5 votes |
def __setitem__(self, key, value): if self.writeback: self.cache[key] = value f = BytesIO() p = Pickler(f, self._protocol) p.dump(value) self.dict[key.encode(self.keyencoding)] = f.getvalue()
Example #29
Source File: hashing.py From Splunking-Crime with GNU Affero General Public License v3.0 | 5 votes |
def _batch_setitems(self, items): # forces order of keys in dict to ensure consistent hash. try: # Trying first to compare dict assuming the type of keys is # consistent and orderable. # This fails on python 3 when keys are unorderable # but we keep it in a try as it's faster. Pickler._batch_setitems(self, iter(sorted(items))) except TypeError: # If keys are unorderable, sorting them using their hash. This is # slower but works in any case. Pickler._batch_setitems(self, iter(sorted((hash(k), v) for k, v in items)))
Example #30
Source File: serialization_utils.py From zipline-chinese with Apache License 2.0 | 5 votes |
def dumps_with_persistent_ids(obj, protocol=None): """ Performs a pickle dumps on the given object, substituting all references to a TradingEnvironment or AssetFinder with tokenized representations. All arguments are passed to pickle.Pickler and are described therein. """ file = BytesIO() pickler = pickle.Pickler(file, protocol) pickler.persistent_id = _persistent_id pickler.dump(obj) return file.getvalue()