Developers' Forum

C# with pylint for cunstruct checking

C# with pylint for cunstruct checking

by James Roberts -
Number of replies: 1

Hi there,

I was wondering if there is anyway to use the functionality of the python3_cosc121 question template with C# code.

Currently I have a template that runs C# code just fine (got it from another forum post on here).

But i was wondering if its possible to check for certain C# constructs, for example requiring that the student writes a for loop and not a while loop.

Any help would be greatly appreciated.


In reply to James Roberts

Re: C# with pylint for cunstruct checking

by Richard Lobb -

I assume the C# template you're using is written in Python. You should be able to extend that template to perform various checks on the submitted code before running it. But I doubt the code in the python3_cosc121 template will be of much direct use. The version currently  in the file uocprototypes.xml in the CodeRunner distribution uses Python's ast module to do a complete parse of the submitted Python ode. It then traverses the parse tree to count uses of various constructs and functions. This makes it relatively straightforward to disallow or require particular constructs and/or functions.

I have a C question type (written in Python) that does something similar, using a third party pycparser module to parse the student's C code. But C# is a much more complex proposition and I can't immediately see any third party C# parsers written in Python on the web.

What we used to do, and still do in messy ill-defined languages like Matlab, is use roll-your-own code checkers (e.g. with extensive use of regular expressions) to remove comments and then search for banned or required constructs. As a very simplistic example, you might have something like:

student_answer = """{{ STUDENT_ANSWER | e('py') }}"""
if 'while' not in student_answer:
    raise Exception("Your code must contain at least one while loop")

This is of course a very weak check, satisfied even by a comment like 

// I never use while loops

but it perhaps shows you the general idea?

To do this all properly (to the extent possible in the absence of a parser) takes a lot of effort, but writing new question types is certainly a lot more enjoyable than marking.

Check out the python3_cosc121 prototype to see how to use template parameters to pass in the banned or required constructs so that you don't have to directly edit the template for each such question.