def test_bytes(self): from stuf.six import u, b self.assertEqual( self.mclass( [1], True, r't', b('i'), u('g'), None, (1,) ).bytes().oneach().peek(), [b('[1]'), b('True'), b('t'), b('i'), b('g'), b('None'), b('(1,)')] )
def optimize(obj, d=dumps, p=protocol, s=set, q=deque, g=genops): ''' Optimize a pickle string by removing unused PUT opcodes. Raymond Hettinger Python cookbook recipe # 545418 ''' # set of args used by a GET opcode this = d(obj, p) gets = s() gadd = gets.add # (arg, startpos, stoppos) for the PUT opcodes puts = q() pappend = puts.append # set to pos if previous opcode was a PUT prevpos, prevarg = None, None for opcode, arg, pos in genops(this): if prevpos is not None: pappend((prevarg, prevpos, pos)) prevpos = None if 'PUT' in opcode.name: prevarg, prevpos = arg, pos elif 'GET' in opcode.name: gadd(arg) # Copy the pickle string except for PUTS without a corresponding GET s = q() sappend = s.append i = 0 for arg, start, stop in puts: sappend(this[i:stop if (arg in gets) else start]) i = stop sappend(this[i:]) return b('').join(s)
def __delitem__(self, key): del self._store[b(key)] self.sync()
def __setitem__(self, key, value): self._store[b(key)] = self.dumps(value) self.sync()
def test_unicode(self): from stuf.six import u, b, tounicode self.assertEqual( [tounicode(i) for i in [[1], True, r't', b('i'), u('g'), None, (1,)]], [u('[1]'), u('True'), u('t'), u('i'), u('g'), u('None'), u('(1,)')] )
def test_ascii(self): from stuf.six import u, b, tobytes self.assertEqual( [tobytes(i, 'ascii') for i in [[1], True, r't', b('i'), u('g'), None, (1,)]], [b('[1]'), b('True'), b('t'), b('i'), b('g'), b('None'), b('(1,)')] )