naming
I try to avoid 1-letter variable names, unless it are x, y, z
for coordinates, or i
during an iteration, so I would rename f
to float_value
or something, but that is a matter of taste.
short_circuit
if value
is an int
or StrictInt
already, you can return early. This way, you also
failing test-cases
there are a few cases that fail the current implementation that are integers
StrictInt(0)
StrictInt(0.0)
StrictInt(3 + 0j)
StrictInt('3 + 0j')
fixing the first is as easy as changing if not f:
to if f is None
#Complex
Complex
adding support for complex is rather easy, and eliminates the need to call float
class StrictInt(int):
def __new__(cls, value, *args, **kwargs):
# type: (Union[int, float, str], Any, Any) -> int
if isinstance(value, (int, StrictInt)):
return super(StrictInt, cls).__new__(cls, value)
if isinstance(value, str):
value = value.replace(' ', '')
elif not isinstance(value, (float, complex)):
type_str = str(type(value)).replace("<class '", "").replace("'>", "")
raise TypeError("Cannot convert type '{type}' to strict integer".format(type=type_str))
try:
complex_value = complex(value)
except ValueError:
raise ValueError("Cannot convert a non-number to a strict integer.")
if complex_value.imag:
raise ValueError('Cannot convert complex number with imaginary part')
float_value = complex_value.real
if not float_value.is_integer():
raise ValueError("Cannot convert value due to non-integer parts.")
return super(StrictInt, cls).__new__(cls, int(float_value))
Python 2
If you want to include python 2, unicode
should also be accepted as type
naming
I try to avoid 1-letter variable names, unless it are x, y, z
for coordinates, or i
during an iteration, so I would rename f
to float_value
or something, but that is a matter of taste.
short_circuit
if value
is an int
or StrictInt
already, you can return early. This way, you also
failing test-cases
there are a few cases that fail the current implementation that are integers
StrictInt(0)
StrictInt(0.0)
StrictInt(3 + 0j)
StrictInt('3 + 0j')
fixing the first is as easy as changing if not f:
to if f is None
#Complex
adding support for complex is rather easy, and eliminates the need to call float
class StrictInt(int):
def __new__(cls, value, *args, **kwargs):
# type: (Union[int, float, str], Any, Any) -> int
if isinstance(value, (int, StrictInt)):
return super(StrictInt, cls).__new__(cls, value)
if isinstance(value, str):
value = value.replace(' ', '')
elif not isinstance(value, (float, complex)):
type_str = str(type(value)).replace("<class '", "").replace("'>", "")
raise TypeError("Cannot convert type '{type}' to strict integer".format(type=type_str))
try:
complex_value = complex(value)
except ValueError:
raise ValueError("Cannot convert a non-number to a strict integer.")
if complex_value.imag:
raise ValueError('Cannot convert complex number with imaginary part')
float_value = complex_value.real
if not float_value.is_integer():
raise ValueError("Cannot convert value due to non-integer parts.")
return super(StrictInt, cls).__new__(cls, int(float_value))
Python 2
If you want to include python 2, unicode
should also be accepted as type
naming
I try to avoid 1-letter variable names, unless it are x, y, z
for coordinates, or i
during an iteration, so I would rename f
to float_value
or something, but that is a matter of taste.
short_circuit
if value
is an int
or StrictInt
already, you can return early. This way, you also
failing test-cases
there are a few cases that fail the current implementation that are integers
StrictInt(0)
StrictInt(0.0)
StrictInt(3 + 0j)
StrictInt('3 + 0j')
fixing the first is as easy as changing if not f:
to if f is None
Complex
adding support for complex is rather easy, and eliminates the need to call float
class StrictInt(int):
def __new__(cls, value, *args, **kwargs):
# type: (Union[int, float, str], Any, Any) -> int
if isinstance(value, (int, StrictInt)):
return super(StrictInt, cls).__new__(cls, value)
if isinstance(value, str):
value = value.replace(' ', '')
elif not isinstance(value, (float, complex)):
type_str = str(type(value)).replace("<class '", "").replace("'>", "")
raise TypeError("Cannot convert type '{type}' to strict integer".format(type=type_str))
try:
complex_value = complex(value)
except ValueError:
raise ValueError("Cannot convert a non-number to a strict integer.")
if complex_value.imag:
raise ValueError('Cannot convert complex number with imaginary part')
float_value = complex_value.real
if not float_value.is_integer():
raise ValueError("Cannot convert value due to non-integer parts.")
return super(StrictInt, cls).__new__(cls, int(float_value))
Python 2
If you want to include python 2, unicode
should also be accepted as type
naming
I try to avoid 1-letter variable names, unless it are x, y, z
for coordinates, or i
during an iteration, so I would rename f
to float_value
or something, but that is a matter of taste.
short_circuit
if value
is an int
or StrictInt
already, you can return early. This way, you also
failing test-cases
there are a fewcasesfew cases that fail the current implementation that are integers
StrictInt(0)
StrictInt(0.0)
StrictInt(3 + 0j)
StrictInt('3 + 0j')
fixing the first is as easy as changing if not f:
to if f is None
#Complex
Since complex
can not handle a string input, the easiest way to include this,
#Complex
adding support for complex is to use ast.literal_eval
rather easy, and then ifeliminates the type isneed to call complexfloat
class StrictInt(int):
def __new__(cls, value, *args, **kwargs):
# type: (Union[int, float, str], Any, Any) -> int
if isinstance(value, (int, StrictInt)):
return super(StrictInt, cls).__new__(cls, value)
if isinstance(value, str):
value = value.replace(' ', '')
elif not isinstance(value, (float, complex)):
type_str = str(type(value)).replace("<class '", "").replace("'>", "")
raise TypeError("Cannot convert type '{type}' to strict integer".format(type=type_str))
try:
complex_value = complex(value)
except ValueError:
raise ValueError("Cannot convert a non-number to a strict integer.")
if complex_value.imag:
raise ValueError('Cannot convert complex number with imaginary part')
float_value = complex_value.real
if not float_value.is_integer():
raise ValueError("Cannot convert value due to non-integer parts.")
return super(StrictInt, cls).__new__(cls, int(float_value))
Python 2
If you want to include python 2, check whether value.imag
is 0unicode
should also be accepted as type
there are a fewcases that fail the current implementation that are integers
StrictInt(0)
StrictInt(3 + 0j)
StrictInt('3 + 0j')
fixing the first is as easy as changing if not f:
to if f is None
#Complex
Since complex
can not handle a string input, the easiest way to include this, is to use ast.literal_eval
, and then if the type is complex
, check whether value.imag
is 0
naming
I try to avoid 1-letter variable names, unless it are x, y, z
for coordinates, or i
during an iteration, so I would rename f
to float_value
or something, but that is a matter of taste.
short_circuit
if value
is an int
or StrictInt
already, you can return early. This way, you also
failing test-cases
there are a few cases that fail the current implementation that are integers
StrictInt(0)
StrictInt(0.0)
StrictInt(3 + 0j)
StrictInt('3 + 0j')
fixing the first is as easy as changing if not f:
to if f is None
#Complex
adding support for complex is rather easy, and eliminates the need to call float
class StrictInt(int):
def __new__(cls, value, *args, **kwargs):
# type: (Union[int, float, str], Any, Any) -> int
if isinstance(value, (int, StrictInt)):
return super(StrictInt, cls).__new__(cls, value)
if isinstance(value, str):
value = value.replace(' ', '')
elif not isinstance(value, (float, complex)):
type_str = str(type(value)).replace("<class '", "").replace("'>", "")
raise TypeError("Cannot convert type '{type}' to strict integer".format(type=type_str))
try:
complex_value = complex(value)
except ValueError:
raise ValueError("Cannot convert a non-number to a strict integer.")
if complex_value.imag:
raise ValueError('Cannot convert complex number with imaginary part')
float_value = complex_value.real
if not float_value.is_integer():
raise ValueError("Cannot convert value due to non-integer parts.")
return super(StrictInt, cls).__new__(cls, int(float_value))
Python 2
If you want to include python 2, unicode
should also be accepted as type
there are a fewcases that fail the current implementation that are integers
StrictInt(0)
StrictInt(3 + 0j)
StrictInt('3 + 0j')
fixing the first is as easy as changing if not f:
to if f is None
#Complex
Since complex
can not handle a string input, the easiest way to include this, is to use ast.literal_eval
, and then if the type is complex
, check whether value.imag
is 0