Next / Previous / Contents / Shipman's homepage

17.4. FileData.__init__()

The first job of the constructor is to create a named temporary file to hold the SQLite database. The tempfile.NamedTemporaryFile() function returns an object whose .name attribute is the absolute pathname of the file.

It is conceivable that this file might show up in the script's output, but this is unlikely for two reasons: the file probably won't be very big during the file survey, and most users will not be looking in /tmp or /var where this module tends to put the file.

deduper
# - - -   F i l e D a t a . _ _ i n i t _ _

    def __init__(self, minSize):
        '''Connect to the database.
        '''
        #-- 1
        # [ self.minSize  :=  minSize
        #   tempName  :=  the name of a named temporary file that will
        #       be removed when the script terminates ]
        self.minSize = minSize
        tempName = tempfile.NamedTemporaryFile().name

Next we create the sqlalchemy.engine.Engine instance representing the database engine. In general, the argument to engine.create_engine() is a Data Source Name (DSN). In the case of SQLite, the DSN has the form “sqlite:///path”, where path is the path name to the single file where SQLite will put the database. In the case where path is an absolute path name, the resulting DSN will have four slashes in a row.

deduper
        #-- 2
        # [ self._engine  :=  an sqlalchemy.engine.Engine instance
        #       representing an SQLite database located at (tempName)
        #   FileData.meta  :=  FileData.meta bound to that Engine ]
        dsn = "sqlite:///{0}".format(tempName)
        self._engine = engine.create_engine(dsn)
        FileData.meta.bind = self._engine

Finally, we create the session class and its instance self.s, and create the table. The autocommit=False option configures the database to commit only on an explicit method call to self.s.commit().

deduper
        #-- 3
        # [ self._Session  :=  a class that creates sessions in self
        #   self.s  :=  an instance of that class
        #   self  :=  self with all tables created ]
        self._Session = orm.sessionmaker(bind=self._engine,
            autoflush=True, autocommit=False, expire_on_commit=True)
        self.s = self._Session()
        self.meta.create_all()