以下代码用于在Google App Engine应用中执行doctests.对于作为单元测试断言而不是作为doctests编写的测试,你会如何做到这一点?
#The solution and tests are untrusted code passed in to the GAE app.
solution = 'b=5'
unittest = 'assertEqual(b, 5)'
#Here is the doctest version as a reference.
solution = 'b=5'
doctest = '>>> b \n 5'
#Compile and exec the untrusted solution provided by the user.
compiled = compile(solution, 'submitted code', 'exec')
sandbox = {}
exec compiled in sandbox
#Compile and exec each of the doctests
test_cases = doctest.DocTestParser().get_examples(doctest)
for test in test_cases:
if not test.want: …Run Code Online (Sandbox Code Playgroud) 我正在使用Google App Engine mapreduce来分析一些数据.我正在生成一些计数器,我想在我的done_callback中创建一个简单的Google图表.如何从回调中访问结果计数器?
#The map method
def count_created_since(entity):
now = datetime.datetime.now()
delta = now-entity.created
#analyze last 12 weeks
for x in range(12):
start = 7*x
stop = 7*(x+1)
if delta.days >= start and delta.days < stop:
#The counters
yield op.counters.Increment(str(x)+" weeks ago")
def my_callback(request):
# fetch counter results to create a simple Google chart url
Run Code Online (Sandbox Code Playgroud) GAEUnit一年多的要求功能之一就是支持代码覆盖.在技术上是否可以为GAEUnit添加覆盖支持,以便在测试完成后,可以生成报告测试的模块以及每个模块的覆盖范围是什么?
我可以使用coverage.py启动dev_appserver并生成GAE python应用程序的覆盖率报告,但这需要本地系统配置.关于gaeunit的一个好处是能够轻松地将其作为应用程序添加到任何python gae应用程序,以便下载该应用程序的任何人都可以运行所包含的测试.我的理想情况是扩展gaeunit并将覆盖率数据添加到报告的底部.因此理想情况下,修改将作为应用程序在dev_appserver内运行,就像gaeunit一样.
这在技术上是否可行?你会推荐什么方法?
由于python版本的Google App Engine不支持信号模块,如果方法在不到2秒内没有返回,调用方法和抛出/捕获异常的最简单方法是什么?
为了重新部署GAE应用程序,我目前必须在我用于部署的系统上安装GAE部署工具.虽然此过程相对简单,但部署过程是一个手动过程,无法在防火墙后面运行,并且必须在将用于更新GAE应用程序的每台计算机上安装部署工具.如果我可以从之前部署的另一个GAE应用程序更新GAE应用程序,那么更理想的解决方案就是如此.这将消除将多个系统配置为部署应用程序的需要.
由于GAE部署工具是用Python编写的,GAE App Engine支持Python,因此可以修改appcfg.py以便在GAE中工作吗?用例是从GitHub或其他一些在线存储库中提取项目,并从另一个GAE应用程序更新一个GAE应用程序.如果这不可能,限制约束是什么?
在python中,从Google App Engine应用程序中启动Amazon EC2实例的最佳方法是什么?我希望保持AWS密钥尽可能安全,并能够检索新启动的EC2实例的公共DNS.
我想让学生为一些简单的python问题提交python代码解决方案.我的应用程序将在GAE中运行.如何限制sumios的源代码的风险?我意识到这是一个难题,我已经阅读了相关的Stackoverflow和其他帖子.我很好奇,如果在GAE环境中适当的限制使得更容易限制不受信任的代码可能造成的损害.是否可以简单地扫描提交的代码以获取一些受限制的关键字(执行,导入等),然后确保代码仅运行不到一段固定的时间,或者即使在resticted GAE环境?例如:
# Import and execute untrusted code in GAE
untrustedCode = """#Untrusted code from students."""
class TestSpace(object):pass
testspace = TestSpace()
try:
#Check the untrusted code somehow and throw and exception.
except:
print "Code attempted to import or access network"
try:
# exec code in a new namespace (Thanks Alex Martelli)
# limit runtime somehow
exec untrustedCode in vars(testspace)
except:
print "Code took more than x seconds to run"
Run Code Online (Sandbox Code Playgroud) 我已经为CSV文件中的模型备份了所有实体.我正在将实体恢复到我的本地dev_server,并希望在csv文件中重新创建具有ID的实体(类似于bulkloader的工作方式).如何在我的create语句中为新实体传入所需的ID?
playerID = 1234
player = Player(created = datetime.datetime(2012, 1, 25, 9, 20, 5, 757227),
nickname = u'chris',
email = u'chris@home.com')
player.put()
Run Code Online (Sandbox Code Playgroud)
当我调用put()时,我添加到Player()以使用player.key()创建播放器.id()== 1234?
什么是动态创建Python对象实例的最佳方法是将Python类保存为字符串?
作为背景,我在Google Application Engine环境中工作,我希望能够从类的字符串版本动态加载类.
problem = “1,2,3,4,5”
solvertext1 = “””class solver:
def solve(self, problemstring):
return len(problemstring) “””
solvertext2 = “””class solver:
def solve(self, problemstring):
return problemstring[0] “””
solver = #The solution code here (solvertext1)
answer = solver.solve(problem) #answer should equal 9
solver = #The solution code here (solvertext2)
answer = solver.solve(problem) # answer should equal 1
Run Code Online (Sandbox Code Playgroud) 我想在使用内置文件类型的Python App Engine的Python版本上运行一些代码.我正在寻找最简单的方法来阻止GAE因非法访问而抛出错误.有没有人已经分类或模拟文件读取和写入内存而不是磁盘?我不需要持久性,只需要模拟文件访问的能力.
我正在Google Application Engine环境中工作,我从字符串加载doctests和python代码来测试Python家庭作业.我的基本实现(由Alex Martelli提供)似乎适用于我的所有问题,除了那些包含print语句的问题.当我尝试在GAE中执行打印命令时,似乎出现了问题.
您如何修改此示例以捕获print语句写出的任何内容?
#This and most other code works
class X(object): pass
x=X()
exec 'a=23' in vars(x)
#This throws an error.
class X(object): pass
x=X()
exec 'print 23' in vars(x)
Run Code Online (Sandbox Code Playgroud) 在导入main.py而不是运行它时,运行此代码块的最佳方法是什么?
#main.py
if __name__ == '__main__':
#Do something interesting.
#Do something to have the interesting code run after importing the file.
import main.py
main.__main__() # wrong of course.
Run Code Online (Sandbox Code Playgroud)