Numeric Types¶
IsInt
¶
Checks that a value is an integer.
Inherits from IsNumeric
and can therefore be initialised with any of its arguments.
from dirty_equals import IsInt
assert 1 == IsInt
assert -2 == IsInt
assert 1.0 != IsInt
assert 'foobar' != IsInt
assert True != IsInt
assert 1 == IsInt(exactly=1)
assert -2 != IsInt(exactly=1)
allowed_types = int
class-attribute
instance-attribute
¶
As the name suggests, only integers are allowed, booleans (True
are False
) are explicitly excluded although
technically they are sub-types of int
.
IsFloat
¶
Checks that a value is a float.
Inherits from IsNumeric
and can therefore be initialised with any of its arguments.
from dirty_equals import IsFloat
assert 1.0 == IsFloat
assert 1 != IsFloat
assert 1.0 == IsFloat(exactly=1.0)
assert 1.001 != IsFloat(exactly=1.0)
allowed_types = float
class-attribute
instance-attribute
¶
As the name suggests, only floats are allowed.
IsPositive ¶
Bases: IsNumber
Check that a value is positive (> 0
), can be an int
, a float
or a Decimal
(or indeed any value which implements __gt__
for 0
).
from decimal import Decimal
from dirty_equals import IsPositive
assert 1.0 == IsPositive
assert 1 == IsPositive
assert Decimal('3.14') == IsPositive
assert 0 != IsPositive
assert -1 != IsPositive
IsNegative ¶
Bases: IsNumber
Check that a value is negative (< 0
), can be an int
, a float
or a Decimal
(or indeed any value which implements __lt__
for 0
).
from decimal import Decimal
from dirty_equals import IsNegative
assert -1.0 == IsNegative
assert -1 == IsNegative
assert Decimal('-3.14') == IsNegative
assert 0 != IsNegative
assert 1 != IsNegative
IsNonNegative ¶
Bases: IsNumber
Check that a value is positive or zero (>= 0
), can be an int
, a float
or a Decimal
(or indeed any value which implements __ge__
for 0
).
from decimal import Decimal
from dirty_equals import IsNonNegative
assert 1.0 == IsNonNegative
assert 1 == IsNonNegative
assert Decimal('3.14') == IsNonNegative
assert 0 == IsNonNegative
assert -1 != IsNonNegative
assert Decimal('0') == IsNonNegative
IsNonPositive ¶
Bases: IsNumber
Check that a value is negative or zero (<=0
), can be an int
, a float
or a Decimal
(or indeed any value which implements __le__
for 0
).
from decimal import Decimal
from dirty_equals import IsNonPositive
assert -1.0 == IsNonPositive
assert -1 == IsNonPositive
assert Decimal('-3.14') == IsNonPositive
assert 0 == IsNonPositive
assert 1 != IsNonPositive
assert Decimal('-0') == IsNonPositive
assert Decimal('0') == IsNonPositive
IsPositiveInt ¶
Bases: IsInt
Like IsPositive
but only for int
s.
from decimal import Decimal
from dirty_equals import IsPositiveInt
assert 1 == IsPositiveInt
assert 1.0 != IsPositiveInt
assert Decimal('3.14') != IsPositiveInt
assert 0 != IsPositiveInt
assert -1 != IsPositiveInt
IsNegativeInt ¶
Bases: IsInt
Like IsNegative
but only for int
s.
from decimal import Decimal
from dirty_equals import IsNegativeInt
assert -1 == IsNegativeInt
assert -1.0 != IsNegativeInt
assert Decimal('-3.14') != IsNegativeInt
assert 0 != IsNegativeInt
assert 1 != IsNegativeInt
IsPositiveFloat ¶
Bases: IsFloat
Like IsPositive
but only for float
s.
from decimal import Decimal
from dirty_equals import IsPositiveFloat
assert 1.0 == IsPositiveFloat
assert 1 != IsPositiveFloat
assert Decimal('3.14') != IsPositiveFloat
assert 0.0 != IsPositiveFloat
assert -1.0 != IsPositiveFloat
IsNegativeFloat ¶
Bases: IsFloat
Like IsNegative
but only for float
s.
from decimal import Decimal
from dirty_equals import IsNegativeFloat
assert -1.0 == IsNegativeFloat
assert -1 != IsNegativeFloat
assert Decimal('-3.14') != IsNegativeFloat
assert 0.0 != IsNegativeFloat
assert 1.0 != IsNegativeFloat
IsFloatInf ¶
IsFloatInfPos ¶
Bases: IsFloatInf
Checks that a value is float and positive infinite.
Inherits from IsFloatInf
.
from dirty_equals import IsFloatInfPos
assert float('inf') == IsFloatInfPos
assert -float('-inf') == IsFloatInfPos
assert -float('inf') != IsFloatInfPos
assert float('-inf') != IsFloatInfPos
IsFloatInfNeg ¶
Bases: IsFloatInf
Checks that a value is float and negative infinite.
Inherits from IsFloatInf
.
from dirty_equals import IsFloatInfNeg
assert -float('inf') == IsFloatInfNeg
assert float('-inf') == IsFloatInfNeg
assert float('inf') != IsFloatInfNeg
assert -float('-inf') != IsFloatInfNeg
IsFloatNan ¶
IsApprox ¶
IsApprox(approx: Num, *, delta: Optional[Num] = None)
Bases: IsNumber
Simplified subclass of IsNumber
that only allows approximate comparisons.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
approx |
Num
|
A value to approximately compare to. |
required |
delta |
Optional[Num]
|
The allowable different when comparing to the value to |
None
|
from dirty_equals import IsApprox
assert 1.0 == IsApprox(1)
assert 123 == IsApprox(120, delta=4)
assert 201 == IsApprox(200)
assert 201 != IsApprox(200, delta=0.1)
IsNumber ¶
IsNumeric ¶
IsNumeric(*, exactly: Optional[N] = None, approx: Optional[N] = None, delta: Optional[N] = None, gt: Optional[N] = None, lt: Optional[N] = None, ge: Optional[N] = None, le: Optional[N] = None)
Bases: DirtyEquals[N]
Base class for all numeric types, IsNumeric
implements approximate and inequality comparisons,
as well as the type checks.
This class can be used directly or via any of its subclasses.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
exactly |
Optional[N]
|
A value to exactly compare to - useful when you want to make sure a value is an |
None
|
approx |
Optional[N]
|
A value to approximately compare to. |
None
|
delta |
Optional[N]
|
The allowable different when comparing to the value to |
None
|
gt |
Optional[N]
|
Value which the compared value should be greater than. |
None
|
lt |
Optional[N]
|
Value which the compared value should be less than. |
None
|
ge |
Optional[N]
|
Value which the compared value should be greater than or equal to. |
None
|
le |
Optional[N]
|
Value which the compared value should be less than or equal to. |
None
|
If not values are provided, only the type is checked.
If approx
is provided as well a gt
, lt
, ge
, or le
, a TypeError
is raised.
Example of direct usage: