Python struct.Struct() Examples

The following are code examples for showing how to use struct.Struct(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: NiujiaoDebugger   Author: MrSrc   File: test_struct.py    GNU General Public License v3.0 7 votes vote down vote up
def test_construct(self):
        def _check_iterator(it):
            self.assertIsInstance(it, abc.Iterator)
            self.assertIsInstance(it, abc.Iterable)
        s = struct.Struct('>ibcp')
        it = s.iter_unpack(b"")
        _check_iterator(it)
        it = s.iter_unpack(b"1234567")
        _check_iterator(it)
        # Wrong bytes length
        with self.assertRaises(struct.error):
            s.iter_unpack(b"123456")
        with self.assertRaises(struct.error):
            s.iter_unpack(b"12345678")
        # Zero-length struct
        s = struct.Struct('>')
        with self.assertRaises(struct.error):
            s.iter_unpack(b"")
        with self.assertRaises(struct.error):
            s.iter_unpack(b"12") 
Example 2
Project: DOTA_models   Author: ringringyi   File: text2bin.py    Apache License 2.0 6 votes vote down vote up
def go(fhs):
  fmt = None
  with open(opt_vocab, 'w') as vocab_out:
    with open(opt_output, 'w') as vecs_out:
      for lines in izip(*fhs):
        parts = [line.split() for line in lines]
        token = parts[0][0]
        if any(part[0] != token for part in parts[1:]):
          raise IOError('vector files must be aligned')

        print >> vocab_out, token

        vec = [sum(float(x) for x in xs) for xs in zip(*parts)[1:]]
        if not fmt:
          fmt = struct.Struct('%df' % len(vec))

        vecs_out.write(fmt.pack(*vec)) 
Example 3
Project: sic   Author: Yanixos   File: base64.py    GNU General Public License v3.0 6 votes vote down vote up
def _85encode(b, chars, chars2, pad=False, foldnuls=False, foldspaces=False):
    # Helper function for a85encode and b85encode
    if not isinstance(b, bytes_types):
        b = memoryview(b).tobytes()

    padding = (-len(b)) % 4
    if padding:
        b = b + b'\0' * padding
    words = struct.Struct('!%dI' % (len(b) // 4)).unpack(b)

    chunks = [b'z' if foldnuls and not word else
              b'y' if foldspaces and word == 0x20202020 else
              (chars2[word // 614125] +
               chars2[word // 85 % 7225] +
               chars[word % 85])
              for word in words]

    if padding and not pad:
        if chunks[-1] == b'z':
            chunks[-1] = chars[0] * 5
        chunks[-1] = chunks[-1][:-padding]

    return b''.join(chunks) 
Example 4
Project: bitpay-brick   Author: javgh   File: bluetoothservice.py    MIT License 6 votes vote down vote up
def read_varint32(sock):
    mask = (1 << 32) - 1
    result = 0
    shift = 0
    while True:
        unpacker = struct.Struct('! B')
        data = sock.recv(unpacker.size, socket.MSG_WAITALL)
        (b,) = unpacker.unpack(data)

        result |= ((b & 0x7f) << shift)
        if not (b & 0x80):
            result &= mask
            return result
        shift += 7
        if shift >= 64:
            raise IOError("Too many bytes when decoding varint.") 
Example 5
Project: job_manager   Author: lnls-fac   File: pyjob_server.py    MIT License 6 votes vote down vote up
def handle(self):
        InfoStruct = struct.Struct(SET_STRUCT_PARAM)
        info = self.rfile.read(InfoStruct.size)
        size, version = InfoStruct.unpack(info)
        data = pickle.loads(self.rfile.read(size))
        if version != VERSION:
            reply = (False, 'client is incompatible')
        else:
            try:
                with self.CallLock:
                    function = self.Call[data[0]]
                reply = function(self, *data[1:])
            except Finish:
                return
        data = pickle.dumps(reply, PICKLE_PROTOCOL)
        self.wfile.write(InfoStruct.pack(len(data), VERSION))
        self.wfile.write(data) 
Example 6
Project: NiujiaoDebugger   Author: MrSrc   File: base64.py    GNU General Public License v3.0 6 votes vote down vote up
def _85encode(b, chars, chars2, pad=False, foldnuls=False, foldspaces=False):
    # Helper function for a85encode and b85encode
    if not isinstance(b, bytes_types):
        b = memoryview(b).tobytes()

    padding = (-len(b)) % 4
    if padding:
        b = b + b'\0' * padding
    words = struct.Struct('!%dI' % (len(b) // 4)).unpack(b)

    chunks = [b'z' if foldnuls and not word else
              b'y' if foldspaces and word == 0x20202020 else
              (chars2[word // 614125] +
               chars2[word // 85 % 7225] +
               chars[word % 85])
              for word in words]

    if padding and not pad:
        if chunks[-1] == b'z':
            chunks[-1] = chars[0] * 5
        chunks[-1] = chunks[-1][:-padding]

    return b''.join(chunks) 
Example 7
Project: aiozk   Author: micro-fan   File: primitives.py    MIT License 6 votes vote down vote up
def parse(cls, buff, offset):
        """
        Given a buffer and offset, returns the parsed value and new offset.

        Parses the ``size_primitive`` first to determine how many more bytes to
        consume to extract the value.
        """
        size, offset = cls.size_primitive.parse(buff, offset)
        if size == -1:
            return None, offset

        var_struct = struct.Struct("!%ds" % size)

        value = var_struct.unpack_from(buff, offset)[0]
        value = cls.parse_value(value)
        offset += var_struct.size

        return value, offset 
Example 8
Project: Spyce   Author: imec-myhdl   File: pyRTPlt.py    GNU Lesser General Public License v2.1 6 votes vote down vote up
def run(self):
        self.sock, self.connection, self.client_address = sk.open_server(self.mainw.server_address)

        strdata=str(self.N+1)+'d'

        while self.mainw.ServerActive==1:
            data = self.connection.recv(8*(self.N+1))
            if data:
                s = struct.Struct(strdata)
                udata = s.unpack(data)

                self.mainw.timebase.append(udata[0]);
                if len(self.mainw.timebase) > self.mainw.Hist:
                    self.mainw.timebase = self.mainw.timebase[-self.mainw.Hist:]
                for n in range(0,self.N):
                    self.mainw.x[n].append(udata[n+1])
                    if len(self.mainw.x[n]) > self.mainw.Hist:
                        self.mainw.x[n] = self.mainw.x[n][-self.mainw.Hist:] 
Example 9
Project: T-Home   Author: TD22057   File: Reply.py    BSD 2-Clause "Simplified" License 6 votes vote down vote up
def decodeItem( self, obj, bytes, offset ):
      self.struct.unpack( self, bytes, offset )
      offset += len( self.struct )

      # Little endian, 4 individual unsigned bytes
      s = struct.Struct( '<BBBB' )

      for i in range( self._numAttr ):
         # x[0], x[1], x[2] are the attribute code
         # x[3] == 1 if the attribute is active or 0 if not
         # Only 1 active attribute per block
         x = s.unpack_from( bytes, offset )
         if x[3] == 1:
            self.code = x[2]*65536 + x[1]*256 + x[0]
            self.value = tags.values.get( self.code, "Unknown" )
            break
         
         offset += s.size
         
      return BaseItem.decodeBase( self, obj, self.value )
   
#============================================================================== 
Example 10
Project: T-Home   Author: TD22057   File: NamedStruct.py    BSD 2-Clause "Simplified" License 6 votes vote down vote up
def __init__( self, endian, elems ):
      """Constructr
      
      endian == BIG_ENDIAN or LITTLE_ENDIAN
      elems = [ ( struct_format_code, name ), ... ]
      """
      assert( endian == "BIG_ENDIAN" or endian == "LITTLE_ENDIAN" )
      
      if endian == "BIG_ENDIAN":
         self.format = ">"
      elif endian == "LITTLE_ENDIAN":
         self.format = "<"

      self.format += "".join( [ i[0] for i in elems ] )
      self.names = [ i[1] for i in elems ]

      self.struct = struct.Struct( self.format )

   #--------------------------------------------------------------------------- 
Example 11
Project: pyblish-win   Author: pyblish   File: test_struct.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_unpack_from(self, cls=str):
        data = cls('abcd01234')
        fmt = '4s'
        s = struct.Struct(fmt)

        self.assertEqual(s.unpack_from(data), ('abcd',))
        self.assertEqual(struct.unpack_from(fmt, data), ('abcd',))
        for i in xrange(6):
            self.assertEqual(s.unpack_from(data, i), (data[i:i+4],))
            self.assertEqual(struct.unpack_from(fmt, data, i), (data[i:i+4],))
        for i in xrange(6, len(data) + 1):
            self.assertRaises(struct.error, s.unpack_from, data, i)
            self.assertRaises(struct.error, struct.unpack_from, fmt, data, i) 
Example 12
Project: pyblish-win   Author: pyblish   File: test_struct.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_pack_into(self):
        test_string = 'Reykjavik rocks, eow!'
        writable_buf = array.array('c', ' '*100)
        fmt = '21s'
        s = struct.Struct(fmt)

        # Test without offset
        s.pack_into(writable_buf, 0, test_string)
        from_buf = writable_buf.tostring()[:len(test_string)]
        self.assertEqual(from_buf, test_string)

        # Test with offset.
        s.pack_into(writable_buf, 10, test_string)
        from_buf = writable_buf.tostring()[:len(test_string)+10]
        self.assertEqual(from_buf, test_string[:10] + test_string)

        # Go beyond boundaries.
        small_buf = array.array('c', ' '*10)
        self.assertRaises((ValueError, struct.error), s.pack_into, small_buf, 0,
                          test_string)
        self.assertRaises((ValueError, struct.error), s.pack_into, small_buf, 2,
                          test_string)

        # Test bogus offset (issue 3694)
        sb = small_buf
        self.assertRaises((TypeError, struct.error), struct.pack_into, b'', sb,
                          None) 
Example 13
Project: pyblish-win   Author: pyblish   File: test_struct.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def check_sizeof(self, format_str, number_of_codes):
        # The size of 'PyStructObject'
        totalsize = support.calcobjsize('5P')
        # The size taken up by the 'formatcode' dynamic array
        totalsize += struct.calcsize('3P') * (number_of_codes + 1)
        support.check_sizeof(self, struct.Struct(format_str), totalsize) 
Example 14
Project: pyblish-win   Author: pyblish   File: test_struct.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_unicode_format(self):
        try:
            unicode
        except NameError:
            self.skipTest('no unicode support')
        # Issue #19099
        s = struct.Struct(unichr(ord('I')))
        self.assertEqual(s.format, 'I')
        self.assertIs(type(s.format), str)
        self.assertRaises(ValueError, struct.Struct, unichr(0x80)) 
Example 15
Project: audio-effects   Author: Souloist   File: get-pip.py    MIT License 5 votes vote down vote up
def b85decode(b):
        _b85dec = [None] * 256
        for i, c in enumerate(iterbytes(_b85alphabet)):
            _b85dec[c] = i

        padding = (-len(b)) % 5
        b = b + b'~' * padding
        out = []
        packI = struct.Struct('!I').pack
        for i in range(0, len(b), 5):
            chunk = b[i:i + 5]
            acc = 0
            try:
                for c in iterbytes(chunk):
                    acc = acc * 85 + _b85dec[c]
            except TypeError:
                for j, c in enumerate(iterbytes(chunk)):
                    if _b85dec[c] is None:
                        raise ValueError(
                            'bad base85 character at position %d' % (i + j)
                        )
                raise
            try:
                out.append(packI(acc))
            except struct.error:
                raise ValueError('base85 overflow in hunk starting at byte %d'
                                 % i)

        result = b''.join(out)
        if padding:
            result = result[:-padding]
        return result 
Example 16
Project: InsightAgent   Author: insightfinder   File: get-pip.py    Apache License 2.0 5 votes vote down vote up
def b85decode(b):
        _b85dec = [None] * 256
        for i, c in enumerate(iterbytes(_b85alphabet)):
            _b85dec[c] = i

        padding = (-len(b)) % 5
        b = b + b'~' * padding
        out = []
        packI = struct.Struct('!I').pack
        for i in range(0, len(b), 5):
            chunk = b[i:i + 5]
            acc = 0
            try:
                for c in iterbytes(chunk):
                    acc = acc * 85 + _b85dec[c]
            except TypeError:
                for j, c in enumerate(iterbytes(chunk)):
                    if _b85dec[c] is None:
                        raise ValueError(
                            'bad base85 character at position %d' % (i + j)
                        )
                raise
            try:
                out.append(packI(acc))
            except struct.error:
                raise ValueError('base85 overflow in hunk starting at byte %d'
                                 % i)

        result = b''.join(out)
        if padding:
            result = result[:-padding]
        return result 
Example 17
Project: InsightAgent   Author: insightfinder   File: get-pip.py    Apache License 2.0 5 votes vote down vote up
def b85decode(b):
        _b85dec = [None] * 256
        for i, c in enumerate(iterbytes(_b85alphabet)):
            _b85dec[c] = i

        padding = (-len(b)) % 5
        b = b + b'~' * padding
        out = []
        packI = struct.Struct('!I').pack
        for i in range(0, len(b), 5):
            chunk = b[i:i + 5]
            acc = 0
            try:
                for c in iterbytes(chunk):
                    acc = acc * 85 + _b85dec[c]
            except TypeError:
                for j, c in enumerate(iterbytes(chunk)):
                    if _b85dec[c] is None:
                        raise ValueError(
                            'bad base85 character at position %d' % (i + j)
                        )
                raise
            try:
                out.append(packI(acc))
            except struct.error:
                raise ValueError('base85 overflow in hunk starting at byte %d'
                                 % i)

        result = b''.join(out)
        if padding:
            result = result[:-padding]
        return result 
Example 18
Project: InsightAgent   Author: insightfinder   File: get-pip.py    Apache License 2.0 5 votes vote down vote up
def b85decode(b):
        _b85dec = [None] * 256
        for i, c in enumerate(iterbytes(_b85alphabet)):
            _b85dec[c] = i

        padding = (-len(b)) % 5
        b = b + b'~' * padding
        out = []
        packI = struct.Struct('!I').pack
        for i in range(0, len(b), 5):
            chunk = b[i:i + 5]
            acc = 0
            try:
                for c in iterbytes(chunk):
                    acc = acc * 85 + _b85dec[c]
            except TypeError:
                for j, c in enumerate(iterbytes(chunk)):
                    if _b85dec[c] is None:
                        raise ValueError(
                            'bad base85 character at position %d' % (i + j)
                        )
                raise
            try:
                out.append(packI(acc))
            except struct.error:
                raise ValueError('base85 overflow in hunk starting at byte %d'
                                 % i)

        result = b''.join(out)
        if padding:
            result = result[:-padding]
        return result 
Example 19
Project: InsightAgent   Author: insightfinder   File: get-pip.py    Apache License 2.0 5 votes vote down vote up
def b85decode(b):
        _b85dec = [None] * 256
        for i, c in enumerate(iterbytes(_b85alphabet)):
            _b85dec[c] = i

        padding = (-len(b)) % 5
        b = b + b'~' * padding
        out = []
        packI = struct.Struct('!I').pack
        for i in range(0, len(b), 5):
            chunk = b[i:i + 5]
            acc = 0
            try:
                for c in iterbytes(chunk):
                    acc = acc * 85 + _b85dec[c]
            except TypeError:
                for j, c in enumerate(iterbytes(chunk)):
                    if _b85dec[c] is None:
                        raise ValueError(
                            'bad base85 character at position %d' % (i + j)
                        )
                raise
            try:
                out.append(packI(acc))
            except struct.error:
                raise ValueError('base85 overflow in hunk starting at byte %d'
                                 % i)

        result = b''.join(out)
        if padding:
            result = result[:-padding]
        return result 
Example 20
Project: InsightAgent   Author: insightfinder   File: get-pip.py    Apache License 2.0 5 votes vote down vote up
def b85decode(b):
        _b85dec = [None] * 256
        for i, c in enumerate(iterbytes(_b85alphabet)):
            _b85dec[c] = i

        padding = (-len(b)) % 5
        b = b + b'~' * padding
        out = []
        packI = struct.Struct('!I').pack
        for i in range(0, len(b), 5):
            chunk = b[i:i + 5]
            acc = 0
            try:
                for c in iterbytes(chunk):
                    acc = acc * 85 + _b85dec[c]
            except TypeError:
                for j, c in enumerate(iterbytes(chunk)):
                    if _b85dec[c] is None:
                        raise ValueError(
                            'bad base85 character at position %d' % (i + j)
                        )
                raise
            try:
                out.append(packI(acc))
            except struct.error:
                raise ValueError('base85 overflow in hunk starting at byte %d'
                                 % i)

        result = b''.join(out)
        if padding:
            result = result[:-padding]
        return result 
Example 21
Project: InsightAgent   Author: insightfinder   File: get-pip.py    Apache License 2.0 5 votes vote down vote up
def b85decode(b):
        _b85dec = [None] * 256
        for i, c in enumerate(iterbytes(_b85alphabet)):
            _b85dec[c] = i

        padding = (-len(b)) % 5
        b = b + b'~' * padding
        out = []
        packI = struct.Struct('!I').pack
        for i in range(0, len(b), 5):
            chunk = b[i:i + 5]
            acc = 0
            try:
                for c in iterbytes(chunk):
                    acc = acc * 85 + _b85dec[c]
            except TypeError:
                for j, c in enumerate(iterbytes(chunk)):
                    if _b85dec[c] is None:
                        raise ValueError(
                            'bad base85 character at position %d' % (i + j)
                        )
                raise
            try:
                out.append(packI(acc))
            except struct.error:
                raise ValueError('base85 overflow in hunk starting at byte %d'
                                 % i)

        result = b''.join(out)
        if padding:
            result = result[:-padding]
        return result 
Example 22
Project: InsightAgent   Author: insightfinder   File: get-pip.py    Apache License 2.0 5 votes vote down vote up
def b85decode(b):
        _b85dec = [None] * 256
        for i, c in enumerate(iterbytes(_b85alphabet)):
            _b85dec[c] = i

        padding = (-len(b)) % 5
        b = b + b'~' * padding
        out = []
        packI = struct.Struct('!I').pack
        for i in range(0, len(b), 5):
            chunk = b[i:i + 5]
            acc = 0
            try:
                for c in iterbytes(chunk):
                    acc = acc * 85 + _b85dec[c]
            except TypeError:
                for j, c in enumerate(iterbytes(chunk)):
                    if _b85dec[c] is None:
                        raise ValueError(
                            'bad base85 character at position %d' % (i + j)
                        )
                raise
            try:
                out.append(packI(acc))
            except struct.error:
                raise ValueError('base85 overflow in hunk starting at byte %d'
                                 % i)

        result = b''.join(out)
        if padding:
            result = result[:-padding]
        return result 
Example 23
Project: InsightAgent   Author: insightfinder   File: get-pip.py    Apache License 2.0 5 votes vote down vote up
def b85decode(b):
        _b85dec = [None] * 256
        for i, c in enumerate(iterbytes(_b85alphabet)):
            _b85dec[c] = i

        padding = (-len(b)) % 5
        b = b + b'~' * padding
        out = []
        packI = struct.Struct('!I').pack
        for i in range(0, len(b), 5):
            chunk = b[i:i + 5]
            acc = 0
            try:
                for c in iterbytes(chunk):
                    acc = acc * 85 + _b85dec[c]
            except TypeError:
                for j, c in enumerate(iterbytes(chunk)):
                    if _b85dec[c] is None:
                        raise ValueError(
                            'bad base85 character at position %d' % (i + j)
                        )
                raise
            try:
                out.append(packI(acc))
            except struct.error:
                raise ValueError('base85 overflow in hunk starting at byte %d'
                                 % i)

        result = b''.join(out)
        if padding:
            result = result[:-padding]
        return result 
Example 24
Project: InsightAgent   Author: insightfinder   File: get-pip.py    Apache License 2.0 5 votes vote down vote up
def b85decode(b):
        _b85dec = [None] * 256
        for i, c in enumerate(iterbytes(_b85alphabet)):
            _b85dec[c] = i

        padding = (-len(b)) % 5
        b = b + b'~' * padding
        out = []
        packI = struct.Struct('!I').pack
        for i in range(0, len(b), 5):
            chunk = b[i:i + 5]
            acc = 0
            try:
                for c in iterbytes(chunk):
                    acc = acc * 85 + _b85dec[c]
            except TypeError:
                for j, c in enumerate(iterbytes(chunk)):
                    if _b85dec[c] is None:
                        raise ValueError(
                            'bad base85 character at position %d' % (i + j)
                        )
                raise
            try:
                out.append(packI(acc))
            except struct.error:
                raise ValueError('base85 overflow in hunk starting at byte %d'
                                 % i)

        result = b''.join(out)
        if padding:
            result = result[:-padding]
        return result 
Example 25
Project: InsightAgent   Author: insightfinder   File: get-pip.py    Apache License 2.0 5 votes vote down vote up
def b85decode(b):
        _b85dec = [None] * 256
        for i, c in enumerate(iterbytes(_b85alphabet)):
            _b85dec[c] = i

        padding = (-len(b)) % 5
        b = b + b'~' * padding
        out = []
        packI = struct.Struct('!I').pack
        for i in range(0, len(b), 5):
            chunk = b[i:i + 5]
            acc = 0
            try:
                for c in iterbytes(chunk):
                    acc = acc * 85 + _b85dec[c]
            except TypeError:
                for j, c in enumerate(iterbytes(chunk)):
                    if _b85dec[c] is None:
                        raise ValueError(
                            'bad base85 character at position %d' % (i + j)
                        )
                raise
            try:
                out.append(packI(acc))
            except struct.error:
                raise ValueError('base85 overflow in hunk starting at byte %d'
                                 % i)

        result = b''.join(out)
        if padding:
            result = result[:-padding]
        return result 
Example 26
Project: InsightAgent   Author: insightfinder   File: get-pip.py    Apache License 2.0 5 votes vote down vote up
def b85decode(b):
        _b85dec = [None] * 256
        for i, c in enumerate(iterbytes(_b85alphabet)):
            _b85dec[c] = i

        padding = (-len(b)) % 5
        b = b + b'~' * padding
        out = []
        packI = struct.Struct('!I').pack
        for i in range(0, len(b), 5):
            chunk = b[i:i + 5]
            acc = 0
            try:
                for c in iterbytes(chunk):
                    acc = acc * 85 + _b85dec[c]
            except TypeError:
                for j, c in enumerate(iterbytes(chunk)):
                    if _b85dec[c] is None:
                        raise ValueError(
                            'bad base85 character at position %d' % (i + j)
                        )
                raise
            try:
                out.append(packI(acc))
            except struct.error:
                raise ValueError('base85 overflow in hunk starting at byte %d'
                                 % i)

        result = b''.join(out)
        if padding:
            result = result[:-padding]
        return result 
Example 27
Project: InsightAgent   Author: insightfinder   File: get-pip.py    Apache License 2.0 5 votes vote down vote up
def b85decode(b):
        _b85dec = [None] * 256
        for i, c in enumerate(iterbytes(_b85alphabet)):
            _b85dec[c] = i

        padding = (-len(b)) % 5
        b = b + b'~' * padding
        out = []
        packI = struct.Struct('!I').pack
        for i in range(0, len(b), 5):
            chunk = b[i:i + 5]
            acc = 0
            try:
                for c in iterbytes(chunk):
                    acc = acc * 85 + _b85dec[c]
            except TypeError:
                for j, c in enumerate(iterbytes(chunk)):
                    if _b85dec[c] is None:
                        raise ValueError(
                            'bad base85 character at position %d' % (i + j)
                        )
                raise
            try:
                out.append(packI(acc))
            except struct.error:
                raise ValueError('base85 overflow in hunk starting at byte %d'
                                 % i)

        result = b''.join(out)
        if padding:
            result = result[:-padding]
        return result 
Example 28
Project: Parallel.GAMIT   Author: demiangomez   File: pyStationInfo.py    GNU General Public License v3.0 5 votes vote down vote up
def parse_station_record(self, record):

        if isinstance(record, str):

            fieldnames = ['StationCode', 'StationName', 'DateStart', 'DateEnd', 'AntennaHeight', 'HeightCode',
                          'AntennaNorth', 'AntennaEast', 'ReceiverCode', 'ReceiverVers', 'ReceiverFirmware',
                          'ReceiverSerial', 'AntennaCode', 'RadomeCode', 'AntennaSerial']

            fieldwidths = (
            1, 6, 18, 19, 19, 9, 7, 9, 9, 22, 22, 7, 22, 17, 7, 20)  # negative widths represent ignored padding fields
            fmtstring = ' '.join('{}{}'.format(abs(fw), 'x' if fw < 0 else 's') for fw in fieldwidths)

            fieldstruct = struct.Struct(fmtstring)
            parse = fieldstruct.unpack_from

            if record[0] == ' ' and len(record) >= 77:
                record = dict(zip(fieldnames, map(str.strip, parse(record.ljust(fieldstruct.size))[1:])))
            else:
                return

        for key in self.keys():
            try:
                if key == 'AntennaNorth' or key == 'AntennaEast' or key == 'AntennaHeight':
                    self[key] = float(record[key])
                else:
                    self[key] = record[key]
            except KeyError:
                # if key not found in the record, may be an added field (like hash)
                pass

        try:
            # if initializing with a RINEX record, some of these may not exist in the dictionary
            self.DateStart = pyDate.Date(stninfo=record['DateStart'])
            self.DateEnd = pyDate.Date(stninfo=record['DateEnd'])
            self.StationCode = record['StationCode'].lower()
        except KeyError:
            pass 
Example 29
Project: Parallel.GAMIT   Author: demiangomez   File: pyRinex.py    GNU General Public License v3.0 5 votes vote down vote up
def read_fields(self, line, record, format_tuple):

        # create the parser object
        formatstr = re.sub(r'\..', '',' '.join(format_tuple).replace('%', '').replace('f', 's').replace('i', 's').replace('-', ''))

        fs = struct.Struct(formatstr)
        parse = fs.unpack_from

        # get the data section by spliting the line using the record text
        data = line.split(record)[0]

        if len(data) < fs.size:
            # line too short, add padding spaces
            f = '%-' + str(fs.size) + 's'
            data = f % line
        elif len(data) > fs.size:
            # line too long! cut
            data = line[0:fs.size]

        fields = list(parse(data))

        # convert each element in the list to float if necessary
        for i, field in enumerate(fields):
            if 'f' in format_tuple[i]:
                try:
                    fields[i] = float(fields[i])
                except ValueError:
                    # invalid number in the field!, replace with something harmless
                    fields[i] = float(2.11)
            elif 'i' in format_tuple[i]:
                try:
                    fields[i] = int(fields[i])
                except ValueError:
                    # invalid number in the field!, replace with something harmless
                    fields[i] = int(1)
            elif 's' in format_tuple[i]:
                fields[i] = fields[i].strip()

        return fields, data 
Example 30
Project: Parallel.GAMIT   Author: demiangomez   File: pyRinex.py    GNU General Public License v3.0 5 votes vote down vote up
def get_firstobs(self):

        if self.rinex_version < 3:
            fs = struct.Struct('1s2s1s2s1s2s1s2s1s2s11s2s1s3s')
        else:
            fs = struct.Struct('2s4s1s2s1s2s1s2s1s2s11s2s1s3s')

        parse = fs.unpack_from

        date = None

        skip = 0
        for line in self.data:
            if skip == 0:
                fields = list(parse(line))

                if int(fields[12]) <= 1: # OK FLAG
                    # read first observation
                    year = int(fields[1])
                    month = int(fields[3])
                    day = int(fields[5])
                    hour = int(fields[7])
                    minute = int(fields[9])
                    second = float(fields[10])

                    try:
                        date = pyDate.Date(year=year, month=month, day=day, hour=hour, minute=minute, second=second)
                    except pyDate.pyDateException as e:
                        raise pyRinexExceptionBadFile(str(e))

                    break
                elif int(fields[12]) > 1:
                    # event, skip lines indicated in next field
                    skip = int(fields[13])
            else:
                skip -= 1

        return date 
Example 31
Project: Parallel.GAMIT   Author: demiangomez   File: pyRinex_old.py    GNU General Public License v3.0 5 votes vote down vote up
def read_fields(self, line, data, format_tuple):

        # create the parser object
        formatstr = re.sub(r'\..', '',' '.join(format_tuple).replace('%', '').replace('f', 's').replace('i', 's').replace('-', ''))

        fs = struct.Struct(formatstr)
        parse = fs.unpack_from

        if len(data) < fs.size:
            # line too short, add padding zeros
            f = '%-' + str(fs.size) + 's'
            data = f % line
        elif len(data) > fs.size:
            # line too long! cut
            data = line[0:fs.size]

        fields = list(parse(data))

        # convert each element in the list to float if necessary
        for i, field in enumerate(fields):
            if 'f' in format_tuple[i]:
                try:
                    fields[i] = float(fields[i])
                except ValueError:
                    # invalid number in the field!, replace with something harmless
                    fields[i] = float(2.11)
            elif 'i' in format_tuple[i]:
                try:
                    fields[i] = int(fields[i])
                except ValueError:
                    # invalid number in the field!, replace with something harmless
                    fields[i] = int(1)
            elif 's' in format_tuple[i]:
                fields[i] = fields[i].strip()

        return fields 
Example 32
Project: deb-python-cassandra-driver   Author: openstack   File: marshal.py    Apache License 2.0 5 votes vote down vote up
def _make_packer(format_string):
    packer = struct.Struct(format_string)
    pack = packer.pack
    unpack = lambda s: packer.unpack(s)[0]
    return pack, unpack 
Example 33
Project: bitpay-brick   Author: javgh   File: bluetoothservice.py    MIT License 5 votes vote down vote up
def write_varint32(sock, value):
    bits = value & 0x7f
    value >>= 7
    while value:
        packer = struct.Struct('! B')
        packed_data = packer.pack(0x80|bits)
        sock.send(packed_data)
        bits = value & 0x7f
        value >>= 7
    packer = struct.Struct('! B')
    packed_data = packer.pack(bits)
    sock.send(packed_data) 
Example 34
Project: XPLoader   Author: RinCamelia   File: libtcodpy.py    MIT License 5 votes vote down vote up
def blit(self, dest, fill_fore=True, fill_back=True):
        # use libtcod's "fill" functions to write the buffer to a console.
        if (console_get_width(dest) != self.width or
            console_get_height(dest) != self.height):
            raise ValueError('ConsoleBuffer.blit: Destination console has an incorrect size.')

        s = struct.Struct('%di' % len(self.back_r))

        if fill_back:
            _lib.TCOD_console_fill_background(dest, (c_int * len(self.back_r))(*self.back_r), (c_int * len(self.back_g))(*self.back_g), (c_int * len(self.back_b))(*self.back_b))

        if fill_fore:
            _lib.TCOD_console_fill_foreground(dest, (c_int * len(self.fore_r))(*self.fore_r), (c_int * len(self.fore_g))(*self.fore_g), (c_int * len(self.fore_b))(*self.fore_b))
            _lib.TCOD_console_fill_char(dest, (c_int * len(self.char))(*self.char)) 
Example 35
Project: job_manager   Author: lnls-fac   File: pyjob.py    MIT License 5 votes vote down vote up
def handle_request(*items, wait_for_reply=True, exit_on_err=True):
    InfoStruct = struct.Struct(SET_STRUCT_PARAM)
    data = pickle.dumps(items, PICKLE_PROTOCOL)
    try:
        with _SocketManager(Address) as sock:
            sock.sendall(InfoStruct.pack(len(data), VERSION))
            sock.sendall(data)
            if not wait_for_reply:
                return
            size_data = sock.recv(InfoStruct.size)
            size = InfoStruct.unpack(size_data)[0]
            result = bytearray()
            while True:
                data = sock.recv(MAX_BLOCK_LEN)
                if not data:
                    break
                result.extend(data)
                if len(result) >= size:
                    break
        return pickle.loads(result)
    except socket.error as err:
        print("{0}: is the pyjob_server running?".format(err))
        if exit_on_err:
            sys.exit(1)
        else:
            raise ServerDown() 
Example 36
Project: me-ica   Author: ME-ICA   File: structreader.py    GNU Lesser General Public License v2.1 5 votes vote down vote up
def unpack(self, fmt):
        ''' Unpack values from contained buffer

        Unpacks values from ``self.buf`` and updates ``self.ptr`` to the
        position after the read data.

        Parameters
        ----------
        fmt : str
           format string as for ``unpack``

        Returns
        -------
        values : tuple
           values as unpacked from ``self.buf`` according to `fmt`
        '''
        # try and get a struct corresponding to the format string from
        # the cache
        pkst = self._cache.get(fmt)
        if pkst is None: # struct not in cache
            # if we've not got a default endian, or the format has an
            # explicit endianness, then we make a new struct directly
            # from the format string
            if self.endian is None or fmt[0] in _ENDIAN_CODES:
                pkst = Struct(fmt)
            else: # we're going to modify the endianness with our
                # default. 
                endian_fmt = self.endian + fmt
                pkst = Struct(endian_fmt)
                # add an entry in the cache for the modified format
                # string as well as (below) the unmodified format
                # string, in case we get a format string with the same
                # endianness as default, but specified explicitly.
                self._cache[endian_fmt] = pkst
            self._cache[fmt] = pkst
        values = pkst.unpack_from(self.buf, self.ptr)
        self.ptr += pkst.size
        return values 
Example 37
Project: WhooshSearch   Author: rokartnaz   File: columns.py    BSD 2-Clause "Simplified" License 5 votes vote down vote up
def __init__(self, dbfile, basepos, length, doccount, fixedlen):
            self._dbfile = dbfile
            self._basepos = basepos
            self._doccount = doccount
            self._fixedlen = fixedlen

            self._typecode = chr(dbfile.get_byte(basepos + length - 1))

            st = struct.Struct("!" + self._typecode)
            self._unpack = st.unpack
            self._itemsize = st.size

            dbfile.seek(basepos + doccount * self._itemsize)
            self._uniques = self._read_uniques() 
Example 38
Project: WhooshSearch   Author: rokartnaz   File: columns.py    BSD 2-Clause "Simplified" License 5 votes vote down vote up
def __init__(self, dbfile, typecode, default):
            self._dbfile = dbfile
            self._pack = struct.Struct("!" + typecode).pack
            self._default = default
            self._defaultbytes = self._pack(default)
            self._fixedlen = struct.calcsize(typecode)
            self._count = 0 
Example 39
Project: WhooshSearch   Author: rokartnaz   File: columns.py    BSD 2-Clause "Simplified" License 5 votes vote down vote up
def __init__(self, dbfile, basepos, length, doccount, typecode,
                     default):
            self._dbfile = dbfile
            self._basepos = basepos
            self._doccount = doccount
            self._default = default
            self._reverse = False

            self._typecode = typecode
            self._unpack = struct.Struct("!" + typecode).unpack
            self._defaultbytes = struct.pack("!" + typecode, default)
            self._fixedlen = struct.calcsize(typecode)
            self._count = length // self._fixedlen 
Example 40
Project: WhooshSearch   Author: rokartnaz   File: columns.py    BSD 2-Clause "Simplified" License 5 votes vote down vote up
def __init__(self, dbfile, spec, default):
            self._dbfile = dbfile
            self._struct = struct.Struct(spec)
            self._fixedlen = self._struct.size
            self._default = default
            self._defaultbytes = self._struct.pack(*default)
            self._count = 0 
Example 41
Project: WhooshSearch   Author: rokartnaz   File: columns.py    BSD 2-Clause "Simplified" License 5 votes vote down vote up
def __repr__(self):
            return "<Struct.Writer>" 
Example 42
Project: WhooshSearch   Author: rokartnaz   File: columns.py    BSD 2-Clause "Simplified" License 5 votes vote down vote up
def __repr__(self):
            return "<Struct.Reader>" 
Example 43
Project: WhooshSearch   Author: rokartnaz   File: fields.py    BSD 2-Clause "Simplified" License 5 votes vote down vote up
def __setstate__(self, d):
        self.__dict__.update(d)
        self._struct = struct.Struct(">" + str(self.sortable_typecode))
        if "min_value" not in d:
            d["min_value"], d["max_value"] = self._min_max() 
Example 44
Project: TornadoWeb   Author: VxCoder   File: connections.py    Apache License 2.0 5 votes vote down vote up
def read_struct(self, fmt):
        s = struct.Struct(fmt)
        result = s.unpack_from(self._data, self._position)
        self._position += s.size
        return result 
Example 45
Project: NiujiaoDebugger   Author: MrSrc   File: test_call.py    GNU General Public License v3.0 5 votes vote down vote up
def test_varargs11_kw(self):
        msg = r"^pack\(\) takes no keyword arguments$"
        self.assertRaisesRegex(TypeError, msg, struct.Struct.pack, struct.Struct(""), x=2) 
Example 46
Project: NiujiaoDebugger   Author: MrSrc   File: test_struct.py    GNU General Public License v3.0 5 votes vote down vote up
def test_unpack_from(self):
        test_string = b'abcd01234'
        fmt = '4s'
        s = struct.Struct(fmt)
        for cls in (bytes, bytearray):
            data = cls(test_string)
            self.assertEqual(s.unpack_from(data), (b'abcd',))
            self.assertEqual(s.unpack_from(data, 2), (b'cd01',))
            self.assertEqual(s.unpack_from(data, 4), (b'0123',))
            for i in range(6):
                self.assertEqual(s.unpack_from(data, i), (data[i:i+4],))
            for i in range(6, len(test_string) + 1):
                self.assertRaises(struct.error, s.unpack_from, data, i)
        for cls in (bytes, bytearray):
            data = cls(test_string)
            self.assertEqual(struct.unpack_from(fmt, data), (b'abcd',))
            self.assertEqual(struct.unpack_from(fmt, data, 2), (b'cd01',))
            self.assertEqual(struct.unpack_from(fmt, data, 4), (b'0123',))
            for i in range(6):
                self.assertEqual(struct.unpack_from(fmt, data, i), (data[i:i+4],))
            for i in range(6, len(test_string) + 1):
                self.assertRaises(struct.error, struct.unpack_from, fmt, data, i)

        # keyword arguments
        self.assertEqual(s.unpack_from(buffer=test_string, offset=2),
                         (b'cd01',)) 
Example 47
Project: NiujiaoDebugger   Author: MrSrc   File: test_struct.py    GNU General Public License v3.0 5 votes vote down vote up
def test_Struct_reinitialization(self):
        # Issue 9422: there was a memory leak when reinitializing a
        # Struct instance.  This test can be used to detect the leak
        # when running with regrtest -L.
        s = struct.Struct('i')
        s.__init__('ii') 
Example 48
Project: NiujiaoDebugger   Author: MrSrc   File: test_struct.py    GNU General Public License v3.0 5 votes vote down vote up
def check_sizeof(self, format_str, number_of_codes):
        # The size of 'PyStructObject'
        totalsize = support.calcobjsize('2n3P')
        # The size taken up by the 'formatcode' dynamic array
        totalsize += struct.calcsize('P3n0P') * (number_of_codes + 1)
        support.check_sizeof(self, struct.Struct(format_str), totalsize) 
Example 49
Project: NiujiaoDebugger   Author: MrSrc   File: test_struct.py    GNU General Public License v3.0 5 votes vote down vote up
def test_issue29802(self):
        # When the second argument of struct.unpack() was of wrong type
        # the Struct object was decrefed twice and the reference to
        # deallocated object was left in a cache.
        with self.assertRaises(TypeError):
            struct.unpack('b', 0)
        # Shouldn't crash.
        self.assertEqual(struct.unpack('b', b'a'), (b'a'[0],)) 
Example 50
Project: NiujiaoDebugger   Author: MrSrc   File: test_struct.py    GNU General Public License v3.0 5 votes vote down vote up
def test_format_attr(self):
        s = struct.Struct('=i2H')
        self.assertEqual(s.format, '=i2H')

        # use a bytes string
        s2 = struct.Struct(s.format.encode())
        self.assertEqual(s2.format, s.format)