I'm writing Hacker Rank style, offline applications for a class at Temple University. I have a task.py, which is where the user should write their own code. I have a test.py where I have created a module to test the user's code.
I'm using Thonny, because it requires very low resources, which is assumed to be if the student doesn't have internet at home, for whom the code is being written.
Here is an example for task.py
import sys
def variables():
f = open('test.txt', 'w')
#Start your code below (tip: Make sure to indent your code)
penguin = "Penguin"
six = "6"
false = "False"
none = "None"
a = "100.66"
f.write(penguin+"\n")
f.write(six+"\n")
f.write(false+"\n")
f.write(none+"\n")
f.write(a+"\n")
print(penguin)
print(6)
print(false)
print(none)
print(a)
f.close()
Here is my testing file, test.py
from task import variables
import filecmp, os
variables()
a=filecmp.cmp("test.txt","ans.txt")
if a == True:
print("Test Case Passed")
os.remove("test.txt")`
I need the test.py file to be locked down so the student can't modify it, but I need it to be able to be called. Any ideas?
There is a module for cross-platform file locking called portalocker (https://pypi.python.org/pypi/portalocker), but if you don't want to use that you can check for something else. Using the os function
last_mod = os.path.getmtime('test.txt')
will give you the last time the file was modified, if the last time is different then when it was created then someone edited it. This is just an alternative to portalocker... Just keep a copy of the og file somewhere and if the modification time is different just have python write the copy in over the modified file. I strongly recommend just using the module for this one because there's a lot more cool functions that you can use from it. If you have any further questions just comment them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With