CiaranWelsh
CiaranWelsh

Reputation: 7681

Is it possible to store a python pickle object as a string inside a class?

I want to do some testing on a feature of my (python) program which is computationally very heavy. I could run the code, store the output in a pandas.DataFrame, pickle the df and distribute with my package so that the tests can be ran by users. However I think this goes against the principles of unittesting, namely, that a test should be independent of external sources and self contained.

An alternative idea would be if I were to store a pickle file as a string within an importable python class then dynamically write the pickle file and clean it up after the test. Is this possible to do and if so how can I do it?

Here's a small bit of code that simply write a df to pickle.pickle in the current working directory.

import pickle
import os    
import pandas

df = pandas.DataFrame([1,2,3,4,5,6])
filename = os.path.join(os.getcwd(), 'pickle.pickle')
df.to_pickle(filename)

Would it then be possible to somehow get a string version of the pickle so that I can store it in a class?

Upvotes: 2

Views: 525

Answers (1)

Netwave
Netwave

Reputation: 42716

Would it then be possible to somehow get a string version of the pickle so that I can store it in a class?

Just read the full file:

with open(filename, "rb") as f:
    data = f.read()

Then if you need you can just unpicle it with loads

unpickled = pickle.loads(data)

Upvotes: 1

Related Questions