# HG changeset patch # User Sylvain Thénault <sylvain.thenault@logilab.fr> # Date 1285345740 -7200 # Fri Sep 24 18:29:00 2010 +0200 # Branch stable # Node ID b93df9e76dfe1a56690f54d6aced4dbec7179169 # Parent dc1e16c28343dff0792e5c6797544a0902ee80a8 introduce make_constant_restriction function, useful to build a restriction without adding it yet to the tree diff --git a/nodes.py b/nodes.py --- a/nodes.py +++ b/nodes.py @@ -105,6 +105,19 @@ relation.append(cmpop) return relation +def make_constant_restriction(var, rtype, value, ctype, operator='='): + if ctype is None: + ctype = etype_from_pyobj(value) + if isinstance(value, (set, frozenset, tuple, list, dict)): + if len(value) > 1: + rel = make_relation(var, rtype, ('IN',), Function, operator) + infunc = rel.children[1].children[0] + for atype in sorted(value): + infunc.append(Constant(atype, ctype)) + return rel + value = iter(value).next() + return make_relation(var, rtype, (value, ctype), Constant, operator) + class EditableMixIn(object): """mixin class to add edition functionalities to some nodes, eg root nodes @@ -163,18 +176,8 @@ variable rtype = value """ - if ctype is None: - ctype = etype_from_pyobj(value) - if isinstance(value, (set, frozenset, tuple, list, dict)): - if len(value) > 1: - rel = make_relation(var, rtype, ('IN',), Function, operator=operator) - infunc = rel.children[1].children[0] - for atype in sorted(value): - infunc.append(Constant(atype, ctype)) - return self.add_restriction(rel) - value = iter(value).next() - return self.add_restriction(make_relation(var, rtype, (value, ctype), - Constant, operator)) + restr = make_constant_restriction(var, rtype, value, ctype, operator) + return self.add_restriction(restr) def add_relation(self, lhsvar, rtype, rhsvar): """builds a restriction node to express '<var> eid <eid>'"""