Comparisons
- class nettlesome.quantities.Comparison(**data)
A Predicate that compares a described quantity to a constant.
The Comparison class extends the concept of a Predicate. A Comparison still contains a truth value and a template string, but that template should be used to identify a quantity that will be compared to an expression using a sign such as an equal sign or a greater-than sign. This expression must be a constant: either an integer, a floating point number, or a physical quantity expressed in units that can be parsed using the pint library.
To encourage consistent phrasing, the template string in every Comparison object must end with the word “was”.
If you phrase a Comparison with an inequality sign using truth=False, Nettlesome will silently modify your statement so it can have truth=True with a different sign. In this example, the user’s input indicates that it’s false that the weight of marijuana possessed by a defendant was more than 10 grams. Nettlesome interprets this to mean it’s true that the weight was no more than 10 grams.
>>> # example comparing a pint Quantity >>> drug_comparison_with_upper_bound = Comparison( ... content="the weight of marijuana that $defendant possessed was", ... sign=">", ... expression="10 grams", ... truth=False) >>> str(drug_comparison_with_upper_bound) 'that the weight of marijuana that $defendant possessed was no more than 10 gram'
When the number needed for a Comparison isn’t a physical quantity that can be described with the units in the pint library library, you should phrase the text in the template string to explain what the number describes. The template string will still need to end with the word “was”. The value of the expression parameter should be an integer or a floating point number, not a string to be parsed.
>>> # example comparing an integer >>> three_children = Comparison( ... content="the number of children in ${taxpayer}'s household was", ... sign="=", ... expression=3) >>> str(three_children) "that the number of children in ${taxpayer}'s household was exactly equal to 3"
- Parameters
sign – A string representing an equality or inequality sign like
==
,>
, or<=
. Used to indicate that the clause ends with a comparison to some quantity. Should be defined if and only if aquantity
is defined. Even though “=” is the default, it’s the least useful, because courts almost always state rules that are intended to apply to quantities above or below some threshold.quantity – a Python number object or
ureg.Quantity
from the pint library. Comparisons to quantities can be used to determine whetherPredicate
s imply or contradict each other. A singlePredicate
may contain no more than onesign
and onequantity
.
- __hash__ = None
- __str__()
Return str(self).
- __weakref__
list of weak references to the object (if defined)
- contradicts(other)
Check if
other
andself
have contradictory meanings.If
self
andother
have consistent text in the predicate attribute, this method looks for a contradiction based on the dimensionality or numeric range of theQuantityRange
s forself
andother
.>>> earlier = Comparison( ... content="the date $dentist became a licensed dentist was", ... sign="<", expression=date(1990, 1, 1)) >>> later = Comparison( ... content="the date $dentist became a licensed dentist was", ... sign=">", expression=date(2010, 1, 1)) >>> str(earlier) 'that the date $dentist became a licensed dentist was less than 1990-01-01' >>> str(later) 'that the date $dentist became a licensed dentist was greater than 2010-01-01' >>> earlier.contradicts(later) True >>> later.contradicts(earlier) True
- Return type
- classmethod expression_to_quantity(value)
Create numeric expression from text for Comparison class.
This expression can be a datetime.date, an int, a float, or a pint quantity. (See pint tutorial)
- Parameters
quantity – an object to be interpreted as the
expression
field of aComparison
- Return type
- Returns
a Python number object or a
pint.Quantity
object created withpint.UnitRegistry
.
- implies(other)
Check if
self
impliesother
.May be based on template text, truth values, and
QuantityRange
s.>>> small_weight=Comparison( ... content="the amount of gold $person possessed was", ... sign=">=", ... expression=Q_("1 gram")) >>> large_weight=Comparison( ... content="the amount of gold $person possessed was", ... sign=">=", ... expression=Q_("100 kilograms")) >>> str(large_weight) 'that the amount of gold $person possessed was at least 100 kilogram' >>> str(small_weight) 'that the amount of gold $person possessed was at least 1 gram' >>> large_weight.implies(small_weight) True
- Return type
- property interval: Union[sympy.sets.sets.FiniteSet, sympy.sets.sets.Interval, sympy.sets.sets.Union]
Get the range of numbers covered by the UnitInterval.
>>> weight=Comparison( ... content="the amount of gold $person possessed was", ... sign=">=", ... expression="10 grams") >>> weight.interval Interval(10, oo)
- means(other)
Check if
self
andother
have identical meanings.This method can convert different units to determine whether self and other refer to the same
QuantityRange
.>>> volume_in_liters = Comparison( ... content="the volume of fuel in the tank was", ... sign="=", expression="10 liters") >>> volume_in_milliliters = Comparison( ... content="the volume of fuel in the tank was", ... sign="=", expression="10000 milliliters") >>> volume_in_liters.means(volume_in_milliliters) True
- Return type
- negated()
Copy
self
, with the opposite truth value.- Return type
- property quantity: Union[int, float, datetime.date, pint.quantity.Quantity]
Get the maximum or minimum of the range.
- classmethod set_quantity_range(values)
Reverse the sign of a Comparison if necessary.
- property sign: str
Get operator describing the relationship between the quantity and the range.
>>> weight=Comparison( ... content="the amount of gold $person possessed was", ... sign=">=", ... expression="10 grams") >>> str(weight.quantity_range) 'at least 10 gram' >>> weight.sign '>='
- Return type