Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
执行python代码并评估/测试结果_Python - Fatal编程技术网

执行python代码并评估/测试结果

执行python代码并评估/测试结果,python,Python,诚然,我不知道该如何问这个问题,因为我知道如何在R(新环境中的代码执行)中处理这个问题,但对python解决方案的等效搜索并没有产生我所希望的结果 简而言之,我将收到一个电子表格(或csv),其中列的内容将包含有效的python代码。这可能相当于一个脚本,但只包含在csv/工作簿中。对于一个用例,考虑教学编程,输出是LMS 我希望做的是在文件上循环,对于每个单元格,运行代码,并将结果存储在内存中,测试是否存在某些东西 例如: 在评估上述电子表格中的第一个响应时,我希望测试x、y和z是否都正确定义

诚然,我不知道该如何问这个问题,因为我知道如何在R(新环境中的代码执行)中处理这个问题,但对python解决方案的等效搜索并没有产生我所希望的结果

简而言之,我将收到一个电子表格(或csv),其中列的内容将包含有效的python代码。这可能相当于一个脚本,但只包含在csv/工作簿中。对于一个用例,考虑教学编程,输出是LMS

我希望做的是在文件上循环,对于每个单元格,运行代码,并将结果存储在内存中,测试是否存在某些东西

例如:

在评估上述电子表格中的第一个响应时,我希望测试x、y和z是否都正确定义并具有预期值

因为文件中有多行,每个学生一行,我如何分别运行每一行,评估结果,并确保仅将结果隔离到该单元格。简单地说,当继续进行时,我不保留任何过去的评估。

(我不知道用于代码检查的工具,因此我以非常手动的方式处理它。)

可以使用Python的
exec()
函数来执行字符串,例如单元格中的内容

例:

处理csv文件:

import csv

csv_file = open("path_to_csv_file", "rt")
csv_reader = csv.reader(csv_file)
iterator = iter(csv_reader)
next(iterator) # To skip the titles of the columns

for row in iterator:
    user = row[0]
    answer = row[1]

### Any other code involving the csv file must be put here to work properly,
### that is, before closing csv_file.

csv_file.close() # Remember to close the file.
它将无法检测是否导入了某些模块(因为从
exec()
函数导入时,该模块将保留在缓存中以供下一个exec使用)。测试这一点的一种方法是“取消”模块,并测试exec是否存在异常。 例:

我觉得这不是最好的办法,但肯定是一种尝试。 我希望这有帮助


编辑:删除了一些错误代码并添加了Tadhg McDonald Jensen评论的一部分。

您正在寻找的是,您可以传递一个字符串代码和一个字典作为变量名称空间使用,以便可以将其作为普通字典进行检查。你是在问关于安全措施的问题,还是这就足够了?@Tadhgcdonald Jensen这可能会起作用,当我从数据科学的角度来看python时,我并不知道。让我来一个高峰。谢谢你。我当然可以用一些。我不知道exec和将名称空间存储为dict以供进一步检查的能力。从技术上讲,
del os
实际上并不能解除对模块的影响,您必须执行类似于
del sys.modules[“os”]
的操作,但此时您最好生成子流程来运行每个模块,这样您就可以监视stdout并每次重置所有模块,但对于你想要的东西来说,这可能是过度的,所以你得到它的方式很好:)
import csv

csv_file = open("path_to_csv_file", "rt")
csv_reader = csv.reader(csv_file)
iterator = iter(csv_reader)
next(iterator) # To skip the titles of the columns

for row in iterator:
    user = row[0]
    answer = row[1]

### Any other code involving the csv file must be put here to work properly,
### that is, before closing csv_file.

csv_file.close() # Remember to close the file.
# This piece of code would be before closing the file,
# INSIDE THE FOR LOOP AND WITH IT IDENTED (Because you want
# it to run for each student.).

try:
    del os # 'unimporting' os (This doesn't 'unimport' as much as deletes a
           # reference to the module, what could be problematic if a 'from
           # module import object' statement was used.)
except NameError: # So that trying to delete a module that wasn't imported
                  # does not lead to Exceptions being raised.
    pass

namespace = dict()
try:
    exec(answer, namespace)
except:
    # Answer code could not be run without raising exceptions, i.e., the code
    # is poorly written.

    # Code you want to run when the answer is wrong.
else:
    # The code hasn't raised Exceptions, time to test the variables.

    x, y, z = namespace['x'], namespace['y'], namespace['z']
    if (x == 2) and (y == 6) and (z == x * y):
        # Code you want to run when the answer is right.
    else:
        # Code you want to run when the answer is wrong.