I'm making a small python script/program for extracting exam questions from a certain med-school, based on filtering keywords.
It's been working pretty well. I've been adding more and more features and improving compatibility, and have done some debugging. However, all of a sudden now the script returns zero questions all the time, and when entering debug mode, I found that there is a very strange NameError
occurring on a line in a class __init__
function, that has been working fine before. I'm not really sure what has triggered it tbh, though IIRC I made some small adjustments in that part of the code.
When watching the variable in question self.rawQuestions
, in the debugger, it appears to work fine until a certain line of code, at which point the NameError
occurs.
Also worth noting that I'm a beginner when it comes to programming so I wouldn't be surprised if this is a really stupid error with an obvious solution, sorry if that's the case. But when I search google/stackoverflow for a similar error, pretty much all of the questions have used the self keyword outside of the __init__
class which is different.
Here is a small excerpt with unrelated code cut out, the error occurs on the line self.rawQuestions = [x for x...:
class Exam:
class Course:
...
class Question:
...
def __init__(self, text, path):
# Add text to text string
self.text = text
# Split text into questions
if re.search(r"(Q|q)uestion", self.text):
self.rawQuestions = self.text.rsplit("Question")
elif re.search(r"(F|f)råga", self.text):
self.rawQuestions = re.split(r"(?:F|f)råga(?=\s)", self.text)
### ERROR IS ON LINE BELOW ###
# Filter out any questions containing only "Orzone..."
self.rawQuestions = [x for x in self.rawQuestions if not re.search(r"^(?<!\w)\s*\d*\s*Orzone\s*AB\s*Gothenburg\s*www\.orzone\.com.*$", x, flags=re.IGNORECASE|re.DOTALL)]
# Filter out questions containing a question, but with an "Orzone.. or Course at the end"
self.rawQuestions = [re.sub(r"\sOrzone\s*AB\s*Gothenburg\s*www\.orzone\.com.*$", "", x) for x in self.rawQuestions]
# Delete course name and semester from questions then filter out empty questions
self.rawQuestions = [re.sub(rf"\s*[\w ]*{self.course.searchterm}.*[vh]t-?\d\d.*$", "", x) for x in self.rawQuestions]
self.rawQuestions = [x for x in self.rawQuestions if len(x) < 1]
# Make a list of question objects using the questions extracted using split
self.questions = []
for question in self.rawQuestions:
self.questions.append(self.Question(question, self))