python 模块动态引入问题

vic_space 2009-11-28 03:34:45
现在问题是,我的py代码是记录在数据库里的,对于记录A有CodeA,里写了方法FuncA,记录B有CodeB,现在想调用CodeA里的FuncA该如何实现呢?
web上说的都是要调用某路径下的py文件~!,总不能使用的时候,去动态生成临时py文件啊~~~

请指教。。。。。
...全文
178 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
luckqt 2009-11-29
  • 打赏
  • 举报
回复
mark
angel_su 2009-11-29
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 thy38 的回复:]
但是Compiled之后效率更高
[/Quote]
效率嘛,觉得要事先编译在摆在数据库里,执行阶段才压好像失去意义。文本形式若要检查修改应该也直观容易些...
thy38 2009-11-29
  • 打赏
  • 举报
回复
这是2.6.4的文档:
compile(source, filename, mode[, flags[, dont_inherit]])
Compile the source into a code or AST object. Code objects can be executed by an exec statement or evaluated by a call to eval(). source can either be a string or an AST object. Refer to the ast module documentation for information on how to work with AST objects.

The filename argument should give the file from which the code was read; pass some recognizable value if it wasn’t read from a file ('<string>' is commonly used).

The mode argument specifies what kind of code must be compiled; it can be 'exec' if source consists of a sequence of statements, 'eval' if it consists of a single expression, or 'single' if it consists of a single interactive statement (in the latter case, expression statements that evaluate to something other than None will be printed).

The optional arguments flags and dont_inherit control which future statements (see PEP 236) affect the compilation of source. If neither is present (or both are zero) the code is compiled with those future statements that are in effect in the code that is calling compile. If the flags argument is given and dont_inherit is not (or is zero) then the future statements specified by the flags argument are used in addition to those that would be used anyway. If dont_inherit is a non-zero integer then the flags argument is it – the future statements in effect around the call to compile are ignored.

Future statements are specified by bits which can be bitwise ORed together to specify multiple statements. The bitfield required to specify a given feature can be found as the compiler_flag attribute on the _Feature instance in the __future__ module.

This function raises SyntaxError if the compiled source is invalid, and TypeError if the source contains null bytes.

thy38 2009-11-29
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 angel_su 的回复:]
compile与否没关系的,文本代码串可直接exec...
[/Quote]但是Compiled之后效率更高
goosman 2009-11-29
  • 打赏
  • 举报
回复
9楼我的意思是python怎么直接从数据库中读出来一个预编译的code对象
goosman 2009-11-29
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 angel_su 的回复:]
引用 5 楼 thy38 的回复:
但是Compiled之后效率更高

效率嘛,觉得要事先编译在摆在数据库里,执行阶段才压好像失去意义。文本形式若要检查修改应该也直观容易些...
[/Quote]

编译之后放在数据库, 用的时候再拿出来?

这个怎么实现? 编译之后以什么形式存进数据库?(二进制吗?) 那么读的时候读出来的是什么? 怎么将读出来的东西直接执行?
angel_su 2009-11-28
  • 打赏
  • 举报
回复
compile与否没关系的,文本代码串可直接exec...
goosman 2009-11-28
  • 打赏
  • 举报
回复
a = compile('''
def f1():
print 'hello', d
def f2():
print 'hello world'
''', 'test', 'exec')
exec(a)
f1()


上面的代码故意使用了一个未定义变量, 来引发异常..
异常中File "test", line 3, in f1使用了我们指定的第二个参数'test'

呵呵, 我只知道这么多.

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "test", line 3, in f1
NameError: global name 'd' is not defined
goosman 2009-11-28
  • 打赏
  • 举报
回复
利用python的compile内建函数将其按照模块的方式编译了.

my_code = '''
def f1():
print 'hello'
def f2():
print 'hello world'
'''
a = compile(my_code, 'test', 'exec')

#将编译后的代码对象执行
exec(a)
#此时, my_code中的那些方法什么的就相当于做了一次模块导入. 进入了当前的名称空间.

#因此, 可以直接使用
f1()
f2()




下面是compile的官方文档:
compile(source, filename, mode[, flags[, dont_inherit]]) -> code object

第一个参数, 是要编译的代码源, 可以是模块, 语句, 表达式.
第二个参数被用于运行时错误消息(具体含义不太清楚)
第三个参数, 是编译的模式: exec, 编译一个模块, single, 编译一条语句. eval编译一个表达式.

Compile the source string (a Python module, statement or expression)
into a code object that can be executed by the exec statement or eval().
The filename will be used for run-time error messages.
The mode must be 'exec' to compile a module, 'single' to compile a
single (interactive) statement, or 'eval' to compile an expression.
The flags argument, if present, controls which future statements influence
the compilation of the code.
The dont_inherit argument, if non-zero, stops the compilation inheriting
the effects of any future statements in effect in the code calling
compile; if absent or zero these statements do influence the compilation,
in addition to any features explicitly specified.
angel_su 2009-11-28
  • 打赏
  • 举报
回复
pyrhon里,可exec直接执行字符串形式语句。

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧