async def save_long(self, obj): if self.bin: # If the int is small enough to fit in a signed 4-byte 2's-comp # format, we can store it more efficiently than the general # case. # First one- and two-byte unsigned ints: if obj >= 0: if obj <= 0xff: await self.write(BININT1 + pack("<B", obj)) return if obj <= 0xffff: await self.write(BININT2 + pack("<H", obj)) return # Next check for 4-byte signed ints: if -0x80000000 <= obj <= 0x7fffffff: await self.write(BININT + pack("<i", obj)) return if self.proto >= 2: encoded = encode_long(obj) n = len(encoded) if n < 256: await self.write(LONG1 + pack("<B", n) + encoded) else: await self.write(LONG4 + pack("<i", n) + encoded) return await self.write(LONG + repr(obj).encode("ascii") + b'L\n')
def longToBinary(l): if l == 0: return '\x00' encoded = pickle.encode_long(l) if not isinstance(encoded, unicode): encoded = unicode(encoded, encoding="utf-8") return ''.join(reversed(encoded))
def btwoc(value): """ Given some kind of integer (generally a long), this function returns the big-endian two's complement as a binary string. """ l = list(pickle.encode_long(value)) l.reverse() result = ''.join(l) log.msg('[bwtwoc] given %r, made %r' % (value, result)) return result
def longToBytes(long, bytes=4): retVal = [ord(x) for x in encode_long(long)] while len(retVal) < bytes: if abs(long) == long: retVal.append(0) else: retVal.append(0xFF) if len(retVal) > bytes: good,bad = retVal[:bytes],retVal[bytes:] for x in bad: if x: raise x86instError("Failed converting long '%s' to bytes '%s'" % \ (long, retVal)) retVal = good return tuple(retVal)
def longToBytes(long, bytes=4): retVal = [ord(x) for x in encode_long(long)] while len(retVal) < bytes: if abs(long) == long: retVal.append(0) else: retVal.append(0xFF) if len(retVal) > bytes: good, bad = retVal[:bytes], retVal[bytes:] for x in bad: if x: raise x86instError("Failed converting long '%s' to bytes '%s'" % \ (long, retVal)) retVal = good return tuple(retVal)
def _encodeInt(x, pack=struct.pack): pad = -1 if x < 0 else 0 if (x >> 7) == pad: return b'b' + pack('b', x) if (x >> 15) == pad: return b'h' + pack('h', x) if (x >> 31) == pad: return b'i' + pack('i', x) if (x >> 63) == pad: return b'q' + pack('q', x) b = encode_long(x) n = len(b) if n < 256: return b'I' + bytes([n]) + b return b'J' + pack('I', n) + b
def _encodeInt(x, pack=struct.pack): pad = -1 if x < 0 else 0 if (x >> 7) == pad: return 'b' + pack('b', x) if (x >> 15) == pad: return 'h' + pack('h', x) if (x >> 31) == pad: return 'i' + pack('i', x) if (x >> 63) == pad: return 'q' + pack('q', x) bytes = encode_long(x) n = len(bytes) if n < 256: return 'I' + chr(n) + bytes return 'J' + pack('I', n) + bytes
def longToBinary(value): if value == 0: return '\x00' return ''.join(reversed(pickle.encode_long(value)))
def _save_long(self, path, obj): array = self.file.save_array(path, str(encode_long(obj))) self.file.set_attr(array, 'pickletype', LONG)
import base64, pickle, urllib from google.appengine.ext import db from random import random segment = 1 class shared(db.Model): assocHandle = db.StringProperty() assocType = db.StringProperty() macKey = db.ByteStringProperty() btwocDec = lambda arg: pickle.decode_long(arg[::-1]) btwocEnc = lambda arg: pickle.encode_long(arg)[::-1] if arg else '\x00' base64Dec = lambda arg: btwocDec(base64.b64decode(arg.replace('-', '+').replace('_', '/'))) base64Enc = lambda arg: base64.b64encode(btwocEnc(arg)) def rndstr(length, alphabet): result = '' # Choose symbols from alphabet at random symbol = random() for _ in range(length): symbol *= len(alphabet) result += alphabet[int(symbol)] symbol -= int(symbol) return result class oneMany: def __init__(ctx, *args):
def longToBinary(l): if l == 0: return '\x00' return ''.join(reversed(pickle.encode_long(l)))
def update_event(self, inp=-1): self.set_output_val(0, pickle.encode_long(self.input(0)))
def longToBinary(l): if l == 0: return b'\x00' b = bytearray(pickle.encode_long(l)) b.reverse() return bytes(b)
new_id = self._next_id() self.logger.debug("id: %i worker_id: %i data_center_id: %i" % (new_id, self.worker_id, self.data_center_id)) return new_id class ThreadSafeSnowflake(Snowflake): def __init__(self, datacenter_id, worker_id): super(ThreadSafeSnowflake, self).__init__(datacenter_id, worker_id) self.condition = threading.Condition(threading.Lock()) def get_id(self): self.condition.acquire() try: return super(ThreadSafeSnowflake, self).get_id() finally: self.condition.release() if __name__ == "__main__": # little test import base64 from pickle import encode_long s = ThreadSafeSnowflake(0, 2) for x in range(1, 20): y = s.get_id() print y b = encode_long(y) print base64.b32encode(b) print base64.urlsafe_b64encode(b), len(base64.urlsafe_b64encode(b))
def long2bin(self, value): if value == 0: return '\x00' return ''.join(reversed(pickle.encode_long(value)))
def _write_recursive(self, obj): # type: (Pekelable) -> None if obj is None: self.f.write(NONE) elif isinstance(obj, bool): if obj is True: self.f.write(NEWTRUE) elif obj is False: self.f.write(NEWFALSE) else: raise NotPekelableError(obj) elif isinstance(obj, six.integer_types): # This basically mirrors the Python 3 implementation of save_long if 0 <= obj <= 0xff: self.f.write(BININT1 + six.int2byte(obj)) elif 0 <= obj <= 0xffff: self.f.write(BININT2 + pack('<H', obj)) elif -0x80000000 <= obj <= 0x7fffffff: self.f.write(BININT + pack('<i', obj)) else: # Use the pickle module to encode the long as a little-endian, two's complement # bytestring encoded = pickle.encode_long(obj) size = len(encoded) if size <= 0xff: self.f.write(LONG1 + six.int2byte(size) + encoded) else: # really big integer self.f.write(LONG4 + pack('<i', size) + encoded) elif isinstance(obj, bytes): size = len(obj) if size <= 0xff: self.f.write(SHORT_BINSTRING + six.int2byte(size) + obj) else: self.f.write(BINSTRING + pack('<i', size) + obj) elif isinstance(obj, six.text_type): data = obj.encode('utf-8') self.f.write(BINUNICODE + pack('<i', len(data)) + data) elif isinstance(obj, tuple): size = len(obj) if size > 3: self.f.write(MARK) for elt in obj: self._write_recursive(elt) if size == 0: self.f.write(EMPTY_TUPLE) elif size == 1: self.f.write(TUPLE1) elif size == 2: self.f.write(TUPLE2) elif size == 3: self.f.write(TUPLE3) else: self.f.write(TUPLE) elif isinstance(obj, list): if not obj: self.f.write(EMPTY_LIST) else: self.f.write(MARK) for elt in obj: self._write_recursive(elt) self.f.write(LIST) else: raise NotPekelableError(obj)
def toString(self, inObject): return pickle.encode_long(inObject)
def btwoc(val): """Return the given value in big-endian two's complement format.""" if val == 0: return '\x00' return ''.join(reversed(pickle.encode_long(val)))