Howdy,
In my app I need to exec user text that defines a function. I want this
function to unpickle an object. Pickle breaks because it is looking for
the object definition that isn't in the calling namespace.
I have mocked up a simple example that shows the problem. Run this
first code (from create_pickle.py) to create the pickle.
create_pickle.py: (run this first)
#############################################
import cPickle
# the pickle file name
file_name = 'd:\\temp\\test1.pickle'
# define a class
class Tree(object):
pass
def main():
# instantiate
t = Tree()
# create the sweet pickle
fp = open(file_name, 'wb')
cPickle.dump(t, fp)
fp.close()
# try to unpickle directly
fp = open(file_name, 'rb')
result = cPickle.load(fp)
fp.close()
print "unpickling directly works just fine, result = ", result
if __name__=='__main__':
main()
#############################################
run this second:
exec_pickle.py
#############################################
# this file shows a problem with sweet pickle in an exec statement
# the pickle file name
file_name = 'd:\\temp\\test1.pickle'
# code to be turned into a function
code_text = '''
def include():
print "this works!"
'''
# a function for creating functions
def create_fun(code_text):
clean_dict = {}
exec code_text in clean_dict
return clean_dict['include']
# include_fun is a bona fide function
include_fun = create_fun(code_text)
# this works
include_fun()
# now try to load the pickle in an exec statement
code_text = '''
def include(file_name):
print "processing file_name: ", file_name
import cPickle
fp = open(file_name, "rb")
result = cPickle.load(fp)
fp.close()
print "result = ", result
'''
# create the new include_fun
include_fun = create_fun(code_text)
# run it
include_fun(file_name)
#############################################
Can anyone enlighten me what I need to do to exec_pickle.py
to get this to work?
thanks,
Danny 2 1601
On Jul 23, 6:01*pm, Danny Shevitz <shev...@lanl.govwrote:
Howdy,
In my app I need to exec user text that defines a function. I want this
function to unpickle an object. Pickle breaks because it is looking for
the object definition that isn't in the calling namespace.
I have mocked up a simple example that shows the problem. Run this
first code (from create_pickle.py) to create the pickle.
create_pickle.py: (run this first)
#############################################
import cPickle
# the pickle file name
file_name = 'd:\\temp\\test1.pickle'
# define a class
class Tree(object):
* * * * pass
def main():
* * * * # instantiate *
* * * * t = Tree()
* * * * # create the sweet pickle
* * * * fp = open(file_name, 'wb')
* * * * cPickle.dump(t, fp)
* * * * fp.close()
* * * * # try to unpickle directly
* * * * fp = open(file_name, 'rb')
* * * * result = cPickle.load(fp)
* * * * fp.close()
* * * * print "unpickling directly works just fine, result = ",result
if __name__=='__main__':
* * * * main()
#############################################
run this second:
exec_pickle.py
#############################################
# this file shows a problem with sweet pickle in an exec statement
# the pickle file name
file_name = 'd:\\temp\\test1.pickle'
# code to be turned into a function
code_text = '''
def include():
* print "this works!"
'''
# a function for creating functions
def create_fun(code_text):
* * * * clean_dict = {}
* * * * exec code_text in clean_dict
* * * * return clean_dict['include']
# include_fun is a bona fide function
include_fun = create_fun(code_text)
# this works
include_fun()
# now try to load the pickle in an exec statement
code_text = '''
def include(file_name):
* print "processing file_name: ", file_name
* import cPickle
* fp = open(file_name, "rb")
* result = cPickle.load(fp)
* fp.close()
* print "result = ", result
'''
# create the new include_fun
include_fun = create_fun(code_text)
# run it
include_fun(file_name)
#############################################
Can anyone enlighten me what I need to do to exec_pickle.py
to get this to work?
thanks,
Danny
Hi,
It works if you paste
# define a class
class Tree(object):
pass
into exec_pickle.py. There are inherent dilemmas in pickling
instances of non-primitive types, such as definition location, and
change in definition across versions. How does 'class Tree' fit in to
your design module?
Danny Shevitz wrote:
Howdy,
In my app I need to exec user text that defines a function. I want this
function to unpickle an object. Pickle breaks because it is looking for
the object definition that isn't in the calling namespace.
I have mocked up a simple example that shows the problem. Run this
first code (from create_pickle.py) to create the pickle.
create_pickle.py: (run this first)
#############################################
import cPickle
# the pickle file name
file_name = 'd:\\temp\\test1.pickle'
# define a class
class Tree(object):
pass
def main():
# instantiate
t = Tree()
# create the sweet pickle
fp = open(file_name, 'wb')
cPickle.dump(t, fp)
fp.close()
# try to unpickle directly
fp = open(file_name, 'rb')
result = cPickle.load(fp)
fp.close()
print "unpickling directly works just fine, result = ", result
if __name__=='__main__':
main()
#############################################
run this second:
exec_pickle.py
#############################################
# this file shows a problem with sweet pickle in an exec statement
# the pickle file name
file_name = 'd:\\temp\\test1.pickle'
# code to be turned into a function
code_text = '''
def include():
print "this works!"
'''
# a function for creating functions
def create_fun(code_text):
clean_dict = {}
exec code_text in clean_dict
return clean_dict['include']
# include_fun is a bona fide function
include_fun = create_fun(code_text)
# this works
include_fun()
# now try to load the pickle in an exec statement
code_text = '''
def include(file_name):
print "processing file_name: ", file_name
import cPickle
fp = open(file_name, "rb")
result = cPickle.load(fp)
fp.close()
print "result = ", result
'''
# create the new include_fun
include_fun = create_fun(code_text)
# run it
include_fun(file_name)
#############################################
Can anyone enlighten me what I need to do to exec_pickle.py
to get this to work?
Pickling saves the name of the module and the class (and of course the
instance data). Because you put the class in your main script the module
name is __main__, and when you unpickle later pickle imports __main__ which
unfortunately is now a different script that doesn't contain the/a Tree
class. To solve that problem put the Tree class in a separate module, say
tree.py, that can be imported by both create_pickle.py and exec_pickle.py.
You only need an explicit
from tree import Tree
in create_pickle.
Peter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Andr? Roberge |
last post by:
I have the following two files:
#--testexec.py--
def exec_code(co):
try:
exec co
except:
print "error"
#-- test.py--
|
by: Oracle 9Ri2 AS 2.1 IA 64 |
last post by:
Hello,
I am working with Oracle 9Ri2 version on Linux AS 2.1 for IA64.
I have a problem when i try to lanch the following program :
EXEC SQL PREPARE MyStmt FROM
SELECT CHAMP1 FROM TAB1...
|
by: Vance Kessler |
last post by:
I am sure this is a configuration or permissions problem, but I cannot
figure out what it might be.
I have 2 SQL 2000 database servers: one is a linked Windows 2003 based
server using a...
|
by: Steve B |
last post by:
I have a system that has been working fine for months, until this week.
This is the error message that I am now getting :
A DB2 problem occurred.
Sqlcode: -712 Sqlstate: 57011 , Sqlerrd.1: -140...
|
by: TPJ |
last post by:
I have the following code:
-----------------------------------
def f():
def g():
a = 'a' # marked line 1
exec 'a = "b"' in globals(), locals()
print "g: a =", a
|
by: Thomas Heller |
last post by:
I'm using code.Interactive console but it doesn't work correctly
with non-ascii characters. I think it boils down to this problem:
Python 2.4.3 (#69, Mar 29 2006, 17:35:34) on win32
Type...
|
by: Gagan Sindhu Dewangan |
last post by:
Below is the following procedure where I am facing the problem, the work of the below procedure is to update the database.
query result:sp_helptext bProjectUpdate
Text ...
|
by: chets |
last post by:
Hi All,
I am facing problem in executing one dynamic query in PRO *C program
on linux. I want to update table
mytable by data MADURAI for a column mycolumn1 where primary key is
myPK.I want to...
|
by: comp.lang.tcl |
last post by:
My TCL proc, XML_GET_ALL_ELEMENT_ATTRS, is supposed to convert an XML
file into a TCL list as follows:
attr1 {val1} attr2 {val2} ... attrN {valN}
This is the TCL code that does this:
set...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
| |