Skip to content

Instantly share code, notes, and snippets.

@thuwarakeshm
Last active August 11, 2022 11:20
Show Gist options
  • Save thuwarakeshm/eb3e8ab8facd10d769a748e74a618725 to your computer and use it in GitHub Desktop.
Save thuwarakeshm/eb3e8ab8facd10d769a748e74a618725 to your computer and use it in GitHub Desktop.
...
import argparse
...
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Watchdog script to watch for new CSV files and load them into the database"
)
parser.add_argument("path", help="Path to watch for new CSV files")
args = parser.parse_args()
path = args.path
# Create an observer.
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
...
import os
import pandas as pd
from sqlalchemy import create_engine
from watchdog.events import FileSystemEventHandler
...
class FileCreateHandler(FileSystemEventHandler):
def on_created(self, event):
# Create SQLAlchemy engine for postgres database using environment variables
engine = create_engine(os.environ["DATABASE_URL"])
with engine.connect() as con:
# Read the CSV data
df = pd.read_csv(event.src_path)
# Do the required transformations
df["date"] = pd.to_datetime(df["date"])
# Write the data to the database
df.to_sql("weather", con, if_exists="append", index=False)
...
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class FileCreateHandler(FileSystemEventHandler):
def on_created(self, event):
print("Created: " + event.src_path)
if __name__ == "__main__":
event_handler = FileCreateHandler()
# Create an observer.
observer = Observer()
# Attach the observer to the event handler.
observer.schedule(event_handler, ".", recursive=True)
# Start the observer.
observer.start()
try:
while observer.is_alive():
observer.join(1)
finally:
observer.stop()
observer.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment