执行动态语句

执行字符串中的代码
http://www.cnblogs.com/fanweibin/p/5418817.html

#!usr/bin/env python
#coding:utf-8
  
namespace = {'name':'xs','data':[18,73,84]}
  
code =  '''def hellocute():return  "name %s ,age %d" %(name,data[0],) '''
  
func = compile(code, '<string>', "exec")
  
exec func in namespace
  
result = namespace['hellocute']()
  
print result

动态加载模块

根据配置信息加载模块,

import config as conf
from importlib import import_module as im

import tornado.web as web
import os

urlpatterns = []

for app in conf.INSTALLED_APPS:
    cmd = app + '.urls' 
    urlpatterns = urlpatterns + getattr(im(cmd), 'urlpatterns')

相关文章:

  • 2021-11-04
  • 2021-12-07
  • 2021-06-14
  • 2022-01-24
  • 2022-01-06
  • 2021-06-27
  • 2021-05-24
  • 2021-08-21
猜你喜欢
  • 2021-06-20
  • 2021-06-24
  • 2021-08-22
  • 2021-11-06
  • 2022-02-15
  • 2021-06-04
  • 2021-10-22
相关资源
相似解决方案