For an ML course I'm TAing next semester, we're using an autograding system. We're asking students to do their own implementations of some standard algorithms, so we'd like to restrict students from loading certain libraries (with either a blacklist or a whitelist, not sure.)
对于我将在下学期学习的ML课程,我们正在使用自动编排系统。我们要求学生自己实施一些标准算法,所以我们要限制学生加载某些库(黑名单或白名单,不确定)。
Are there any reasonable ways to do this with R or matlab? Or is inspecting the source code (i.e. regex/grep) the best way to go?
有没有合理的方法用R或matlab做到这一点?或者正在检查源代码(即regex / grep)的最佳方法?
1 个解决方案
#1
1
Use the trace
function to change the behaviour of library
. When the library
function is called, the following code retrieves the name of the package that is passed to library
, and then throws an error if it on the banned list.
使用跟踪功能可以更改库的行为。调用库函数时,以下代码检索传递给库的包的名称,然后在禁止列表中抛出错误。
trace(
base::library,
function()
{
package_name <- if(parent.frame()$character.only)
{
parent.frame()$package
} else
{
deparse(substitute(package, parent.frame()))
}
if(package_name %in% c("ggplot2", "lattice")) #or whichever packages are banned
{
stop("The ", sQuote(package_name), " package is not allowed")
}
}
)
library(ggplot2)
library("ggplot2", character.only = TRUE)
library(plyr)
You'll also need to trace
the require
function.
您还需要跟踪require函数。
Beware sneaky students: if they know that this is how you are preventing package loading, then they can turn tracing off in their script (and maybe reenable it afterwards). You should perhaps check for calls to trace
/untrace
/traceOn
/traceOff
in their scripts too. How much effort you put into this depends on how much manual looking at their code you are going to do and how honest your students are. Weird evasive code like that should stand out if you read it.
谨防偷偷摸摸的学生:如果他们知道这是你阻止包加载的方式,那么他们可以在他们的脚本中关闭跟踪(并且可能在之后重新启用它)。您也许应该在脚本中检查对trace / untrace / traceOn / traceOff的调用。你投入了多少精力取决于手动查看他们将要执行的代码的程度以及学生的诚实程度。如果你阅读它,那些奇怪的回避代码应该脱颖而出。
#1
1
Use the trace
function to change the behaviour of library
. When the library
function is called, the following code retrieves the name of the package that is passed to library
, and then throws an error if it on the banned list.
使用跟踪功能可以更改库的行为。调用库函数时,以下代码检索传递给库的包的名称,然后在禁止列表中抛出错误。
trace(
base::library,
function()
{
package_name <- if(parent.frame()$character.only)
{
parent.frame()$package
} else
{
deparse(substitute(package, parent.frame()))
}
if(package_name %in% c("ggplot2", "lattice")) #or whichever packages are banned
{
stop("The ", sQuote(package_name), " package is not allowed")
}
}
)
library(ggplot2)
library("ggplot2", character.only = TRUE)
library(plyr)
You'll also need to trace
the require
function.
您还需要跟踪require函数。
Beware sneaky students: if they know that this is how you are preventing package loading, then they can turn tracing off in their script (and maybe reenable it afterwards). You should perhaps check for calls to trace
/untrace
/traceOn
/traceOff
in their scripts too. How much effort you put into this depends on how much manual looking at their code you are going to do and how honest your students are. Weird evasive code like that should stand out if you read it.
谨防偷偷摸摸的学生:如果他们知道这是你阻止包加载的方式,那么他们可以在他们的脚本中关闭跟踪(并且可能在之后重新启用它)。您也许应该在脚本中检查对trace / untrace / traceOn / traceOff的调用。你投入了多少精力取决于手动查看他们将要执行的代码的程度以及学生的诚实程度。如果你阅读它,那些奇怪的回避代码应该脱颖而出。