weka/easypy

Add convenience functions to TypedStruct

Opened this issue · 0 comments

Assuming:

class Foo(TypedStruct):
    a = int
    a.default = 1

    b = str
    b.default = 'two'

We want the following functions:

  • Setting a field to DEFAULT should use the default as defined in the typed struct:
    assert Foo(a=DEFAULT, b='four') == Foo(a=1, b='four')
    foo = Foo(a=3, b='four')
    foo.b = DEFAULT
    assert foo == Foo(a=3, b='two')
  • .copy():
    foo = Foo(a=3, b='four')
    assert foo.copy() == foo
    assert foo.copy() is not foo
    Current behavior: foo.copy() returns a dict.
  • .update():
    foo = Foo(a=3, b='four')
    foo.update(a=5)
    assert foo == Foo(a=5, b='four')
    with pytest.raises():
        foo.update(a='six')
    Current behavior: update is dict.update which bypasses the verifications.
  • .but_with():
    foo = Foo(a=3, b='four')
    assert foo.but_with(a=5) == Foo(a=5, b='four')
  • Setting a field to AUTO should leave it unchanged:
    foo = Foo(a=3, b='four')
    foo.b = AUTO
    assert foo == Foo(a=3, b='four')
    This will be useful with update() and but_with():
    def make_foo(a=AUTO, b=AUTO):
        foo = Foo(a=3, b='four')
        foo.update(a=a, b=b)
        return foo
    
    assert make_foo() == Foo(a=3, b='four')
    assert make_foo(a=5) == Foo(a=5, b='four')
    assert make_foo(b='six') == Foo(a=3, b='six')