0

I will try to explain the problem I am facing with a small piece of code:

class MyHandler(PatternMatchingEventHandler):
    patterns = ["*.csv","*.processing", "*.transforming","*.loading"]

    def process(self, event):
        eventFileName = event.src_path
        eventType = event.event_type
        if eventType == 'moved':
            eventFileName = event.dest_path
        fileNameWithPath, fileExtension = os.path.splitext(eventFileName)

        if fileExtension == '.processing': 
            # Here some function is called to do something, and then appends ".loading" to the file name
            testVariable = 75.3
        if fileExtension == '.loading':
            print testVariable
    def on_moved(self, event):
        self.process(event)

    def on_created(self, event):
        self.process(event)

if __name__ == '__main__':
    observer = Observer()
    observer.schedule(MyHandler(), path='.')
    observer.start()

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()

    observer.join()

When I try to do the above I am getting this error: global name 'testVariable' is not defined which kinda makes sense but how do I make the above work? I tried to define "testVariable" globally and initiated to 0 and tried using it the way I showed in above code but it did not work as well. I also tried initiating testVariable as testVariable=0 inside the class (right after "patterns = ....." line but I got this error: local variable "testVariable" referenced before assignment pointing towards print testVariable. So that didnot work as well.

3 Answers 3

2

"(...) how do I make the above work?"

By defining testVariable outside your conditional statements. E.g. here:

def process(self, event):
    eventFileName = event.src_path
    testVariable = 0
    ...

This will make it available within the process function. If you want it to be available throughout the class, you can define it here:

class MyHandler(PatternMatchingEventHandler):
    patterns = ["*.csv","*.processing", "*.transforming","*.loading"]
    testVariable = 0

But then you have to access it via the self object within functions like so:

def process(self, event):
    ...
    if fileExtension == '.processing': 
        # Here some function is called to do something, and then appends ".loading" to the file name
        self.testVariable = 75.3
Sign up to request clarification or add additional context in comments.

3 Comments

I tried the second approach before I posted the question (apologies for not putting that information in the question. Just updated it) and it generated: local variable 'testVariable' referenced before assignment error first approach looks like working. I will test my code out
Works :-) On a side note, I tried the self before posting the question but I did something stupid. But thanks, I see my mistake in the code now.
Nice. Which version is best depends on your requirements. If you need to access testVariable from outside the class, you'll have to make it a class variable so you can do handler = MyHandler(arguments); handler.testVariable but if you only need it in the process function, that's not necessary
0

testVariable only exists if you have the extension ".processing". If it's ".loading", the program tries to print a variable that hasn't been made to exist.

If statements do not create a garbage collecting scope in Python, so you don't have to "declare" it outside, so long as somewhere in your if-tree, tesVariable gets a value.

Comments

0
def process(self, event):
    def extension():
        eventFileName = event.src_path
        eventType = event.event_type
        if eventType == 'moved':
            eventFileName = event.dest_path
        return os.path.splitext(eventFileName)[1]

    if extension() == '.processing': 
        ...
    if extension() == '.loading':
        ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.