Python Forum
Mult-threading and locking file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mult-threading and locking file
#3
I wrote a tutorial on automatically creating a database from csv files.
Note: Project uses sqlite database. Other DBMS can be used with a simple change to the model.

The project demonstrates dynamic model creation, and subsequent database load, achieving the following:

  1. Given a path to a directory that contains all of the CSV files (one for each table) do:
    • Determine columns and sizes required for each.
    • Auto-create an SQLalchemy model class which represents the table that will be associated with the data from each CSV file.
    • Using pandas, populate the database.

to show the simplicity of the code, here's the database load module:
from FlightPaths import FlightPaths
import pandas as pd
import DbModel
import csv


class LoadDatabase:
    def __init__(self):
        self.fpath = FlightPaths()

        self.Model = DbModel

    def dispatch(self):
        self.load_all()

    def load_all(self):
        datafiles = self.fpath.ourairport_datafiles

        filelist = [fn for fn in datafiles.iterdir() if fn.is_file() and fn.suffix == '.csv']
        for fn in filelist:
            tablename = fn.stem.replace('-', '_')
            df = pd.read_csv(fn)
            print(f"engine: {self.Model.engine}")
            df.to_sql(tablename, con=self.Model.engine, if_exists='replace')


def main():
    lDb = LoadDatabase()
    lDb.load_all()


if __name__ == '__main__':
    main()
If you are interested, you will find the tutorial here: https://python-forum.io/thread-33843.html
Reply


Messages In This Thread
Mult-threading and locking file - by mr_byte31 - Oct-15-2021, 02:35 PM
RE: Mult-threading and locking file - by SamHobbs - Oct-15-2021, 04:59 PM
RE: Mult-threading and locking file - by Larz60+ - Oct-15-2021, 06:06 PM
RE: Mult-threading and locking file - by mr_byte31 - Oct-15-2021, 06:50 PM
RE: Mult-threading and locking file - by Larz60+ - Oct-16-2021, 01:54 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Concurrent futures threading running at same speed as non-threading billykid999 13 1,866 May-03-2023, 08:22 AM
Last Post: billykid999
  Tutorials on sockets, threading and multi-threading? muzikman 2 2,131 Oct-01-2021, 08:32 PM
Last Post: muzikman
  File access / locking. MuntyScruntfundle 1 2,152 Oct-16-2018, 02:41 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020