def __init__()

in asfpy/db.py [0:0]


    def __init__(self, fname, yaml_fname=None, yaml_section='queries'):

        def row_factory(cursor, row):
            "Return an EasyDict of the row, for attribute access."
            return easydict.EasyDict(sqlite3.Row(cursor, row))

        # Note: isolation_level=None means autocommit mode.
        self.conn = sqlite3.connect(fname, isolation_level=None)
        self.conn.row_factory = row_factory

        # If a YAML file containing SQL queries is presented, then create
        # cursors for each statement, and storing those cursors as (named)
        # attributes on SELF, for later use by the calling application.
        if yaml_fname:
            # Note: this could be a general configuration file for the
            # application. We'll look at just one section of it.
            yml = yaml.safe_load(open(yaml_fname))

            # The YAML_SECTION (default "queries") should have names of
            # queries, to use as attributes, and the value should be the
            # SQL statement/query.
            for name, sql in yml.get(yaml_section, { }).items():
                if hasattr(self, name):
                    ### ValueError seems to be the bog standard for duplicates
                    raise ValueError(f'duplicate: {name}')
                #print(f'{name}: {sql}')
                setattr(self, name, self.cursor_for(sql))