How to use sqlitedict - 10 common examples

To help you get started, we’ve selected a few sqlitedict examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github RaRe-Technologies / sqlitedict / tests / test_sqlite_migration.py View on Github external
def setUp(self):
        self.old_file = norm_file(
            os.path.join(os.path.dirname(__file__),
                         'db', 'migrate.sqlite'))
        self.new_file = norm_file(
            os.path.join(os.path.dirname(__file__),
                         'db', 'migrate_new.sqlite'))
        self.table_name = 'unnamed'
        MAKE_TABLE = 'CREATE TABLE IF NOT EXISTS %s (key TEXT PRIMARY KEY, value BLOB)' % self.table_name
        self.conn_old = sqlitedict.SqliteMultithread(self.old_file, autocommit=True, journal_mode="DELETE")
        self.conn_old.execute(MAKE_TABLE)
        self.conn_old.commit()
        self.conn_new = sqlitedict.SqliteMultithread(self.new_file, autocommit=True, journal_mode="DELETE")
        self.conn_new.execute(MAKE_TABLE)
        self.conn_new.commit()
github RaRe-Technologies / sqlitedict / tests / test_sqlite_migration.py View on Github external
def setUp(self):
        self.old_file = norm_file(
            os.path.join(os.path.dirname(__file__),
                         'db', 'migrate.sqlite'))
        self.new_file = norm_file(
            os.path.join(os.path.dirname(__file__),
                         'db', 'migrate_new.sqlite'))
        self.table_name = 'unnamed'
        MAKE_TABLE = 'CREATE TABLE IF NOT EXISTS %s (key TEXT PRIMARY KEY, value BLOB)' % self.table_name
        self.conn_old = sqlitedict.SqliteMultithread(self.old_file, autocommit=True, journal_mode="DELETE")
        self.conn_old.execute(MAKE_TABLE)
        self.conn_old.commit()
        self.conn_new = sqlitedict.SqliteMultithread(self.new_file, autocommit=True, journal_mode="DELETE")
        self.conn_new.execute(MAKE_TABLE)
        self.conn_new.commit()
github RaRe-Technologies / sqlitedict / tests / test_core.py View on Github external
def test_terminate_instead_close(self):
        ''' make terminate() instead of close()
        '''
        d = sqlitedict.open('tests/db/sqlitedict-terminate.sqlite')
        d['abc'] = 'def'
        d.commit()
        self.assertEqual(d['abc'], 'def')
        d.terminate()
        self.assertFalse(os.path.isfile('tests/db/sqlitedict-terminate.sqlite'))
github RaRe-Technologies / sqlitedict / tests / test_utils.py View on Github external
def test_terminate_instead_close(self):
        ''' make terminate() instead of close()
        '''
        d = sqlitedict.open('tests/db/sqlitedict-terminate.sqlite')
        d['abc'] = 'def'
        d.commit()
        self.assertEqual(d['abc'], 'def')
        d.terminate()
        self.assertFalse(os.path.isfile('tests/db/sqlitedict-terminate.sqlite'))
github OpenMDAO / OpenMDAO1 / mpitest / test_sqlite.py View on Github external
def assertIterationDataRecorded(self, expected, tolerance, root):
        if self.comm.rank != 0:
            return

        db = SqliteDict(self.filename, self.tablename_iterations)
        _assertIterationDataRecorded(self, db, expected, tolerance)
        db.close()
github RaRe-Technologies / sqlitedict / tests / test_sqlite_migration.py View on Github external
def test_migrate(self):
        # adding manually items with plain text keys
        ADD_ITEM = 'REPLACE INTO %s (key, value) VALUES (?,?)' % self.table_name
        self.conn_old.execute(ADD_ITEM, (b"abcd", sqlitedict.encode(u"abcd")))
        ADD_ITEM = 'REPLACE INTO %s (key, value) VALUES (?,?)' % self.table_name
        self.conn_old.execute(ADD_ITEM, (b"xyz", sqlitedict.encode(24)))
        ADD_ITEM = 'REPLACE INTO %s (key, value) VALUES (?,?)' % self.table_name
        self.conn_old.execute(ADD_ITEM, (u"+ěščřžýáíé%123456789úů".encode('utf8'), sqlitedict.encode(u"special")))
        # migrating the DB
        sqlite_migration.migrate(self.table_name, self.conn_old, self.conn_new)
        # checking migrated DB via sqlitedict
        d = sqlitedict.SqliteDict(filename=self.new_file, tablename=self.table_name, autocommit=True)
        self.assertEqual(d[b"abcd"], u"abcd")
        self.assertEqual(d[b"xyz"], 24)
        # jquast: please note, previously when db[u"unicode"] was stored, it
        # was returned as utf-8 encoded bytes.  Going forward, it will store
        # as u"unicode", so any dependent code must make its own further
        # migration to re-encode all key-as-bytes as key-as-unicode.
        self.assertEqual(d[u"+ěščřžýáíé%123456789úů".encode('utf8')], u"special")
        d.terminate()
github RaRe-Technologies / sqlitedict / tests / test_sqlite_migration.py View on Github external
def test_migrate(self):
        # adding manually items with plain text keys
        ADD_ITEM = 'REPLACE INTO %s (key, value) VALUES (?,?)' % self.table_name
        self.conn_old.execute(ADD_ITEM, (b"abcd", sqlitedict.encode(u"abcd")))
        ADD_ITEM = 'REPLACE INTO %s (key, value) VALUES (?,?)' % self.table_name
        self.conn_old.execute(ADD_ITEM, (b"xyz", sqlitedict.encode(24)))
        ADD_ITEM = 'REPLACE INTO %s (key, value) VALUES (?,?)' % self.table_name
        self.conn_old.execute(ADD_ITEM, (u"+ěščřžýáíé%123456789úů".encode('utf8'), sqlitedict.encode(u"special")))
        # migrating the DB
        sqlite_migration.migrate(self.table_name, self.conn_old, self.conn_new)
        # checking migrated DB via sqlitedict
        d = sqlitedict.SqliteDict(filename=self.new_file, tablename=self.table_name, autocommit=True)
        self.assertEqual(d[b"abcd"], u"abcd")
        self.assertEqual(d[b"xyz"], 24)
        # jquast: please note, previously when db[u"unicode"] was stored, it
        # was returned as utf-8 encoded bytes.  Going forward, it will store
        # as u"unicode", so any dependent code must make its own further
        # migration to re-encode all key-as-bytes as key-as-unicode.
        self.assertEqual(d[u"+ěščřžýáíé%123456789úů".encode('utf8')], u"special")
        d.terminate()
github RaRe-Technologies / sqlitedict / tests / test_sqlite_migration.py View on Github external
def setUp(self):
        # create database of v1.2 values
        self.filename = norm_file(
            os.path.join(os.path.dirname(__file__),
                         'db', 'unmigrated.sqlite'))
        self.db = sqlitedict.SqliteDict(filename=self.filename, flag='n')
        ADD_ITEM = 'REPLACE INTO %s (key, value) VALUES (?,?)' % self.db.tablename
        keyval = (b"bytes", sqlitedict.encode(('some', 'value')))
        self.db.conn.execute(ADD_ITEM, keyval)
github RaRe-Technologies / sqlitedict / tests / test_sqlite_migration.py View on Github external
def test_migrate(self):
        # adding manually items with plain text keys
        ADD_ITEM = 'REPLACE INTO %s (key, value) VALUES (?,?)' % self.table_name
        self.conn_old.execute(ADD_ITEM, (b"abcd", sqlitedict.encode(u"abcd")))
        ADD_ITEM = 'REPLACE INTO %s (key, value) VALUES (?,?)' % self.table_name
        self.conn_old.execute(ADD_ITEM, (b"xyz", sqlitedict.encode(24)))
        ADD_ITEM = 'REPLACE INTO %s (key, value) VALUES (?,?)' % self.table_name
        self.conn_old.execute(ADD_ITEM, (u"+ěščřžýáíé%123456789úů".encode('utf8'), sqlitedict.encode(u"special")))
        # migrating the DB
        sqlite_migration.migrate(self.table_name, self.conn_old, self.conn_new)
        # checking migrated DB via sqlitedict
        d = sqlitedict.SqliteDict(filename=self.new_file, tablename=self.table_name, autocommit=True)
        self.assertEqual(d[b"abcd"], u"abcd")
        self.assertEqual(d[b"xyz"], 24)
        # jquast: please note, previously when db[u"unicode"] was stored, it
        # was returned as utf-8 encoded bytes.  Going forward, it will store
        # as u"unicode", so any dependent code must make its own further
        # migration to re-encode all key-as-bytes as key-as-unicode.
        self.assertEqual(d[u"+ěščřžýáíé%123456789úů".encode('utf8')], u"special")
        d.terminate()
github RaRe-Technologies / sqlitedict / tests / test_core.py View on Github external
def test_tablenames(self):
        fname = norm_file('tests/db/tablenames-test-1.sqlite')
        SqliteDict(fname)
        self.assertEqual(SqliteDict.get_tablenames(fname), ['unnamed'])

        fname = norm_file('tests/db/tablenames-test-2.sqlite')
        with SqliteDict(fname,tablename='table1') as db1:
            self.assertEqual(SqliteDict.get_tablenames(fname), ['table1'])
        with SqliteDict(fname,tablename='table2') as db2:
            self.assertEqual(SqliteDict.get_tablenames(fname), ['table1','table2'])
        
        tablenames = SqliteDict.get_tablenames('tests/db/tablenames-test-2.sqlite')
        self.assertEqual(tablenames, ['table1','table2'])

sqlitedict

Persistent dict in Python, backed up by sqlite3 and pickle, multithread-safe.

Apache-2.0
Latest version published 2 years ago

Package Health Score

46 / 100
Full package analysis