导入模块而不执行它? [英] Import a module without executing it?

查看:65
本文介绍了导入模块而不执行它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种很好的方法可以在不执行内容的情况下导入python文件?


我正在尝试一些基于relfection的东西,我希望能够导入一个

模块动态检查它的内容(定义类广告功能)

但没有执行任何内容。


For例如:

----------------------

def test(var):

打印var

#main

test(3)

-------------- --------


我希望能够导入这个模块,所以我可以看到啊哈,这个模块

定义了一个函数名为''test''',但我不想在导入过程中执行

底部的代码。


谢谢


保重,

Jay

解决方案




你可以解析模型文件。我最喜欢


***

f = open(''ModuleYouWantToExamine.py'',''r'')

for i in f:

if i.find(''def'')> -1:

print''找到一个函数!:''+ i.replace(''def'','''')


f .close()

***


你需要建立这个以进行更全面的检查。

当然,这里的一个大师应该能够给你指导

关于用解释器实际解析文件(不执行)

并建立一个包含所有不同类型

结构的字典或其他内容。那不是我:)


我想能够导入这个模块,所以我可以看到啊哈,这个模块定义了一个函数叫''test''',但我不想在导入过程中执行
底部的代码。

谢谢

保重,Jay






你想要使用编译器包。 compiler.parseFile将

返回一个你可以检查的AST(这不是'反射'',

顺便说一句。)


/ arg

2004年12月7日晚上10:56,Caleb Hattingh写道:



你可以解析模型文件。在我的头顶上

***
f =打开(''ModuleYouWantToExamine.py'',''r'')
for f in f: print''找到一个函数!:''+ i.replace(''def'','''')

f.close()
***

你必须建立这个以进行更全面的检查。当然,这里的一位大师应该能够给你关于用解释器实际解析文件的指导(不是执行)和建立一个dict或者之类的东西。所有不同类型的结构。那不是我:)


我想能够导入这个模块,所以我可以看到啊哈,这个
模块定义了一个函数叫''test''',但我不想在导入过程中执行底部的代码

谢谢

保重,周杰伦



http://mail.python.org/mailman/listinfo/python-list




< blockquote> Andy


thx。我有一个名为''tktest.py'的文件,我做了:


''>>> a = compiler.parseFile(''tktest.py'')


和a看起来像这样:


***

Stmt([导入([('''Tkinter'',无)]),功能(无, ''add_rows'',[''w'',

''titles'',''rows''],[],0,None,Stmt([Discard(CallFunc(Getattr(名称(''w''),

''configure''),[Keyword(''state'',Const(''normal''))],None,None)),

For(AssName(''r'',''OP_ASSIGN''),Name(''rows''),

Stmt([For(AssTuple([ AssName(''t'',''OP_ASSIGN''),AssName(''v'',

''OP_ASSIGN'')]),CallFunc(名称('''zip'') ,[名称(''标题''),名称(''r'')],无,

无),Stmt([丢弃(CallFunc)(Getattr(名称(''w'') ),''insert''),[Const(''end''),

Mod((Const(''%s:\ t%s \'''),元组([Name(''t''),Name(''v'')])))],None,None))]),

无),Discard(CallFunc(Getattr(姓名(''w''),' insert''),[Const(''end''),

Const(''\ n'')],None,None))]),None),Discard(CallFunc( Getattr(名称(''w''),

''configure''),[Keyword(''state'',Const(''disabled''))],None,None) )]),

分配([AssName(''app'',''OP_ASSIGN'')],CallFunc(Getattr(名称(''Tkinter''),

''Tk''),[],无,无)),分配([AssName(''t'',''OP_ASSIGN'')],

CallFunc(Getattr(姓名(''Tkinter''),''文字''),[姓名(''app''),关键字(''州'',

Const(''禁用'') ),无,无)),丢弃(CallFunc(Getattr(姓名('t''),

''包''),[],无,无)),分配( [AssName(''info'',''OP_ASSIGN'')],

List([List([Const(''Ali''),Const(18)]),List([ Const(''Zainab''),Const(16)]),

List([Const(''Khalid''),Const(18)])])),Discard(CallFunc(名称( 'add_rows''),

[Name(''t''),List([Const(''Name''),Const(''Age'')]),Name(' 'info'')],无,

无)),丢弃(CallFunc(Getattr(名称(''app''),''mainloop''),[],无,

无))])

***


相当令人印象深刻:)


你知道更多的电池可以进一步处理这些东西吗?

兴趣缘故(也许是OP)?


thx再次

Caleb

2004年12月7日星期二15:57:16 -0500,Andy Gross< an ** @ andygross.org>写道:


你想要使用编译器包。 compiler.parseFile将返回一个你可以检查的AST(这不是'反射'',
btw)。

/ arg


Is there a good way to import python files without executing their content?

I''m trying some relfection based stuff and I want to be able to import a
module dynamically to check it''s contents (class ad functions defined)
but without having any of the content executed.

For example:
----------------------
def test(var):
print var
#main
test(3)
----------------------

I want to be able to import this module so I can see "ah ha, this module
defines a function called ''test''", but I don''t want the code at the
bottom executed during the import.

Thanks

Take care,
Jay

解决方案

Hi

You could just parse the model file. Off the top of my head

***
f = open(''ModuleYouWantToExamine.py'',''r'')
for i in f:
if i.find(''def '') > -1:
print ''Found a function!: ''+i.replace(''def '','''')

f.close()
***

You would have to build this up for a more complete examination. Of
course, one of the guru''s around here should be able to give you guidance
regarding actually parsing the file with the interpreter (not executing)
and building a dict or something with all the different types of
constructs. That''s not me :)


I want to be able to import this module so I can see "ah ha, this module
defines a function called ''test''", but I don''t want the code at the
bottom executed during the import.

Thanks

Take care,
Jay





You''ll want to use the "compiler" package. compiler.parseFile will
return an AST that you can inspect (which is not really ''reflection'',
btw).

/arg
On Dec 7, 2004, at 10:56 PM, Caleb Hattingh wrote:

Hi

You could just parse the model file. Off the top of my head

***
f = open(''ModuleYouWantToExamine.py'',''r'')
for i in f:
if i.find(''def '') > -1:
print ''Found a function!: ''+i.replace(''def '','''')

f.close()
***

You would have to build this up for a more complete examination. Of
course, one of the guru''s around here should be able to give you
guidance regarding actually parsing the file with the interpreter (not
executing) and building a dict or something with all the different
types of constructs. That''s not me :)


I want to be able to import this module so I can see "ah ha, this
module defines a function called ''test''", but I don''t want the code
at the bottom executed during the import.

Thanks

Take care,
Jay



--
http://mail.python.org/mailman/listinfo/python-list




Andy

thx for that. I had a file called ''tktest.py'' lying around, and I did:

''>>> a = compiler.parseFile(''tktest.py'')

And "a" looks something like this:

***
Stmt([Import([(''Tkinter'', None)]), Function(None, ''add_rows'', [''w'',
''titles'', ''rows''], [], 0, None, Stmt([Discard(CallFunc(Getattr(Name(''w''),
''configure''), [Keyword(''state'', Const(''normal''))], None, None)),
For(AssName(''r'', ''OP_ASSIGN''), Name(''rows''),
Stmt([For(AssTuple([AssName(''t'', ''OP_ASSIGN''), AssName(''v'',
''OP_ASSIGN'')]), CallFunc(Name(''zip''), [Name(''titles''), Name(''r'')], None,
None), Stmt([Discard(CallFunc(Getattr(Name(''w''), ''insert''), [Const(''end''),
Mod((Const(''%s:\t%s\n''), Tuple([Name(''t''), Name(''v'')])))], None, None))]),
None), Discard(CallFunc(Getattr(Name(''w''), ''insert''), [Const(''end''),
Const(''\n'')], None, None))]), None), Discard(CallFunc(Getattr(Name(''w''),
''configure''), [Keyword(''state'', Const(''disabled''))], None, None))])),
Assign([AssName(''app'', ''OP_ASSIGN'')], CallFunc(Getattr(Name(''Tkinter''),
''Tk''), [], None, None)), Assign([AssName(''t'', ''OP_ASSIGN'')],
CallFunc(Getattr(Name(''Tkinter''), ''Text''), [Name(''app''), Keyword(''state'',
Const(''disabled''))], None, None)), Discard(CallFunc(Getattr(Name(''t''),
''pack''), [], None, None)), Assign([AssName(''info'', ''OP_ASSIGN'')],
List([List([Const(''Ali''), Const(18)]), List([Const(''Zainab''), Const(16)]),
List([Const(''Khalid''), Const(18)])])), Discard(CallFunc(Name(''add_rows''),
[Name(''t''), List([Const(''Name''), Const(''Age'')]), Name(''info'')], None,
None)), Discard(CallFunc(Getattr(Name(''app''), ''mainloop''), [], None,
None))])
***

Pretty impressive :)

Do you know of more batteries that can process this stuff further, for
interest sake (and maybe the OP)?

thx again
Caleb
On Tue, 7 Dec 2004 15:57:16 -0500, Andy Gross <an**@andygross.org> wrote:


You''ll want to use the "compiler" package. compiler.parseFile will
return an AST that you can inspect (which is not really ''reflection'',
btw).

/arg



这篇关于导入模块而不执行它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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