Create bytes from properties
761437
def to_bytes(self):
        '''
        
        '''
        # Verify that the properties make sense
        self.sanitize()

        # Write the version
        bitstream = BitStream('uint:4=%d' % self.version)

        # Write the header length
        options_len = math.ceil(len(self.options) / 4.0)
        bitstream += BitStream('uint:4=%d' % (5 + options_len))

        # Write the type of service
        bitstream += BitStream('uint:8=%d' % self.tos)

        # Write the total length
        payload_bytes = bytes(self.payload)
        total_length = 20 + len(payload_bytes)
        bitstream += BitStream('uint:16=%d' % total_length)

        # Write the identification
        bitstream += BitStream('uint:16=%d' % self.identification)

        # Write the flags
        bitstream += BitStream('bool=False, bool=%d, '
                               'bool=%d' % (self.dont_fragment,
                                            self.more_fragments))

        # Write the fragment offset
        bitstream += BitStream('uint:13=%d' % self.fragment_offset)

        # Write the TTL
        bitstream += BitStream('uint:8=%d' % self.ttl)

        # Write the protocol number
        bitstream += BitStream('uint:8=%d' % self.protocol)

        # Write the header checksum as 0 for now, we calculate it later
        bitstream += BitStream('uint:16=0')

        # Write the source and destination addresses
        bitstream += BitStream('uint:32=%d, '
                               'uint:32=%d' % (int(self.source),
                                               int(self.destination)))

        # Add the options
        bitstream += BitStream(bytes=self.options)
        padding_len = (4 - (len(self.options) % 4)) % 4
        bitstream += BitStream(padding_len * 8)

        # Calculate the header checksum and fill it in
        my_checksum = checksum.ones_complement(bitstream.bytes)
        bitstream[80:96] = BitStream('uint:16=%d' % my_checksum)

        return bitstream.bytes + payload_bytes
188615
def to_bytes(s, encoding="utf-8"):
    """"""
    if isinstance(s, six.binary_type):
        return s
    if six.PY3:
        return bytes(s, encoding)
    return s.encode(encoding)
1005332
def write_bytes(fo, datum, schema=None):
    """"""
    write_long(fo, len(datum))
    fo.write(datum)
93991
def to_bytes(self):
        '''
        
        '''
        raw = b''
        if not self._options:
            return raw
        for ipopt in self._options:
            raw += ipopt.to_bytes()
        padbytes = 4 - (len(raw) % 4)
        raw += b'\x00'*padbytes
        return raw
1172185
def to_bytes(self):
        '''
        
        '''
        # Verify that properties make sense
        self.sanitize()

        # Start with the type
        bitstream = BitArray('uint:4=%d' % self.message_type)

        # Add padding
        bitstream += self._reserved1

        # Add the record count
        bitstream += BitArray('uint:8=%d' % len(self.records))

        # Add the nonce
        bitstream += BitArray(bytes=self.nonce)

        # Add the records
        for record in self.records:
            bitstream += record.to_bitstream()

        return bitstream.bytes
133470
@Override
    public final Object fromBytes(Class targetClass, byte[] bytes)
    {
        try
        {
            if (bytes == null)
            {
                return null;
            }
            if (targetClass != null && targetClass.equals(byte[].class))
            {
                return bytes;
            }
            ObjectInputStream ois;
            ois = new ObjectInputStream(new ByteArrayInputStream(bytes));
            Object o = ois.readObject();
            ois.close();
            return o;
        }
        catch (IOException e)
        {
            log.error("IO exception, Caused by {}.", e);
            throw new PropertyAccessException(e);
        }
        catch (ClassNotFoundException e)
        {
            log.error("Class not found exception, Caused by {}.", e);
            throw new PropertyAccessException(e);
        }

    }
1063088
def serialize_properties(properties):
    
    new_properties = properties.copy()
    for attr_name, attr_value in new_properties.items():
        if isinstance(attr_value, datetime):
            new_properties[attr_name] = attr_value.isoformat()
        elif not isinstance(attr_value, (dict, list, tuple, str, int, float, bool, type(None))):
            # Property is not JSON-serializable according to this table
            # https://docs.python.org/3.4/library/json.html#json.JSONEncoder
            # so we convert to string
            new_properties[attr_name] = str(attr_value)
    return new_properties
1043398
def to_bytes(self):
        '''
        
        '''
        # Verify that the properties make sense
        self.sanitize()

        # Write the next header type
        bitstream = BitStream('uint:8=%d' % self.next_header)

        # Add the reserved bits
        bitstream += BitStream(8)

        # Add the fragment offset
        bitstream += BitStream('uint:13=%d' % self.fragment_offset)

        # Add the reserved bits
        bitstream += BitStream(2)

        # Add the flags
        bitstream += BitStream('bool=%d' % self.more_fragments)

        # Add the identification
        bitstream += BitStream('uint:32=%d' % self.identification)

        return bitstream.bytes + bytes(self.payload)
364762
function chars_to_bytes(ac) {
        var retval = []
        for (var i = 0; i < ac.length; i++) {
            retval = retval.concat(str_to_bytes(ac[i]))
        }
        return retval
    }
821052
def make_streams(name, value, boundary, encoding):
    """"""

    filename = None
    mime = None

    # user passed in a special dict.
    if isinstance(value, collections.Mapping) and "name" in value and "value" in value:
        filename = value["name"]
        try:
            mime = value["mime"]
        except KeyError:
            pass
        value = value["value"]

    if not filename:
        filename = getattr(value, "name", None)
        if filename:
            filename = os.path.split(filename)[1]

    mime = mime or "application/octet-stream"

    name, filename, mime = [escape_header(v) for v in (name, filename, mime)]

    stream = BytesIO()
    stream.write("--{}\r\n".format(boundary).encode(encoding))
    if not filename:
        stream.write(
            'Content-Disposition: form-data; name="{}"\r\n'.format(name).encode(
                encoding
            )
        )
    else:
        stream.write(
            'Content-Disposition: form-data; name="{}"; filename="{}"\r\n'.format(
                name, filename
            ).encode(encoding)
        )
        if mime:
            stream.write("Content-Type: {}\r\n".format(mime).encode(encoding))
    stream.write(b"\r\n")

    if hasattr(value, "read"):
        stream.seek(0)
        return stream, value, BytesIO("\r\n".encode(encoding))

    # not a file-like object, encode headers and value in one go
    value = value if isinstance(value, (str, bytes)) else json.dumps(value)
    if isinstance(value, bytes):
        stream.write(value)
    else:
        stream.write(value.encode(encoding))
    stream.write(b"\r\n")
    stream.seek(0)
    return (stream,)
753314
def cast_bytes(s, encoding=None):
    """"""
    if not isinstance(s, bytes):
        return encode(s, encoding)
    return s
724973
def bytes_to_string(raw):
    """"""
    ret = bytes()
    for byte in raw:
        if byte == 0x00:
            return ret.decode("utf-8")
        ret += bytes([byte])
    return ret.decode("utf-8")
681078
public static <T extends BaseBo> T fromBytes(byte[] bytes, Class<T> clazz) {
        return fromBytes(bytes, clazz, null);
    }
397123
def to_bytes(data):
    
    if isinstance(data, six.string_types) and not isinstance(data, bytes):
        return codecs.encode(data, aws_encryption_sdk.internal.defaults.ENCODING)
    return data
384487
private static String createByteString(byte bytes[])
    {
        StringBuilder sb = new StringBuilder();
        if (bytes == null)
        {
            sb.append("null");
        }
        else
        {
            for (byte b : bytes)
            {
                sb.append(String.format("%02x", b));
            }
        }
        return sb.toString();
    }
357790
def to_bytes(self):
        
        '''
        # Verify that properties make sense
        self.sanitize()

        # Start with the type
        bitstream = BitArray('uint:4=%d' % self.message_type)

        # Add the flags
        bitstream += BitArray('bool=%d, bool=%d' % (self.security,
                                                    self.ddt_originated))

        # Add padding
        bitstream += self._reserved1

        # Determine payload
        payload = self.payload
        if hasattr(payload, 'to_bytes'):
            payload = payload.to_bytes()

        return bitstream.bytes + payload
795783
def to_bytes(self):
        '''
        
        '''
        # Verify that the properties make sense
        self.sanitize()

        # Write the source and destination ports
        bitstream = BitStream('uint:16=%d, '
                              'uint:16=%d' % (self.source_port,
                                              self.destination_port))

        # Write the length
        payload_bytes = bytes(self.payload)
        length = len(payload_bytes) + 8
        bitstream += BitStream('uint:16=%d' % length)

        # Write the checksum
        bitstream += BitStream('uint:16=%d' % self.checksum)

        return bitstream.bytes + payload_bytes