Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is `pickle.dump(d, f)` equivalent to `f.write(pickle.dumps(d))`?

Tags:

python

pickle

Given an arbitrary picklable Python data structure data, is

with open('a', 'bw') as f:
    f.write(pickle.dumps(data))

equivalent to

with open('a', 'bw') as f:
    pickle.dump(data, f)

i.e. can I assume this equivalence when writing code? Could you describe how you reached to this conclusion?

The use-case is to separate serialization from writing, since I may want to write pickle to a non-disk location.

like image 731
Jorge Leitao Avatar asked Aug 11 '19 06:08

Jorge Leitao


People also ask

What is the difference between pickle dump and dumps?

The first two methods are used during the pickling process, and the other two are used during unpickling. The only difference between dump() and dumps() is that the first creates a file containing the serialization result, whereas the second returns a string.

Does pickle dump append or overwrite?

Usually, you'd open the file in append ( "ab" ) mode to add data at the end. However, Pickle doesn't support appending, so you'll have to save your data to a new file (come up with a different file name -- ask the user or use a command-line parameter such as -o test. txt ?)

What is pickle dump?

Python Pickle dump dump() function to store the object data to the file. pickle. dump() function takes 3 arguments. The first argument is the object that you want to store. The second argument is the file object you get by opening the desired file in write-binary (wb) mode.

Why pickle dump is used in Python?

Python pickle module is used for serializing and de-serializing a Python object structure. Any object in Python can be pickled so that it can be saved on disk. What pickle does is that it “serializes” the object first before writing it to file. Pickling is a way to convert a python object (list, dict, etc.)


2 Answers

Yes, they are equivalent. Looking at the source code of pickle.py:

def _dump(obj, file, protocol=None, *, fix_imports=True):
    _Pickler(file, protocol, fix_imports=fix_imports).dump(obj)

def _dumps(obj, protocol=None, *, fix_imports=True):
    f = io.BytesIO()
    _Pickler(f, protocol, fix_imports=fix_imports).dump(obj)
    res = f.getvalue()
    assert isinstance(res, bytes_types)
    return res

dumps does the exact same thing as dump, just with an io.BytesIO object instead of a file object. It calls the internal _Pickler().dump() in the same way and simply returns the contents of the io.BytesIO object. Therefore, all f.write(pickle.dumps(data)) does is first forward the result to an io.BytesIO object then to the actual file instead of writing to the file directly.

like image 174
iz_ Avatar answered Oct 13 '22 08:10

iz_


They are not equivalent if an exception occurs during pickling. f.write(pickle.dumps(data)) will not write anything to the file. Whereas pickle.dump(data, f) will end up with a snapped or partial pickle.

like image 43
Dan D. Avatar answered Oct 13 '22 09:10

Dan D.