timtadh/zhang-shasha

Add __sub__ dunder method

danvergara opened this issue · 1 comments

Changes in the Node class

The Node class is handy and well designed. It's used intensively in my project called vanguard-kit, but we noticed that it'd be even better and simpler to use. How to do that? Simple, add a __sub __ dunder method to call the simple_distance top-level function. In fact we extended its behavior by inheritance to achieve that, but we think that behavior should be in implemented in the original class.

I'll take your example to extend my explanation:

from zss import simple_distance, Node

A = (
    Node("f")
        .addkid(Node("a")
            .addkid(Node("h"))
            .addkid(Node("c")
                .addkid(Node("l"))))
        .addkid(Node("e"))
    )
B = (
    Node("f")
        .addkid(Node("a")
            .addkid(Node("d"))
            .addkid(Node("c")
                .addkid(Node("b"))))
        .addkid(Node("e"))
    )

We got these trees, A and B. Then, to calculate the simple distance, the simple_distance function is called explicitly:

assert simple_distance(A, B) == 2

Now, image if we implement the __sub __ dunder method:

assert A - B == 2

For us, this way to express distance is more natural.

Thank you again for the contribution. This feature request was merged in at #51