利用NodeSchool工具创建教程学习工具系列3

这是利用NodeSchool工具创建教程学习工具系列之三,上一部分见利用NodeSchool工具创建教程学习工具系列2

最近几天利用workshopper在仿照learnyounode制作一款学习python的教程工具,总之真的深深体会到一句话:“说的容易,做起来难”。

由于learnyounode本身就是处理js文件所以就相对方便许多,但是对于python学习就需要处理py文件,这样就需要多一道处理工序。

首先,说说用到的一些模块吧:

  • workshopper – 这个肯定的,核心模块
  • workshopper-exercise – 这个是workshopper有关的文件检查,代码执行以及对比有关的工具库
  • python-shell – 用来执行python文件的

上面是用到的主要模块,其他一些会在之后遇到时详细讲的。

python代码处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var PythonShell = require('python-shell'),
path = require('path');

var file = process.argv[2],
filePath = path.resolve(file),
dir = path.dirname(filePath);

PythonShell.run(file, {
pythonPath: 'D:\\software\\python34\\python3.exe',
scriptPath: dir,
},function(err, results) {
if (err) throw err;
// results is an array consisting of messages collected during execution
console.log(results.join(''));
});

主要是通过process.argv[2]来接受要执行的文件,然后将该文件分别传给参考答案执行进程和用户提交答案执行进程。

最后根据上一节讲的workshopper的工作方式,通过在exercise.js中去传递参数,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
var exercise      = require('workshopper-exercise')()
, filecheck = require('workshopper-exercise/filecheck')
, execute = require('workshopper-exercise/execute')
, comparestdout = require('workshopper-exercise/comparestdout')
, path = require('path');


// checks that the submission file actually exists
exercise = filecheck(exercise);

// execute the solution and submission in parallel with spawn()
exercise = execute(exercise);

// compare stdout of solution and submission
exercise = comparestdout(exercise);


exercise.addSetup(function (mode, callback) {
// mode == 'run' || 'verify'

// supply the args to the 'execute' processor for both
// solution and submission spawn()
this.submissionArgs = this.submission;
this.solutionArgs = this.solution.replace(/\.js/, '.py');

this.submissionCommand = [ 'run-verify.js' ].concat(this.submissionArgs);

process.nextTick(callback);
});

module.exports = exercise;

这是我的想法,如果大家有什么idea可以提出来,谢谢。

未完待续。。。。。。下节见

微信