当前位置: 首页>>代码示例>>Python>>正文


Python shelve.Shelf方法代码示例

本文整理汇总了Python中shelve.Shelf方法的典型用法代码示例。如果您正苦于以下问题:Python shelve.Shelf方法的具体用法?Python shelve.Shelf怎么用?Python shelve.Shelf使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在shelve的用法示例。


在下文中一共展示了shelve.Shelf方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_mutable_entry

# 需要导入模块: import shelve [as 别名]
# 或者: from shelve import Shelf [as 别名]
def test_mutable_entry(self):
        d1 = {}
        s = shelve.Shelf(d1, protocol=2, writeback=False)
        s['key1'] = [1,2,3,4]
        self.assertEqual(s['key1'], [1,2,3,4])
        s['key1'].append(5)
        self.assertEqual(s['key1'], [1,2,3,4])
        s.close()

        d2 = {}
        s = shelve.Shelf(d2, protocol=2, writeback=True)
        s['key1'] = [1,2,3,4]
        self.assertEqual(s['key1'], [1,2,3,4])
        s['key1'].append(5)
        self.assertEqual(s['key1'], [1,2,3,4,5])
        s.close()

        self.assertEqual(len(d1), 1)
        self.assertEqual(len(d2), 1) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:21,代码来源:test_shelve.py

示例2: test_mutable_entry

# 需要导入模块: import shelve [as 别名]
# 或者: from shelve import Shelf [as 别名]
def test_mutable_entry(self):
        d1 = byteskeydict()
        s = shelve.Shelf(d1, protocol=2, writeback=False)
        s['key1'] = [1,2,3,4]
        self.assertEqual(s['key1'], [1,2,3,4])
        s['key1'].append(5)
        self.assertEqual(s['key1'], [1,2,3,4])
        s.close()

        d2 = byteskeydict()
        s = shelve.Shelf(d2, protocol=2, writeback=True)
        s['key1'] = [1,2,3,4]
        self.assertEqual(s['key1'], [1,2,3,4])
        s['key1'].append(5)
        self.assertEqual(s['key1'], [1,2,3,4,5])
        s.close()

        self.assertEqual(len(d1), 1)
        self.assertEqual(len(d2), 1) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:21,代码来源:test_shelve.py

示例3: __init__

# 需要导入模块: import shelve [as 别名]
# 或者: from shelve import Shelf [as 别名]
def __init__(self, save):
        self.save = save

        try:
            self.db_experiences = shelve.Shelf(
                LMDBDict("data/commit_experiences.lmdb", readonly=not save),
                protocol=pickle.DEFAULT_PROTOCOL,
                writeback=save,
            )
        except lmdb.Error as e:
            if not save and "No such file or directory" in str(e):
                self.db_experiences = {}
            else:
                raise

        if not save:
            self.mem_experiences = {} 
开发者ID:mozilla,项目名称:bugbug,代码行数:19,代码来源:repository.py

示例4: create_shelf_multi_csv

# 需要导入模块: import shelve [as 别名]
# 或者: from shelve import Shelf [as 别名]
def create_shelf_multi_csv(self, uris, key_col, dialect):
        # sanity check inputs
        assert uris is not None
        assert len(uris) > 0

        # Shelve creates a file with specific database. Using a temp file requires a workaround to open it.
        # dumbdbm creates an empty database file. In this way shelve can open it properly.

        #note: this file is never deleted!
        filename = tempfile.NamedTemporaryFile(delete=True).name
        shelf = shelve.Shelf(dict=dbm.open(filename, 'n'))
        for uri in uris:
            with URLZSource(uri).open() as f_obj:
                f_obj = codecs.getreader("utf-8")(f_obj)
                for row in csv.DictReader(f_obj, dialect=dialect):
                    key_value = row[key_col]
                    key = self.str_hook(key_value)
                    if key is not None:
                        row_dict = dict(row)
                        del row_dict[key_col]
                        existing = shelf.get(key,[])
                        existing.append(row_dict)
                        shelf[key] = existing
        return shelf 
开发者ID:opentargets,项目名称:data_pipeline,代码行数:26,代码来源:Drug.py

示例5: create_shelf_csv

# 需要导入模块: import shelve [as 别名]
# 或者: from shelve import Shelf [as 别名]
def create_shelf_csv(self, uris, key_col, dialect):
        # sanity check inputs
        assert uris is not None
        assert len(uris) > 0

        # Shelve creates a file with specific database. Using a temp file requires a workaround to open it.
        # dumbdbm creates an empty database file. In this way shelve can open it properly.

        #note: this file is never deleted!
        filename = tempfile.NamedTemporaryFile(delete=True).name
        shelf = shelve.Shelf(dict=dbm.open(filename, 'n'))
        for uri in uris:
            with URLZSource(uri).open() as f_obj:
                f_obj = codecs.getreader("utf-8")(f_obj)
                for row in csv.DictReader(f_obj, dialect=dialect):
                    key_value = row[key_col]
                    key = self.str_hook(key_value)
                    if key is not None:
                        if key in shelf:
                            raise ValueError("Duplicate key %s in uri %s" % (key,uri))
                        row_dict = dict(row)
                        del row_dict[key_col]
                        shelf[key] = row_dict
        return shelf 
开发者ID:opentargets,项目名称:data_pipeline,代码行数:26,代码来源:Drug.py

示例6: populate_molecules_dict

# 需要导入模块: import shelve [as 别名]
# 或者: from shelve import Shelf [as 别名]
def populate_molecules_dict(self):
        # Shelve creates a file with specific database. Using a temp file requires a workaround to open it.
        t_filename = tempfile.NamedTemporaryFile(delete=True).name
        # dbm could not work: Eg. dbm.error: cannot add item.
        # Use dumbdbm for the local execution. Python 3 should fix this issue.
        dumb_dict = dbm.open(t_filename, 'n')
        shelve_out = shelve.Shelf(dict=dumb_dict)
        for uri in self.molecule_uri:
            self._logger.debug('ChEMBL getting Molecule from %s', uri)
            with URLZSource(uri).open() as f_obj:
                for line in f_obj:
                    #TODO handle malformed JSON lines better
                    mol = json.loads(line)
                    shelve_out[str(mol["molecule_chembl_id"])] = mol

        self._logger.debug('ChEMBL Molecule loading done.')
        return shelve_out 
开发者ID:opentargets,项目名称:data_pipeline,代码行数:19,代码来源:chembl_lookup.py

示例7: test_close

# 需要导入模块: import shelve [as 别名]
# 或者: from shelve import Shelf [as 别名]
def test_close(self):
        d1 = {}
        s = shelve.Shelf(d1, protocol=2, writeback=False)
        s['key1'] = [1,2,3,4]
        self.assertEqual(s['key1'], [1,2,3,4])
        self.assertEqual(len(s), 1)
        s.close()
        self.assertRaises(ValueError, len, s)
        try:
            s['key1']
        except ValueError:
            pass
        else:
            self.fail('Closed shelf should not find a key') 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:16,代码来源:test_shelve.py

示例8: test_in_memory_shelf

# 需要导入模块: import shelve [as 别名]
# 或者: from shelve import Shelf [as 别名]
def test_in_memory_shelf(self):
        d1 = {}
        s = shelve.Shelf(d1, protocol=0)
        s['key1'] = (1,2,3,4)
        self.assertEqual(s['key1'], (1,2,3,4))
        s.close()
        d2 = {}
        s = shelve.Shelf(d2, protocol=1)
        s['key1'] = (1,2,3,4)
        self.assertEqual(s['key1'], (1,2,3,4))
        s.close()

        self.assertEqual(len(d1), 1)
        self.assertNotEqual(d1, d2) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:16,代码来源:test_shelve.py

示例9: test_writeback_also_writes_immediately

# 需要导入模块: import shelve [as 别名]
# 或者: from shelve import Shelf [as 别名]
def test_writeback_also_writes_immediately(self):
        # Issue 5754
        d = {}
        s = shelve.Shelf(d, writeback=True)
        s['key'] = [1]
        p1 = d['key']  # Will give a KeyError if backing store not updated
        s['key'].append(2)
        s.close()
        p2 = d['key']
        self.assertNotEqual(p1, p2)  # Write creates new object in store 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:12,代码来源:test_shelve.py

示例10: _empty_mapping

# 需要导入模块: import shelve [as 别名]
# 或者: from shelve import Shelf [as 别名]
def _empty_mapping(self):
        if self._in_mem:
            x= shelve.Shelf({}, **self._args)
        else:
            self.counter+=1
            x= shelve.open(self.fn+str(self.counter), **self._args)
        self._db.append(x)
        return x 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:10,代码来源:test_shelve.py

示例11: __enter__

# 需要导入模块: import shelve [as 别名]
# 或者: from shelve import Shelf [as 别名]
def __enter__(self):
        assert(not self.is_open)
        self.is_open = True
        self._ui.debug('ENTER CALLED ON RUNCONTEXT')
        raw_db = dumb_dbm.open(self.file_context.file_name)
        self.db = shelve.Shelf(raw_db, writeback=True)
        if not hasattr(self, 'partitions'):
            self.partitions = []
        return self 
开发者ID:datarobot,项目名称:batch-scoring,代码行数:11,代码来源:writer.py

示例12: test_in_memory_shelf

# 需要导入模块: import shelve [as 别名]
# 或者: from shelve import Shelf [as 别名]
def test_in_memory_shelf(self):
        d1 = byteskeydict()
        s = shelve.Shelf(d1, protocol=0)
        s['key1'] = (1,2,3,4)
        self.assertEqual(s['key1'], (1,2,3,4))
        s.close()
        d2 = byteskeydict()
        s = shelve.Shelf(d2, protocol=1)
        s['key1'] = (1,2,3,4)
        self.assertEqual(s['key1'], (1,2,3,4))
        s.close()

        self.assertEqual(len(d1), 1)
        self.assertEqual(len(d2), 1)
        self.assertNotEqual(d1.items(), d2.items()) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:17,代码来源:test_shelve.py


注:本文中的shelve.Shelf方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。