def test_it_should_be_able_to_do_basic_arithmetic_with_other_unit_value(self): v = UnitValue(20.4, '$') r = UnitValue(14.5, '$') + v expect(r).to.be.a(UnitValue) expect(r.value).to.equal(34.9, epsilon=0.01) expect(r.unit).to.equal('$') r = v - UnitValue(14.5, '$') expect(r).to.be.a(UnitValue) expect(r.value).to.equal(5.9, epsilon=0.01) expect(r.unit).to.equal('$') r = v * UnitValue(2, '$') expect(r).to.be.a(UnitValue) expect(r.value).to.equal(40.8, epsilon=0.01) expect(r.unit).to.equal('$') r = v / UnitValue(2, '$') expect(r).to.be.a(UnitValue) expect(r.value).to.equal(10.2, epsilon=0.01) expect(r.unit).to.equal('$')
def test_it_should_raise_arithmetic_error_if_not_the_same_unit(self): v = UnitValue(20.4, '$') other = UnitValue(4, '€') expect(lambda: v + other).to.throw(ArithmeticError) expect(lambda: v - other).to.throw(ArithmeticError) expect(lambda: v * other).to.throw(ArithmeticError) expect(lambda: v / other).to.throw(ArithmeticError)
def test_it_should_returns_false_when_comparing_different_units(self): v = UnitValue(20.4, '$') other = UnitValue(4, '€') expect(v == other).to.be.false expect(v != other).to.be.false expect(v < other).to.be.false expect(v <= other).to.be.false expect(v > other).to.be.false expect(v >= other).to.be.false
def test_it_should_be_able_to_do_basic_arithmetic_with_values(self): v = UnitValue(20.4, '$') r = v + 14.5 expect(r).to.be.a(UnitValue) expect(r.value).to.equal(34.9, epsilon=0.01) expect(r.unit).to.equal('$') r = v - 14.5 expect(r).to.be.a(UnitValue) expect(r.value).to.equal(5.9, epsilon=0.01) expect(r.unit).to.equal('$') r = v * 2 expect(r).to.be.a(UnitValue) expect(r.value).to.equal(40.8, epsilon=0.01) expect(r.unit).to.equal('$') r = v / 2 expect(r).to.be.a(UnitValue) expect(r.value).to.equal(10.2, epsilon=0.01) expect(r.unit).to.equal('$')
def test_it_should_be_able_to_compare_with_values(self): v = UnitValue(20.4, '$') expect(v).to.equal(20.4, epsilon=0.01) expect(v).to_not.equal(20.9, epsilon=0.01) expect(v > 10).to.be.true expect(v >= 25).to.be.false expect(v < 30).to.be.true expect(v <= 20).to.be.false
def test_it_should_be_able_to_compare_with_other_unit_value(self): v = UnitValue(20.4, '$') expect(v).to.equal(UnitValue(20.4, '$'), epsilon=0.01) expect(v).to_not.equal(UnitValue(20.9, '$'), epsilon=0.01) expect(v > UnitValue(10, '$')).to.be.true expect(v >= UnitValue(25, '$')).to.be.false expect(v < UnitValue(30, '$')).to.be.true expect(v <= UnitValue(20, '$')).to.be.false
def get_entity_value(data): """Try to retrieve a flat value from a parsed snips entity. It will try to convert the parsed slot value to a python representation: - for Duration, it will returns a `dateutil.relativedelta` object - for AmountOfMoney and Temperature, it returns a `pytlas.interpreters.slot.UnitValue` - for Percentage, a float between 0 and 1 - for InstantTime, a `datetime.datetime` object, - for TimeInterval, a tuple of `datetime.datetime` objects, - for anything else, a string Args: data (dict): Entity data Returns: any: A python object which match the slot value kind """ kind = data.get('kind') if kind == 'Duration': return relativedelta( days=data.get('days'), hours=data.get('hours'), minutes=data.get('minutes'), months=data.get('months'), seconds=data.get('seconds'), weeks=data.get('weeks'), years=data.get('years'), ) elif kind == 'AmountOfMoney' or kind == 'Temperature': return UnitValue(data.get('value'), data.get('unit')) elif kind == 'Percentage': return data.get('value') / 100.0 elif kind == 'InstantTime': return parse_date(data.get('value')) elif kind == 'TimeInterval': return (parse_date(data.get('from')), parse_date(data.get('to'))) return data.get('value')
def test_it_should_keep_the_value_and_the_unit(self): v = UnitValue(20.4, '$') expect(v.value).to.equal(20.4) expect(v.unit).to.equal('$')
def test_it_should_print_value_and_unit(self): v = UnitValue(20, '$') expect(str(v)).to.equal('20$')