Pack the struct (and its default values) into a string of bytes.

        Returns
        -------
        :py:class:`bytes`
            Byte-string representation of struct containing default values.
768773
def pack(self):
        
        # Generate a buffer big enough to hold the packed values
        data = bytearray(b"\x00" * self.size)

        # Iterate over the fields, pack each value in little-endian format and
        # insert into the buffered data.
        for field in six.itervalues(self.fields):
            packed_data = struct.pack(b"<" + field.pack_chars, field.default)
            data[field.offset:len(packed_data)+field.offset] = packed_data

        return bytes(data)
343650
def unpack_struct(self, struct):
        

        size = struct.size

        offset = self.offset
        if self.data:
            avail = len(self.data) - offset
        else:
            avail = 0

        if avail < size:
            raise UnpackException(struct.format, size, avail)

        self.offset = offset + size
        return struct.unpack_from(self.data, offset)
255946
def to_bytes()
      packer = Packer.new()

      full_flags = ((@qr     << 15) & 0x8000) |
                   ((@opcode << 11) & 0x7800) |
                   ((@flags  <<  7) & 0x0780) |
                   ((@rcode  <<  0) & 0x000F)

      packer.pack('nnnnnn',
        @trn_id,             # trn_id
        full_flags,          # qr, opcode, flags, rcode
        @questions.length(), # qdcount
        @answers.length(),   # ancount
        0,                   # nscount (we don't handle)
        0,                   # arcount (we don't handle)
      )

      questions.each do |q|
        q.pack(packer)
      end

      answers.each do |a|
        a.pack(packer)
      end

      return packer.get()
    end
330113
def to_string(self):
        
        # From ykdef.h :
        #
        # // Frame structure
	# #define SLOT_DATA_SIZE  64
        # typedef struct {
        #     unsigned char payload[SLOT_DATA_SIZE];
        #     unsigned char slot;
        #     unsigned short crc;
        #     unsigned char filler[3];
        # } YKFRAME;
        filler = b''
        return struct.pack('<64sBH3s',
                           self.payload, self.command, self.crc, filler)
696141
def _StructPackEncoder(wire_type, format):
  

  value_size = struct.calcsize(format)

  def SpecificEncoder(field_number, is_repeated, is_packed):
    local_struct_pack = struct.pack
    if is_packed:
      tag_bytes = TagBytes(field_number, wire_format.WIRETYPE_LENGTH_DELIMITED)
      local_EncodeVarint = _EncodeVarint
      def EncodePackedField(write, value):
        write(tag_bytes)
        local_EncodeVarint(write, len(value) * value_size)
        for element in value:
          write(local_struct_pack(format, element))
      return EncodePackedField
    elif is_repeated:
      tag_bytes = TagBytes(field_number, wire_type)
      def EncodeRepeatedField(write, value):
        for element in value:
          write(tag_bytes)
          write(local_struct_pack(format, element))
      return EncodeRepeatedField
    else:
      tag_bytes = TagBytes(field_number, wire_type)
      def EncodeField(write, value):
        write(tag_bytes)
        return write(local_struct_pack(format, value))
      return EncodeField

  return SpecificEncoder
37391
def to_bytes(obj):
    

    if PY3:
        if isinstance(obj, str):
            return obj.encode(settings.DEFAULT_ENCODING)
        else:
            return obj if isinstance(obj, bytes) else b''
    else:
        if isinstance(obj, str):
            return obj
        else:
            return obj.encode(settings.DEFAULT_ENCODING) if isinstance(obj, unicode) else ''
763933
def pack_value(self, value):

        

        if type(value) is tuple:
            return self.to_binary(*value)
        elif isinstance(value, dict):
            return self.to_binary(**value)
        elif isinstance(value, DictWrapper):
            return self.to_binary(**value._data)
        else:
            raise BadDataError('%s is not a tuple or a list' % (value))
218028
def as_struct_array(*columns):
    
    columns = [np.asarray(c) for c in columns]
    rows = len(columns[0])

    names = ['f'+str(i) for i in range(len(columns))]
    dtype = [(names[i], c.dtype, c.shape[1:]) for i, c in enumerate(columns)]
    data = np.empty(rows, dtype)
    for i, c in enumerate(columns):
        data[names[i]] = c
    return data
505442
def pack_iterable(messages):
    ''''''
    # [ 4-byte body size ]
    # [ 4-byte num messages ]
    # [ 4-byte message #1 size ][ N-byte binary data ]
    #      ... (repeated <num_messages> times)
    return pack_string(
        struct.pack('>l', len(messages)) +
        ''.join(map(pack_string, messages)))
10344
def encode_contents(self):
        

        header = struct.pack("<LL", self.offset, len(self.raw_data))
        return bytearray(header) + self.raw_data
362085
def to_struct(self):
        
        structobj = self.struct_type()
        for k in structobj.attributes():
            self.log.info("Setting attribute %s to %r" % (k, getattr(self, k)))
            setattr(structobj, k, getattr(self, k))
        return structobj
59463
def to_bytes(value):
    
    vtype = type(value)

    if vtype == bytes or vtype == type(None):
        return value

    try:
        return vtype.encode(value)
    except UnicodeEncodeError:
        pass
    return value
747255
def pack(self):
        

        # Possible structs for packet ID.
        #
        try:
            structs_ = get_structs_for_fields([self.fields[0]])
        except (TypeError):
            # TypeError, if self.fields[0] is a wrong argument to `chr()`.
            raise PackError(self)


        # Possible structs for packet ID + subcode
        #
        if structs_ == []:
            try:
                structs_ = get_structs_for_fields([self.fields[0], self.fields[1]])
            except (IndexError, TypeError):
                # IndexError, if no self.fields[1]
                # TypeError, if self.fields[1] is a wrong argument to `chr()`.
                raise PackError(self)


        # Try to pack the packet with any of the possible structs.
        #
        for struct_ in structs_:
            try:
                return struct_.pack(*self.fields)
            except struct.error:
                pass

        # We only get here if the ``return`` inside the``for`` loop
        # above wasn't reached, i.e. none of the `structs_` matched.
        #
        raise PackError(self)
283516
def encode_value(value):
    # type: (bytes) -> bytes
    
    return struct.pack(">I{attr_len:d}s".format(attr_len=len(value)), len(value), value)
55490
def to_bytes(self):
        '''
        
        '''
        hdr = struct.pack(RIPv2._PACKFMT, self.command.value, 2)
        routes = b''.join([r.to_bytes() for r in self._routes])
        return hdr + routes
504450
def idaunpack(buf):
    
    buf = bytearray(buf)

    def nextval(o):
        val = buf[o] ; o += 1
        if val == 0xff:  # 32 bit value
            val, = struct.unpack_from(">L", buf, o)
            o += 4
            return val, o
        if val < 0x80:  # 7 bit value
            return val, o
        val <<= 8
        val |= buf[o] ; o += 1
        if val < 0xc000:  # 14 bit value
            return val & 0x3fff, o

        # 29 bit value
        val <<= 8
        val |= buf[o] ; o += 1
        val <<= 8
        val |= buf[o] ; o += 1
        return val & 0x1fffffff, o

    values = []
    o = 0
    while o < len(buf):
        val, o = nextval(o)
        values.append(val)
    return values
279320
def pack_value(self, val):
        """"""
        if isinstance(val, bytes):
            val = list(iterbytes(val))

        slen = len(val)

        if self.pad:
            pad = b'\0\0' * (slen % 2)
        else:
            pad = b''

        return struct.pack('>' + 'H' * slen, *val) + pad, slen, None