在不知道模块的情况下导入类 [英] Importing a class without knowing the module

查看:61
本文介绍了在不知道模块的情况下导入类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我正在开发一个小型的XML marshaller,我正面临一个令人讨厌的问题。\\ b
问题。这里有一些示例代码:


###########我的测试应用程序############

class Foo(对象):

#我想序列化的课程

通过


import 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类和任何其他的

class我试着反序列化?我试图将我的测试应用程序'

globals()传递给marshaller,它可以工作,但它很脏喔喔......我已经

也尝试在sys.modules中找到类(这里是Foo)

在loads中。方法,但很重,不成功。


非常感谢你的帮助!

Franck

Hello,

I''m developing a small XML marshaller and I''m facing an annoying
issue. Here''s some sample code:

########### 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 ? I''ve tried to pass my test application''s
globals() to the marshaller, it works but it''s dirty IMHO... I''ve
tried also to locate the class (here "Foo") somewhere in sys.modules
in the "loads" method, but it was heavy and unsuccessful.

Thanks a lot for your help !
Franck

推荐答案

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> ; http://www.mired.org/home/mwm/ <独立的WWW / Perforce / FreeBSD / Unix顾问,请发送电子邮件以获取更多信息。
-
http://mail.python.org/mailman/listinfo/python-list



这篇关于在不知道模块的情况下导入类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆