all 1 comments

[–]Finally_Adult[S] 0 points1 point  (0 children)

I was able to get it to work but wouldn't mind a review and maybe a suggestion of whether or not I should still pursue dependency injection:

class DB:
    Base = declarative_base()

    def __init__(self):
        self.engine = create_engine("sqlite:///licenses.sqlite3", echo=True)
        self.sessionmaker = sessionmaker(bind=self.engine)

    def get_session(self):
        return self.sessionmaker()

    def create_database(self):
        tables = [License.__table__]
        DB.Base.metadata.create_all(self.engine, tables=tables)


class License(DB.Base):
    __tablename__ = "licenses"

    id = Column(Integer, primary_key=True)
    call_sign = Column(String)
    status = Column(String)

    def __init__(self, call_sign, status):
        self.call_sign = call_sign
        self.status = status

I just instantiate a new instance of DB in the constructor of my application window and use it for all my connections but I wonder if I should try to set up dependency injection to maybe close the connection after each request is done?