-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Description
In #2706 we assumed that filter(Variable.is_discrete, domain) would return discrete variables from domain. It doesn't.
is_discrete and is_continuous are currently instance properties that return bool and as such cannot be used as predicates. To be used as predicates, they would need to be class properties resolving to a function. Python's Descriptors actually foresaw this dual functionality. Implementation would be as follows.
class _IndicatorDescriptor:
def __init__(self, atype):
self.check_type = lambda instance: isinstance(instance, atype)
def __get__(self, instance, owner):
if instance is not None:
return self.check_type(instance)
else:
return self.check_type
Variable.is_discrete = _IndicatorDescriptor(DiscreteVariable)
Variable.is_continuous = _IndicatorDescriptor(ContinuousVariable)
Variable.is_string = _IndicatorDescriptor(StringVariable)
Variable.is_time = _IndicatorDescriptor(TimeVariable)
This requires that DiscreteVariable and ContinuousVariable are defined before Variable, which is impossible, so properties are inserted into Variable class at the end of the module. (Needless to say, Variable could have a metaclass that would ensure that any derived class would push its corresponding property into the base class Variable. Needless to hope, @astaric won't like it. :))
This now allows
>>> v = ContinuousVariable("x")
>>> v.is_continuous # current functionality
True
>>> Variable.is_continuous(v) # needed for filter predicates
True
>>> v.is_discrete
False
>>> Variable.is_discrete(v)
False
This doesn't work for is_primitive, which is a class method. A similar trick should work there, however.
Please comment before I continue with is_primitive, add tests etc.