-1

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))
emileber
  • 23
  • 4
  • ```Class``` is not the same as ```class```. Python is case sensitive. The code should raise an error stating ```Class``` is not defined. You have a typo. You have written ```Class``` instead of ```class``` –  Aug 23 '21 at 11:21
  • Oops that is also only incorrect here on SO, not in my code... sorry. It says class in my code. – emileber Aug 23 '21 at 11:43

1 Answers1

0

THe problem here should be indentation on your '__ init __' method. Try change it like this:

class Exam:
    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)

        # 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))

    class Course:
        ...
    class Question:
        ...
Alfredo Neto
  • 24
  • 1
  • 5
  • whoops I formatted the code incorrectly here on stackoverflow. It's actually formatted the way you did it ^^ sorry about that, edited now. Provided the indentation is correct, what could be another cause? – emileber Aug 23 '21 at 11:20
  • ```Class``` is not the same as ```class```. –  Aug 23 '21 at 11:20
  • Oops that is also only incorrect here on SO, not in my code... sorry. It says *c*lass in my code. – emileber Aug 23 '21 at 11:24
  • Got any other ideas? @Sujay – emileber Aug 23 '21 at 17:01