def tlv_cast(tlvs, tlv_attr, cast_func):
    """Return cast'd attribute of first TLV or None."""
    tlv_val = None
    if tlvs:
        try:
            tlv_val = cast_func(getattr(tlvs[0], tlv_attr))
        except (AttributeError, ValueError, TypeError):
            pass
    return tlv_val