Beispiel #1
0
def test_compose():
    f = restrict_arity(Mock(), 1)
    g = restrict_arity(Mock(), 3)
    h = compose(f, g)

    result = h(1, 2, 3)

    g.assert_called_once_with(1, 2, 3)
    f.assert_called_once_with(g.return_value)
    assert result is f.return_value
Beispiel #2
0
def test_simple_unit_conversion_system():
    registry = initialize_unit_registry()
    unit = registry['unit']
    inch_to_meter = registry['inch_to_meter']
    psi_to_nsm = registry['psi_to_nsm']
    celsius_to_kelvin = registry['celsius_to_kelvin']
    fahrenheit_to_celsius = registry['fahrenheit_to_celsius']

    radius_inch = unit.invert(inch_to_meter)(sphere_radius(
        gas_law_volume(psi_to_nsm(14.7),
                       compose(celsius_to_kelvin, fahrenheit_to_celsius)(68),
                       1)))

    assert math.isclose(radius_inch, 7.04962, abs_tol=1e-5)
Beispiel #3
0
def test_compose_multiple_funcs():
    def poly(*args):
        results = []
        for x in args:
            results.extend([1, x, x * x])
        return Values(*results)

    def sum(*args):
        r = 0
        for x in args:
            r += x
        return r

    def square(x):
        return x * x

    h = compose(square, sum, poly)
    assert h(2) == 7 * 7
Beispiel #4
0
def initialize_unit_registry():
    unit = ConversionRegistry()

    celsius_to_kelvin = unit.make_conversion(lambda c: c + 273.15,
                                             lambda k: k - 273.15)

    fahrenheit_to_celsius = unit.make_conversion(lambda f: (f - 32) * 5 / 9,
                                                 lambda c: c * 9 / 5 + 32)

    pound_to_newton = unit.make_conversion(lambda p: p / 0.22480894387096,
                                           lambda n: n * 0.22480894387096)

    inch_to_meter = unit.make_conversion(lambda i: i / 39.37,
                                         lambda m: m * 39.37)

    psi_to_nsm = compose(pound_to_newton, unit.invert(inch_to_meter),
                         unit.invert(inch_to_meter))

    return locals()
Beispiel #5
0
    value_exec = analyze(definition_value(expression))

    def the_definition(environment):
        define_variable(var, value_exec(environment), environment)
        return var

    return the_definition


define_generic_procedure_handler(x.analyze, match_args(is_definition),
                                 analyze_definition)

# === derived syntax ===

define_generic_procedure_handler(x.analyze, match_args(is_cond),
                                 compose(analyze, cond_to_if))
define_generic_procedure_handler(x.analyze, match_args(is_let),
                                 compose(analyze, let_to_combination))

# === apply ===

define_generic_procedure_handler(
    x.apply,
    match_args(is_strict_primitive_procedure, is_executors, is_environment),
    lambda procedure, operand_execs, environment: apply_primitive_procedure(
        procedure,
        map(lambda operand_exec: x.advance(operand_exec(environment)),
            operand_execs),
    ),
)
Beispiel #6
0
def test_composition_checks_arity():
    f = restrict_arity(Mock(), 1)
    h = compose(f, lambda x, y: 0)
    with pytest.raises(TypeError, match="argument"):
        compose(f, h)()
Beispiel #7
0
def test_compose_zero_funcs():
    h = compose()
    assert h(1) == Values(1)
Beispiel #8
0
def test_composition_arity_equals_arity_of_g():
    f = restrict_arity(Mock(), 1)
    h = compose(f, lambda x, y: 0)
    assert get_arity(h) == Arity(2)
Beispiel #9
0
 def pow(self, unit, n):
     assert self.is_unit_converter(unit)
     return self.make_conversion(compose(*[unit] * n),
                                 compose(*[self.invert(unit)] * n))
Beispiel #10
0
 def div(self, u1, u2):
     assert self.is_unit_converter(u1)
     assert self.is_unit_converter(u2)
     return self.make_conversion(compose(self.invert(u2), u1),
                                 compose(self.invert(u1), u2))
Beispiel #11
0
 def mul(self, *units):
     assert all(self.is_unit_converter(u) for u in units)
     return self.make_conversion(compose(*units[::-1]),
                                 compose(*(self.invert(u) for u in units)))