def __getitem__(self, key): val = shmht.getval(self.fd, key) if val == None: raise KeyError(key) return val
def __contains__(self, key): return shmht.getval(self.fd, key) != None
#!/usr/bin/python #coding: utf-8 import shmht import time capacity = 300000 fd = shmht.open('/dev/shm/test.performance', capacity, 1) begin_time = time.time() for i in range(capacity): s = '%064d' % i shmht.setval(fd, s, s) end_time = time.time() print(capacity / (end_time - begin_time), 'iops @ set') begin_timend_time = time.time() for i in range(capacity): s = '%064d' % i if s != shmht.getval(fd, s): raise Exception(s) end_time = time.time() print(capacity / (end_time - begin_time), 'iops @ get') shmht.close(fd)
def get(self, key, default=None): val = shmht.getval(self.fd, key) if val == None: return default return val
ident = shmht.open(testfile) except shmht.error as e: pass else: assert False, 'should have raised an exception' with pycode.test('open-init'): ident = shmht.open(testfile, 10) with pycode.test('insert-lookup'): shmht.setval(ident, 'arf', 'data for arf') assert shmht.getval(ident, 'arf') == 'data for arf' shmht.setval(ident, 'narf', 'data for narf') assert shmht.getval(ident, 'narf') == 'data for narf' assert shmht.getval(ident, 'arf') == 'data for arf' assert shmht.getval(ident, 'narf') == 'data for narf' with pycode.test('iter-small'): d = {} def collect(key, value): d[key] = value shmht.foreach(ident, collect)
#!/usr/bin/python #coding: utf-8 import shmht import time capacity = 300000 fd = shmht.open('/dev/shm/test.performance', capacity, 1) begin_time = time.time() for i in range(capacity): s = bytes('%064d' % i, 'ascii') shmht.setval(fd, s, s) end_time = time.time() print(capacity / (end_time - begin_time), 'iops @ set') begin_timend_time = time.time() for i in range(capacity): s = bytes('%064d' % i, 'ascii') if s != shmht.getval(fd, s): raise Exception(s) end_time = time.time() print(capacity / (end_time - begin_time), 'iops @ get') shmht.close(fd)