def encode(self):
header    = bytearray(<NUM_LIT:1>)<EOL>varHeader = bytearray()<EOL>payload   = bytearray()<EOL>if self.qos:<EOL><INDENT>header[<NUM_LIT:0>] = <NUM_LIT> | self.retain | (self.qos << <NUM_LIT:1>) | (self.dup << <NUM_LIT:3>)<EOL>varHeader.extend(encodeString(self.topic)) <EOL>varHeader.extend(encode16Int(self.msgId))  <EOL><DEDENT>else:<EOL><INDENT>header[<NUM_LIT:0>] = <NUM_LIT> | self.retain<EOL>varHeader.extend(encodeString(self.topic)) <EOL><DEDENT>if isinstance(self.payload, bytearray):<EOL><INDENT>payload.extend(self.payload)<EOL><DEDENT>elif isinstance(self.payload, str):<EOL><INDENT>payload.extend(bytearray(self.payload, encoding='<STR_LIT:utf-8>'))<EOL><DEDENT>else:<EOL><INDENT>raise PayloadTypeError(type(self.payload))<EOL><DEDENT>totalLen = len(varHeader) + len(payload)<EOL>if totalLen > <NUM_LIT>:<EOL><INDENT>raise PayloadValueError(totalLen)<EOL><DEDENT>header.extend(encodeLength(totalLen))<EOL>header.extend(varHeader)<EOL>header.extend(payload)<EOL>self.encoded = header<EOL>return str(header) if PY2 else bytes(header)<EOL>
Encode and store a PUBLISH control packet.
@raise e: C{ValueError} if encoded topic string exceeds 65535 bytes.
@raise e: C{ValueError} if encoded packet size exceeds 268435455 bytes.
@raise e: C{TypeError} if C{data} is not a string, bytearray, int, boolean or float.