Pack a struct to a stream
        
        :param namedstruct: struct to pack
        
        :param stream: a buffered stream
        
        :return: appended bytes size
166776
def packto(self, namedstruct, stream):
        
        # Default implementation
        data = self.pack(namedstruct)
        return stream.write(data)
883799
def stream(self):
        
        if self._stream is None:
            self._stream = tempfile.NamedTemporaryFile(delete=False)
            try:
               self._stream.write(self.client.open(self.filename, view='data').read())
            except:
               pass
        return self._stream
584636
def paddingsize(self, namedstruct):
        '''
        
        '''
        if self.base is not None:
            return self.base.paddingsize(namedstruct)
        realsize = namedstruct._realsize()
        return (realsize + self.padding - 1) // self.padding * self.padding
836000
def unpack(self, fmt):
        

        sfmt = compile_struct(fmt)
        size = sfmt.size

        if not self.data:
            raise UnpackException(fmt, size, 0)

        buff = self.data.read(size)
        if len(buff) < size:
            raise UnpackException(fmt, size, len(buff))

        return sfmt.unpack(buff)
1053213
def pack(self, remaining_size):
        """"""
        arguments_count, payload = self.pack_data(remaining_size - self.header_size)
        payload_length = len(payload)

        # align payload length to multiple of 8
        if payload_length % 8 != 0:
            payload += b"\x00" * (8 - payload_length % 8)

        self.header = PartHeader(self.kind, self.attribute, arguments_count, self.bigargumentcount,
                                 payload_length, remaining_size)
        hdr = self.header_struct.pack(*self.header)
        if pyhdb.tracing:
            self.trace_header = humanhexlify(hdr, 30)
            self.trace_payload = humanhexlify(payload, 30)
        return hdr + payload
92076
def pack(content):
        
        if isinstance(content, six.text_type):
            content = content.encode("utf-8")
        return struct.pack('i', len(content)) + content
172678
def pack(cls, data):
        ''''''
        return struct.pack('>ll', len(data) + 4, cls.FRAME_TYPE) + data
1138299
def _append(self, signature, fields=(), response=None):
        
        self.packer.pack_struct(signature, fields)
        self.output_buffer.chunk()
        self.output_buffer.chunk()
        self.responses.append(response)
210497
def unpack(self, struc):
        
        buf, offset = readall_fast(self, struc.size)
        return struc.unpack_from(buf, offset)
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
509422
def Pack(self):
        """"""
        return struct.pack(self.format, self.command, self.arg0, self.arg1,
                           len(self.data), self.checksum, self.magic)
420008
def writeBytes(self, s):
        
        l = len(s)

        if l > 0xffff:
            self.writeType(TYPE_LONGSTRING)
        else:
            self.writeType(TYPE_STRING)

        if l > 0xffff:
            self.stream.write_ulong(l)
        else:
            self.stream.write_ushort(l)

        self.stream.write(s)
1174041
def MapByteStream(self, byte_stream, **unused_kwargs):  # pylint: disable=redundant-returns-doc
    
    raise errors.MappingError(
        'Unable to map {0:s} data type to byte stream'.format(
            self._data_type_definition.TYPE_INDICATOR))
1127332
def p_struct(self, p):
        ''''''
        p[0] = ast.Struct(
            name=p[2], fields=p[4], annotations=p[6], lineno=p.lineno(2)
        )
444843
def byteswap(s):
    
    assert len(s) % 2 == 0
    parts = []
    for i in range(0, len(s), 2):
        chunk = s[i:i + 2]
        newchunk = struct.pack('<h', *struct.unpack('>h', chunk))
        parts.append(newchunk)
    return b''.join(parts)
671963
def stream(self, name):
        
        if self.stream_name != "default":
            raise ValueError("Nested streams aren't supported")
        if self._streams.get(name) == None:
            self._streams[name] = History(self.fname, out_dir=self.out_dir,
                                          add_callback=self._add_callback, stream_name=name)
        return self._streams[name]
956218
def read_struct(fstream):
    
    line = fstream.readline().strip()
    fragments = line.split(",")
    fragments = [x for x in fragments if x is not None]
    partition = dict()
    if not len(fragments) >= 3:
        return None

    partition["struct"] = fragments[0]
    partition["info"] = fragments[1]
    partition["num_lines"] = fragments[2]

    struct = None
    if partition is not None and partition["struct"] == "STRUCT":
        num_lines = int(partition["num_lines"].strip())
        struct = {}
        for _ in range(num_lines):
            cols = fetch_cols(fstream)
            struct.update({cols[0]: cols[1:]})
    return struct