I'm having difficulty loading a pickle in Python that was dumped in IronPython.
When I pickle something basic like "[1,2,3]" in IronPython, the pickle loads fine in Python. But, when I pickle results from the DB query below in IronPython, I get the error "No module named clr" when trying to load the pickle in Python.
What might be going wrong? (And is there a better way to share data between Python and IronPython?)
def GetData(id):
TheConnection = SqlClient.SqlConnection("server=MyServer;database=MyDB;Trusted_Connection=yes")
TheConnection.Open()
sqlcommand = "MyStoredProcedure#GetMyData '%s'" %id
MyAction = SqlClient.SqlCommand(sqlcommand, TheConnection)
MyReader = MyAction.ExecuteReader()
results = list()
while MyReader.Read():
row = {
'Type':'LolCat',
'Date':datetime.datetime(MyReader[1]),
'Location':str(MyReader[3]),
'Weight':float(MyReader[6])/float(MyReader[7]),
}
results.append(row)
TheConnection.Close()
MyReader.Close()
return results
results1 = GetData(1234)
results2 = GetData(2345)
...
picklefile= open("testpickle","w")
cPickle.dump((results1,results2),picklefile)
...
picklefile = open("testpickle","r")
p = cPickle.load(file) # this is the line that gives the error "ImportError: No module named clr"
Pickling is indeed a good general way to share data between pythons (when you can trust the data and know it hasn't been tampered with). But it's only really good for simple, builtin types. When you have objects of special types (such as whatever the results of GetData()
are), then pickling will embed the fully-qualified name of the class, and just hope that the python on the other end knows how to find that class and reinstantiate it. Needless to say, this can get very messy if you're not careful.
Try converting your results1
, results2
values to simple types, like (possibly nested) tuples, dicts, lists, etc.
The data you're pickling isn't made up of generic Python objects, but has some objects that are implementation-dependent. clr
is an IronPython-specific module, as its name implies a connection to .NET. Looking at your data, I would guess this to be something with your datetime objects.
Try using a format other than datetime for your serialized data, like UNIX epoch time:
import time
results['Date'] = time.mktime(datetime.datetime(MyReader[1]).timetuple())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With