algorithmic modeling for Rhino
hi, I am trying to pickle class object in python/grasshopper but I got the following exception..
Runtime error (ImportException): No module named __main__
I usually don't have this exception in python so I guess it's something related to python in grasshopper.
Any idea? thx
import cPickle as pickle
class DoSomething:
def __init__(self, data):
self.data = data
#dump
test = DoSomething('hello')
output = open('D:\pklTest01.pkl', 'wb')
pickle.dump(test, output)
output.close()
#load
input = open("D:\pklTest01.pkl", "rb")
data = pickle.load(input)
print data
Tags:
hi 筑梦NARUTO and thanks for your answer. ok about instance attributes but then how to pickle a class object ?
cPickle is supposed to serialize class however I still get an exception line8 when pickle.dump
any idea?
import cPickle as pickle
class DoSomething:
def __init__(self, data):
self.data = data
#dump
test = DoSomething('hello')
output = open('D:\pklTest01.pkl', 'wb')
pickle.dump(test, output)
output.close()
#load
input = open("D:\pklTest01.pkl", "rb")
a = pickle.load(input)
print a.data
HI Rad2205
You should study the python class,http://www.plethora-project.com/2011/09/12/rhino-python-tutorials/,You pickle.dump (test, output) to pickle.dump (test.date, output) can run
thanks. Yes it should run, but it doesn't in grasshopper and I don't understand why. Actually the code is working good outside grasshopper, in eclipse for exaample, but I still get an exception in gh.
Does the last piece of code I posted returns you an exception?
Appreciate your help ;)
well,.. I think you didn't understand the issue.
I don't want to dump attributes. I want to dump the class object, which usually works.. https://docs.python.org/2/library/pickle.html
import cPickle as pickle
class DoSomething:
def __init__(self, talk, eat, drink):
self.talk = talk
self.eat = eat
self.drink = drink
#dump
test = DoSomething('hello','pasta','nama biru')
output = open('D:\pklTest01.pkl', 'wb')
pickle.dump(test, output)
output.close()
#load
input = open("D:\pklTest01.pkl", "rb")
a = pickle.load(input)
print (a.talk)
print (a.eat)
print (a.drink)
>>>'hello'
>>>'pasta'
>>>'nama biru'
Welcome to
Grasshopper
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
© 2024 Created by Scott Davidson. Powered by