def unify_object_attrs(u, v, s, attrs): """ Unify only certain attributes of two Python objects >>> from logpy.unifymore import unify_object_attrs >>> from logpy import var >>> class Foo(object): ... def __init__(self, a, b): ... self.a = a ... self.b = b ... def __str__(self): ... return "Foo(%s, %s)"%(str(self.a), str(self.b)) >>> x = var('x') >>> y = var('y') >>> f = Foo(x, y) >>> g = Foo(1, 2) >>> print unify_object_attrs(f, g, {}, ['a', 'b']) #doctest: +SKIP {~x: 1, ~y: 2} >>> print unify_object_attrs(f, g, {}, ['a']) {~x: 1} This function is meant to be partially specialized >>> from functools import partial >>> unify_Foo_a = partial(unify_object_attrs, attrs=['a']) attrs contains the list of attributes which participate in reificiation """ gu = lambda a: getattr(u, a) gv = lambda a: getattr(v, a) return unify_seq(map(gu, attrs), map(gv, attrs), s)
def test_unify_seq(): assert unify_seq((1, 2), (1, 2), {}) == {} assert unify_seq([1, 2], [1, 2], {}) == {} assert unify_seq((1, 2), (1, 2, 3), {}) == False assert unify_seq((1, var(1)), (1, 2), {}) == {var(1): 2} assert unify_seq((1, var(1)), (1, 2), {var(1): 3}) == False
def unify_slice(u, v, s): """ Unify a Python ``slice`` object """ return unify_seq((u.start, u.stop, u.step), (v.start, v.stop, v.step), s)