def __init__(self, dbfn, default_encoding='utf-8', keep_order=False,
                 pragmas=constants.default_pragmas,
                 sort_attribute_values=False,
                 text_factory=sqlite3.OptimizedUnicode):
        """
        Connect to a database created by :func:`gffutils.create_db`.

        Parameters
        ----------

        dbfn : str

            Path to a database created by :func:`gffutils.create_db`.

        text_factory : callable

            Optionally set the way sqlite3 handles strings.  Default is
            sqlite3.OptimizedUnicode, which returns ascii when possible,
            unicode otherwise

        encoding : str

            When non-ASCII characters are encountered, assume they are in this
            encoding.

        keep_order : bool

            If True, all features returned from this instance will have the
            order of their attributes maintained.  This can be turned on or off
            database-wide by setting the `keep_order` attribute or with this
            kwarg, or on a feature-by-feature basis by setting the `keep_order`
            attribute of an individual feature.

            Default is False, since this includes a sorting step that can get
            time-consuming for many features.

        pragmas : dict
            Dictionary of pragmas to use when connecting to the database.  See
            http://www.sqlite.org/pragma.html for the full list of
            possibilities, and constants.default_pragmas for the defaults.
            These can be changed later using the :meth:`FeatureDB.set_pragmas`
            method.

        Notes
        -----

            `dbfn` can also be a subclass of :class:`_DBCreator`, useful for
            when :func:`gffutils.create_db` is provided the ``dbfn=":memory:"``
            kwarg.

        """
        # Since specifying the string ":memory:" will actually try to connect
        # to a new, separate (and empty) db in memory, we can alternatively
        # pass in a sqlite connection instance to use its existing, in-memory
        # db.
        from gffutils import create
        if isinstance(dbfn, create._DBCreator):
            self.conn = dbfn.conn
            self.dbfn = dbfn.dbfn

        elif isinstance(dbfn, sqlite3.Connection):
            self.conn = dbfn
            self.dbfn = dbfn
        # otherwise assume dbfn is a string.
        elif dbfn == ':memory:':
            raise ValueError(
                "cannot connect to memory db; please provide the connection")
        else:
            if not os.path.exists(dbfn):
                raise ValueError("Database file %s does not exist" % dbfn)
            self.dbfn = dbfn
            self.conn = sqlite3.connect(self.dbfn)

        if text_factory is not None:
            self.conn.text_factory = text_factory
        self.conn.row_factory = sqlite3.Row

        self.default_encoding = default_encoding
        self.keep_order = keep_order
        self.sort_attribute_values = sort_attribute_values
        c = self.conn.cursor()

        # Load some meta info
        # TODO: this is a good place to check for previous versions, and offer
        # to upgrade...
        c.execute(
            '''
            SELECT version, dialect FROM meta
            ''')
        version, dialect = c.fetchone()
        self.version = version
        self.dialect = helpers._unjsonify(dialect)

        # Load directives from db
        c.execute(
            '''
            SELECT directive FROM directives
            ''')
        self.directives = [directive[0] for directive in c if directive]

        # Load autoincrements so that when we add new features, we can start
        # autoincrementing from where we last left off (instead of from 1,
        # which would cause name collisions)
        c.execute(
            '''
            SELECT base, n FROM autoincrements
            ''')
        self._autoincrements = dict(c)

        self.set_pragmas(pragmas)

        if not self._analyzed():
            warnings.warn(
                "It appears that this database has not had the ANALYZE "
                "sqlite3 command run on it. Doing so can dramatically "
                "speed up queries, and is done by default for databases "
                "created with gffutils >0.8.7.1 (this database was "
                "created with version %s) Consider calling the analyze() "
                "method of this object." % self.version)
def __init__ ( self , dbfn , default_encoding = 'utf-8' , keep_order = False , pragmas = constants . default_pragmas , sort_attribute_values = False , text_factory = sqlite3 . OptimizedUnicode ) : from gffutils import create if isinstance ( dbfn , create . _DBCreator ) : self . conn = dbfn . conn self . dbfn = dbfn . dbfn elif isinstance ( dbfn , sqlite3 . Connection ) : self . conn = dbfn self . dbfn = dbfn elif dbfn == ':memory:' : raise ValueError ( "cannot connect to memory db; please provide the connection" ) else : if not os . path . exists ( dbfn ) : raise ValueError ( "Database file %s does not exist" % dbfn ) self . dbfn = dbfn self . conn = sqlite3 . connect ( self . dbfn ) if text_factory is not None : self . conn . text_factory = text_factory self . conn . row_factory = sqlite3 . Row self . default_encoding = default_encoding self . keep_order = keep_order self . sort_attribute_values = sort_attribute_values c = self . conn . cursor ( ) c . execute ( '''
            SELECT version, dialect FROM meta
            ''' ) version , dialect = c . fetchone ( ) self . version = version self . dialect = helpers . _unjsonify ( dialect ) c . execute ( '''
            SELECT directive FROM directives
            ''' ) self . directives = [ directive [ 0 ] for directive in c if directive ] c . execute ( '''
            SELECT base, n FROM autoincrements
            ''' ) self . _autoincrements = dict ( c ) self . set_pragmas ( pragmas ) if not self . _analyzed ( ) : warnings . warn ( "It appears that this database has not had the ANALYZE " "sqlite3 command run on it. Doing so can dramatically " "speed up queries, and is done by default for databases " "created with gffutils >0.8.7.1 (this database was " "created with version %s) Consider calling the analyze() " "method of this object." % self . version )
Connect to a database created by :func:`gffutils.create_db`.

        Parameters
        ----------

        dbfn : str

            Path to a database created by :func:`gffutils.create_db`.

        text_factory : callable

            Optionally set the way sqlite3 handles strings.  Default is
            sqlite3.OptimizedUnicode, which returns ascii when possible,
            unicode otherwise

        encoding : str

            When non-ASCII characters are encountered, assume they are in this
            encoding.

        keep_order : bool

            If True, all features returned from this instance will have the
            order of their attributes maintained.  This can be turned on or off
            database-wide by setting the `keep_order` attribute or with this
            kwarg, or on a feature-by-feature basis by setting the `keep_order`
            attribute of an individual feature.

            Default is False, since this includes a sorting step that can get
            time-consuming for many features.

        pragmas : dict
            Dictionary of pragmas to use when connecting to the database.  See
            http://www.sqlite.org/pragma.html for the full list of
            possibilities, and constants.default_pragmas for the defaults.
            These can be changed later using the :meth:`FeatureDB.set_pragmas`
            method.

        Notes
        -----

            `dbfn` can also be a subclass of :class:`_DBCreator`, useful for
            when :func:`gffutils.create_db` is provided the ``dbfn=":memory:"``
            kwarg.