當前位置: 首頁>>代碼示例>>Python>>正文


Python dbm.ndbm方法代碼示例

本文整理匯總了Python中dbm.ndbm方法的典型用法代碼示例。如果您正苦於以下問題:Python dbm.ndbm方法的具體用法?Python dbm.ndbm怎麽用?Python dbm.ndbm使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在dbm的用法示例。


在下文中一共展示了dbm.ndbm方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_keys

# 需要導入模塊: import dbm [as 別名]
# 或者: from dbm import ndbm [as 別名]
def test_keys(self):
        self.d = dbm.ndbm.open(self.filename, 'c')
        self.assertEqual(self.d.keys(), [])
        self.d['a'] = 'b'
        self.d[b'bytes'] = b'data'
        self.d['12345678910'] = '019237410982340912840198242'
        self.d.keys()
        self.assertIn('a', self.d)
        self.assertIn(b'a', self.d)
        self.assertEqual(self.d[b'bytes'], b'data')
        # get() and setdefault() work as in the dict interface
        self.assertEqual(self.d.get(b'a'), b'b')
        self.assertIsNone(self.d.get(b'xxx'))
        self.assertEqual(self.d.get(b'xxx', b'foo'), b'foo')
        with self.assertRaises(KeyError):
            self.d['xxx']
        self.assertEqual(self.d.setdefault(b'xxx', b'foo'), b'foo')
        self.assertEqual(self.d[b'xxx'], b'foo')
        self.d.close() 
開發者ID:bkerler,項目名稱:android_universal,代碼行數:21,代碼來源:test_dbm_ndbm.py

示例2: setUp

# 需要導入模塊: import dbm [as 別名]
# 或者: from dbm import ndbm [as 別名]
def setUp(self):
        self.filename = support.TESTFN
        self.d = dbm.ndbm.open(self.filename, 'c')
        self.d.close() 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:6,代碼來源:test_dbm_ndbm.py

示例3: test_keys

# 需要導入模塊: import dbm [as 別名]
# 或者: from dbm import ndbm [as 別名]
def test_keys(self):
        self.d = dbm.ndbm.open(self.filename, 'c')
        self.assertTrue(self.d.keys() == [])
        self.d['a'] = 'b'
        self.d[b'bytes'] = b'data'
        self.d['12345678910'] = '019237410982340912840198242'
        self.d.keys()
        self.assertIn('a', self.d)
        self.assertIn(b'a', self.d)
        self.assertEqual(self.d[b'bytes'], b'data')
        self.d.close() 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:13,代碼來源:test_dbm_ndbm.py

示例4: test_modes

# 需要導入模塊: import dbm [as 別名]
# 或者: from dbm import ndbm [as 別名]
def test_modes(self):
        for mode in ['r', 'rw', 'w', 'n']:
            try:
                self.d = dbm.ndbm.open(self.filename, mode)
                self.d.close()
            except error:
                self.fail() 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:9,代碼來源:test_dbm_ndbm.py

示例5: test_context_manager

# 需要導入模塊: import dbm [as 別名]
# 或者: from dbm import ndbm [as 別名]
def test_context_manager(self):
        with dbm.ndbm.open(self.filename, 'c') as db:
            db["ndbm context manager"] = "context manager"

        with dbm.ndbm.open(self.filename, 'r') as db:
            self.assertEqual(list(db.keys()), [b"ndbm context manager"])

        with self.assertRaises(dbm.ndbm.error) as cm:
            db.keys()
        self.assertEqual(str(cm.exception),
                         "DBM object has already been closed") 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:13,代碼來源:test_dbm_ndbm.py

示例6: test_whichdb_ndbm

# 需要導入模塊: import dbm [as 別名]
# 或者: from dbm import ndbm [as 別名]
def test_whichdb_ndbm(self):
        # Issue 17198: check that ndbm which is referenced in whichdb is defined
        db_file = '{}_ndbm.db'.format(_fname)
        with open(db_file, 'w'):
            self.addCleanup(test.support.unlink, db_file)
        self.assertIsNone(self.dbm.whichdb(db_file[:-3])) 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:8,代碼來源:test_dbm.py

示例7: __init__

# 需要導入模塊: import dbm [as 別名]
# 或者: from dbm import ndbm [as 別名]
def __init__(self, filename, dbm_class=dbm.ndbm, serializer: Serializer = None, clean: bool = False):
        if not serializer:
            serializer = PickleSerializer()
        self._db = dbm_class.open(filename, 'c')
        self._serializer = serializer

        if clean:
            self.clean() 
開發者ID:usc-isi-i2,項目名稱:rltk,代碼行數:10,代碼來源:dbm_key_value_adapter.py

示例8: test_empty_value

# 需要導入模塊: import dbm [as 別名]
# 或者: from dbm import ndbm [as 別名]
def test_empty_value(self):
        if dbm.ndbm.library == 'Berkeley DB':
            self.skipTest("Berkeley DB doesn't distinguish the empty value "
                          "from the absent one")
        self.d = dbm.ndbm.open(self.filename, 'c')
        self.assertEqual(self.d.keys(), [])
        self.d['empty'] = ''
        self.assertEqual(self.d.keys(), [b'empty'])
        self.assertIn(b'empty', self.d)
        self.assertEqual(self.d[b'empty'], b'')
        self.assertEqual(self.d.get(b'empty'), b'')
        self.assertEqual(self.d.setdefault(b'empty'), b'')
        self.d.close() 
開發者ID:bkerler,項目名稱:android_universal,代碼行數:15,代碼來源:test_dbm_ndbm.py

示例9: test_unicode

# 需要導入模塊: import dbm [as 別名]
# 或者: from dbm import ndbm [as 別名]
def test_unicode(self):
        with dbm.ndbm.open(self.filename, 'c') as db:
            db['Unicode key \U0001f40d'] = 'Unicode value \U0001f40d'
        with dbm.ndbm.open(self.filename, 'r') as db:
            self.assertEqual(list(db.keys()), ['Unicode key \U0001f40d'.encode()])
            self.assertTrue('Unicode key \U0001f40d'.encode() in db)
            self.assertTrue('Unicode key \U0001f40d' in db)
            self.assertEqual(db['Unicode key \U0001f40d'.encode()],
                             'Unicode value \U0001f40d'.encode())
            self.assertEqual(db['Unicode key \U0001f40d'],
                             'Unicode value \U0001f40d'.encode()) 
開發者ID:bkerler,項目名稱:android_universal,代碼行數:13,代碼來源:test_dbm_ndbm.py

示例10: test_nonascii_filename

# 需要導入模塊: import dbm [as 別名]
# 或者: from dbm import ndbm [as 別名]
def test_nonascii_filename(self):
        filename = support.TESTFN_NONASCII
        for suffix in ['', '.pag', '.dir', '.db']:
            self.addCleanup(support.unlink, filename + suffix)
        with dbm.ndbm.open(filename, 'c') as db:
            db[b'key'] = b'value'
        self.assertTrue(any(os.path.exists(filename + suffix)
                            for suffix in ['', '.pag', '.dir', '.db']))
        with dbm.ndbm.open(filename, 'r') as db:
            self.assertEqual(list(db.keys()), [b'key'])
            self.assertTrue(b'key' in db)
            self.assertEqual(db[b'key'], b'value') 
開發者ID:bkerler,項目名稱:android_universal,代碼行數:14,代碼來源:test_dbm_ndbm.py


注:本文中的dbm.ndbm方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。