在Google App Engine中动态加载来自数据库的Python应用程序代码 [英] Dynamically loading Python application code from database under Google App Engine

查看:96
本文介绍了在Google App Engine中动态加载来自数据库的Python应用程序代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将python代码存储在数据库中,并将其加载到某种bootstrap.py应用程序中以供执行。我无法使用文件系统,因为我使用GAE,所以这是我唯一的选择。



然而,我不是一个经验丰富的Python用户。



我已经能够加载1行代码并使用eval来运行它,但是一行两行或更多行的代码给了我一个无效的语法错误。



我也在考虑是否可以扩展导入加载器来实现数据库加载。



谢谢!

解决方案

在阅读了关于Python动态代码加载的更多信息之后,我能够完成自己的工作。

以下是示例代码。我删除了标题更轻:



谢谢!



========== ===

  class DynCode(db.Model):
name = db.StringProperty()
code = db.TextProperty(default = None)

=========== ==

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' DynCode()
dyn =index
dyn.code =
from google.appengine.ext import webapp $ b $ class MainHandler(webapp.RequestHandler):
def get(self):
self.response.out.write(Hello World \\)
self.response.out.write(Hello World 2 \\\\


dyn.put()
self.response.out.write(OK。)

def main():
application = webapp.WSGIApplication([('/ update',MainHandler)],debug = True)
util.run_wsgi_app(应用程序)
$ b $如果__name__ =='__main__':
main()

============================ ======

  def main():
query = DynCode.all()
dyncodes = query.fetch(1)
module = imp.new_module('mymodule')
for dynodes中的dyn:
模块中的exec dyn.code .__ dict__

application = webapp.WSGIApplication([('/',module.MainHandler)],debug = True)
util.run_wsgi_app(应用程序)
$ b $如果__name__ =='__main__':
main()

=============== ========


I need to store python code in a database and load it in some kind of bootstrap.py application for execution. I cannot use filesystem because I'm using GAE, so this is my only choice.

However I'm not a python experienced user.

I already was able to load 1 line of code and run it using eval, however a piece of code with two lines or more gave me a "invalid syntax" error.

I'm also thinking if it's possible to extend the "import" loader to implement the DB loading.

Thanks!

解决方案

I was able to do what I intent after reading more about Python dynamic code loading.

Here is the sample code. I removed headers to be lighter:

Thanks anyway!

=============

class DynCode(db.Model):
    name = db.StringProperty()
    code = db.TextProperty(default=None)

=============

class MainHandler(webapp.RequestHandler):
    def get(self):
        dyn = DynCode()
        dyn = "index"
        dyn.code = """
from google.appengine.ext import webapp
class MainHandler(webapp.RequestHandler):
    def get(self):
        self.response.out.write("Hello World\\n")
        self.response.out.write("Hello World 2\\n")
"""
        dyn.put()
        self.response.out.write("OK.")

def main():
    application = webapp.WSGIApplication([('/update', MainHandler)], debug=True)
    util.run_wsgi_app(application)

if __name__ == '__main__':
    main()

==================================

def main():
    query = DynCode.all()
    dyncodes = query.fetch(1)
    module = imp.new_module('mymodule')
    for dyn in dyncodes:
        exec dyn.code in module.__dict__

    application = webapp.WSGIApplication([('/', module.MainHandler)], debug=True)
    util.run_wsgi_app(application)

if __name__ == '__main__':
    main()

=======================

这篇关于在Google App Engine中动态加载来自数据库的Python应用程序代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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