且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在不知道模块的情况下导入类

更新时间:2023-09-11 21:01:34

Franck PEREZ< fr ********** @ gmail.com>写道:
Franck PEREZ <fr**********@gmail.com> writes:
###########我的测试应用程序############
类Foo(对象):
#我想序列化的课程
传递

导入myMarshaller
foo = Foo()
s = myMarshaller.dumps(foo)#works fine,吐出像< object
class =" Foo" ...>
another_foo = loads(s)#fails,见下文

###### #####我的marshaller(在自己的模块中)############
def load(s):
#First,获取类名(这里是Foo ;)
klass = eval(className)#fails因为Foo不在
marshaller的命名空间中!

我怎么能告诉编组找到Foo类和我试图反序列化的任何其他类?
########### My test application ############
class Foo(object):
#The class I''d like to serialize
pass

import myMarshaller
foo = Foo()
s = myMarshaller.dumps(foo) #works fine, spits something like <object
class = "Foo"...>
another_foo = loads(s) #fails, see below

########### My marshaller (in its own module) ############
def loads(s):
#First, get class name (here "Foo")
klass = eval(className) #fails because "Foo" is not in the
marshaller''s namespace !

How could I tell the marshaller to locate the Foo class and any other
class I try to deserialize ?




如何将Foo .__ file__添加到序列化数据中?


< mike

-

Mike Meyer< mw*@mired.org> http://www.mired.org/home/mwm/

独立的WWW / Perforce / FreeBSD / Unix顾问,电子邮件以获取更多信息。



How about adding Foo.__file__ to the serialized data?

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.


Franck PEREZ< fr ******** **@gmail.com>写道:
Franck PEREZ <fr**********@gmail.com> writes:
###########我的测试应用程序############
类Foo(对象):
#我想序列化的课程
传递

导入myMarshaller
foo = Foo()
s = myMarshaller.dumps(foo)#works fine,吐出像< object
class =" Foo" ...>
another_foo = loads(s)#fails,见下文

###### #####我的marshaller(在自己的模块中)############
def load(s):
#First,获取类名(这里是Foo ;)
klass = eval(className)#fails因为Foo不在
marshaller的命名空间中!

我怎么能告诉编组找到Foo类和我试图反序列化的任何其他类?
########### My test application ############
class Foo(object):
#The class I''d like to serialize
pass

import myMarshaller
foo = Foo()
s = myMarshaller.dumps(foo) #works fine, spits something like <object
class = "Foo"...>
another_foo = loads(s) #fails, see below

########### My marshaller (in its own module) ############
def loads(s):
#First, get class name (here "Foo")
klass = eval(className) #fails because "Foo" is not in the
marshaller''s namespace !

How could I tell the marshaller to locate the Foo class and any other
class I try to deserialize ?




如何将Foo .__ file__添加到序列化数据中?


< mike

-

Mike Meyer< mw*@mired.org> http://www.mired.org/home/mwm/

独立的WWW / Perforce / FreeBSD / Unix顾问,电子邮件以获取更多信息。



How about adding Foo.__file__ to the serialized data?

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.


我考虑过它,但它会使XML文件依赖在

机器上...没有更多便携性......

2005年11月18日,Mike Meyer< mw*@mired.org>写道:
I thought about it, but it would make the XML file depend on the
machine... no more portability...

On 11/18/05, Mike Meyer <mw*@mired.org> wrote:
Franck PEREZ< fr ********** @ gmail.com>写道:
Franck PEREZ <fr**********@gmail.com> writes:
###########我的测试应用程序############
类Foo(对象):
#我想序列化的课程
传递

导入myMarshaller
foo = Foo()
s = myMarshaller.dumps(foo)#works fine,吐出像< object
class =" Foo" ...>
another_foo = loads(s)#fails,见下文

###### #####我的marshaller(在自己的模块中)############
def load(s):
#First,获取类名(这里是Foo ;)
klass = eval(className)#fails因为Foo不在
marshaller的命名空间中!

我怎么能告诉编组找到Foo类和我试图反序列化的任何其他类?
########### My test application ############
class Foo(object):
#The class I''d like to serialize
pass

import myMarshaller
foo = Foo()
s = myMarshaller.dumps(foo) #works fine, spits something like <object
class = "Foo"...>
another_foo = loads(s) #fails, see below

########### My marshaller (in its own module) ############
def loads(s):
#First, get class name (here "Foo")
klass = eval(className) #fails because "Foo" is not in the
marshaller''s namespace !

How could I tell the marshaller to locate the Foo class and any other
class I try to deserialize ?



如何将Foo .__ file__添加到序列化数据中?

< mike
-
Mike Meyer< mw*@mired.org&gt ; http://www.mired.org/home/mwm/ -
http://mail.python.org/mailman/listinfo/python-list