1

I'm using the Python Watchdog to monitor a directory for new files being created. Several different types of files are created in said directory but I only need to monitor a single file type, hence I use the Watchdog PatternMatchingEventHandler, where I specify the pattern to monitor using the patterns keyword.

To correctly execute the code under the hood (not displayed here) I need to initialize an empty dataframe in my event-handler, and I am having trouble getting this to work. If I remove the __init__ in the code below, everything works just fine btw.

I used the code in this answer as inspiration for my own.

The code I have set up looks as follows:

from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
import time
import pandas as pd
import numpy as np
from multiprocessing import Pool

class HandlerEQ54(PatternMatchingEventHandler):

    def __init__(self):
        #Initializing an empty dataframe for storage purposes.
        data_54 = pd.DataFrame(columns = ['Barcode','DUT','Step12','Step11','Np1','Np2','TimestampEQ54'])
        #Converting to INT for later purposes
        data_54[['Barcode','DUT']]=data_54[['Barcode','DUT']].astype(np.int64)
        self.data = data_54

    def on_created(self,event):
        if event.is_directory:
            return True

        elif event.event_type == 'created':
        #Take action here when a file is created.
            print('Found new files:')
            print(event.src_path)
            time.sleep(0.1)

            #Creating process pool to return data
            pool1 = Pool(processes=4)
            #Pass file to parsing function and return parsed result.
            result_54 = pool1.starmap(parse_eq54,[(event.src_path,self.data)])
            #returns the dataframe rather than the list of dataframes returned by starmap
            self.data = result_54[0]


            print('Data read: ')
            print(self.data)


def monitorEquipment(equipment):
    '''Uses the Watchdog package to monitor the data directory for new files.
    See the HandlerEQ54 and HandlerEQ51 classes in multiprocessing_handlers for actual monitoring code.  Monitors each equipment.'''

    print('equipment')

    if equipment.upper() == 'EQ54':

        event_handler = HandlerEQ54(patterns=["*.log"])
        filepath = '/path/to/first/file/source/'

    # set up observer
    observer = Observer()
    observer.schedule(event_handler, path=filepath, recursive=True)
    observer.daemon=True
    observer.start()
    print('Observer started')
    # monitor
    try:
        while True:
            time.sleep(5)
    except KeyboardInterrupt:
        observer.unschedule_all()
        observer.stop()
    observer.join()

However, when I execute monitorEquipment I receive the following error message:

TypeError: __init__() got an unexpected keyword argument 'patterns'

Evidently I'm doing something wrong when I'm initializing my handler class, but I'm drawing a blank as to what that is (which probably reflects my less-than-optimal understanding of classes). Can someone advice me on how to correctly initialize the empty dataframe in my HandlerEQ54 class, to not get the error I do?

AstroAT
  • 482
  • 1
  • 6
  • 16

1 Answers1

0

Looks like you are missing the patterns argument from your __init__ method, you'll also need a super() call to the __init__ method of the parent class (PatternMatchingEventHandler), so you can pass the patterns argument upwards.

it should look something like this:

class HandlerEQ54(PatternMatchingEventHandler):

    def __init__(self, patterns=None):
        super(HandlerEQ54, self).__init__(patterns=patterns)
...

event_handler = HandlerEQ54(patterns=["*.log"])

or, for a more generic case and to support all of PatternMatchingEventHandler's arguments:

class HandlerEQ54(PatternMatchingEventHandler):

    def __init__(self, *args, **kwargs):
        super(HandlerEQ54, self).__init__(*args, **kwargs)
...

event_handler = HandlerEQ54(patterns=["*.log"])
Jon
  • 101
  • 5