'How to only pack values that aren't None

def serialize_header(header):
    serialized_header = (
        resource_type.ResourceType[header.resource_type].value
        + serialization_method.SerializationMethod[header.serialization_method].value
    ).encode() + struct.pack(
        ">" + "I" * 2 + "H" * 2 + "B" * 2,
        *[
            header.head_revision,
            header.dependency_table_offset,
            header.branch_id,
            header.branch_revision,
            header.compression_flags,
            header.is_compressed,
        ],
    )

    return serialized_header

I have a function that packs data from a list as bytes, is there a way to perform this only for values in the list that aren't None? For example, header.head_revision may be None. Is there a way to do the same thing I'm doing, but don't pack None values?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source