I'm newbie in python and I'm trying to handle errors but I don't know the most efficient way to handle errors in python. I tried this way, but it seems a bit complex to understand. I think may can exist some other "better" manner to handle it.
def set_bit(value, pos, nbits):
"""
Set bit at position.
Keyword Arguments
value (int)
Bitstring value.
pos (int)
Position to set bit.
nbits (int)
Number of bits.
"""
if isinstance(value, int):
if value > -1:
# Positives only
if isinstance(pos, int):
if pos > -1:
if isinstance(nbits, int):
if nbits > -1:
return get_bistring(value | 2 ** (pos % nbits), nbits)
else:
raise ValueError(
'"nbits" was set {}, but it must be positive only'
.format(nbits)
)
else:
raise TypeError(
'"nbits" was set {}, but it must be int only'
.format(type(nbits))
)
else:
raise ValueError(
'"pos" was set {}, but it must be positive only'
.format(pos)
)
else:
raise TypeError(
'"pos" was set {}, but it must be int only'
.format(type(pos))
)
else:
raise ValueError(
'"value" was set {}, but it must be positive only'
.format(value)
)
else:
raise TypeError(
'"value" was set {}, but it must be int only'
.format(type(value))
)