def soft_cast(value, cast_type): """ cast a value to a given type, if the cast fails, cast to null Example: >>> cast(None, int) None >>> cast('', int) None """ try: return cast(value, cast_type) except (TypeError, ValueError): pass return tab.cast_null(value)
def soft_null_cast(value): try: tab.cast_null(value) except TypeError: return value