Python - Getting Object is not callable error

Viewed 28

I'm doing a homework assignment for my class this deals with state machines. This is a very basic state machine that checks the positive or negative wording towards python. while trying to test my code I get the Object is not callable error on my M variable for testing here is my code for my simple state machine class

class InitializationError(Exception): 
pass 
class StateMachine: 
     
    def __init__(self): 
        self.handlers = {} 
        self.startState = None 
        self.endStates = [] 
 
    def add_state(self, name, handler, end_state=0): 
        name = name.upper() 
        self.handlers[name] = handler 
        if end_state: 
            self.endStates.append(name) 
 
    def set_start(self, name): 
        self.startState = name.upper() 
 
    def run(self, cargo): 
        try: 
            handler = self.handlers[self.startState] 
        except: 
            raise InitializationError("must call .set_start() before .run()") 
        if not self.endStates: 
            raise  InitializationError("at least one state must be an end_state") 
     
        while True: 
            (newState, cargo) = handler(cargo) 
            if newState.upper() in self.endStates: 
                print("reached ", newState) 
                break  
            else: 
                handler = self.handlers[newState.upper()]  

The next section is my main function

import StateMachine
positive_adj = ["great","super", "fun", "entertaining", "easy"] 
negative_adj = ["boring", "difficult", "ugly", "bad"] 

def start_transitions(txt): 
    splitted_txt=txt.split(None,1)
    word,txt=splitted_txt if len(splitted_txt) > 1 else (txt,"")
    if word == "Python":
            newState="Python_state"
    else:
            newState="error_state"
    return(newState,txt)  
 
 
def python_state_transitions(txt): 
    splitted_txt=txt.split(None,1)
    word,txt=splitted_txt if len(splitted_txt)>1 else(txt,"")
    if word=="is":
        newState="is_state"
    else:
        newState="error_state"
    return(newState,txt)
 

def is_state_transitions(txt): 
    splitted_txt=txt.split(None,1)
    word,txt=splitted_txt if len(splitted_txt)>1 else(txt,"")
    if word=="not":
        newState="not_state"
    elif word in positive_adj:
        newState="pos_state"
    elif word in negative_adj:
        newState="neg_state"
    else:
        newState="error_state"
    return(newState,txt)
 

def not_state_transitions(txt): 
    splitted_txt=txt.split(None,1)
    word,txt=splitted_txt if len(splitted_txt)>1 else(txt,"")
    if word in positive_adj:
        newState="neg_state"
    elif word in negative_adj:
        newState="pos_state"
    else:
        newState="error_state"
    return(newState,txt)


m = StateMachine() 
m.add_state("Start", start_transitions) 
m.add_state("Python_state", python_state_transitions) 
m.add_state("is_state", is_state_transitions) 
m.add_state("not_state", not_state_transitions) 
m.add_state("neg_state", None, end_state=1) 
m.add_state("pos_state", None, end_state=1) 
m.add_state("error_state", None, end_state=1) 
m.set_start("Start") 
m.run("Python is great") 
m.run("Python is difficult") 
m.run("Perl is ugly")

I was wondering why my m variable for the state machine is saying it is not callable even though I have functions connected to it. thank you for reading my post.

0 Answers
Related