Exemplo n.º 1
0
def test_rgetattr(separator):
    """Test to ensure recursive model access works.
    GUI elements are named f"prefix{_}" "recursive{_}attr" and bind to recursive.attr.

    https://stackoverflow{_}com/a/31174427/
    """
    p = Person()
    _ = separator

    # Test rgetattr(present)
    assert rgetattr(p, f"pet{_}species") == "Dog"
    assert rgetattr(p, f"pet{_}species", object()) == "Dog"

    # Test rgetattr(missing)
    assert rgetattr(p, f"pet{_}ghost{_}species", "calico") == "calico"
    with pytest.raises(AttributeError):
        # Without a default argument, `rgetattr`, like `getattr`, raises
        # AttributeError when the dotted attribute is missing
        print(rgetattr(p, f"pet{_}ghost{_}species"))

    # Test rsetattr()
    rsetattr(p, f"pet{_}name", "Sparky")
    rsetattr(p, f"residence{_}type", "Apartment")
    assert p.pet.name == "Sparky"
    assert p.residence.type == "Apartment"

    # Test rhasattr()
    assert rhasattr(p, f"pet")
    assert rhasattr(p, f"pet{_}name")

    # Test rhasattr(levels of missing)
    assert not rhasattr(p, f"pet{_}ghost")
    assert not rhasattr(p, f"pet{_}ghost{_}species")
    assert not rhasattr(p, f"ghost")
    assert not rhasattr(p, f"ghost{_}species")
Exemplo n.º 2
0
 def setter(self: "ConfigModel", val: str):
     val = path_strip_quotes(val)
     rsetattr(self.cfg, path, val)
Exemplo n.º 3
0
 def setter(self: "ConfigModel", val):
     rsetattr(self.cfg, path, val)