compare option

This commit is contained in:
Emmanuel Garette 2013-05-05 21:43:19 +02:00
parent 2d57ed160e
commit 1b29793e0f
2 changed files with 72 additions and 8 deletions

33
test/test_option.py Normal file
View file

@ -0,0 +1,33 @@
import autopath
from tiramisu.option import BoolOption, IntOption
def test_option_comparison():
dummy1 = BoolOption('dummy1', 'doc dummy')
dummy2 = BoolOption('dummy2', 'doc dummy')
dummy3 = BoolOption('dummy1', 'doc dummy')
assert dummy1 != dummy2
assert dummy1 == dummy3
def test_option_comparison_obj():
dummy1 = BoolOption('dummy1', 'doc dummy')
dummy2 = IntOption('dummy1', 'doc dummy')
assert dummy1 != dummy2
def test_option_comparison_advanced():
dummy1 = BoolOption('dummy1', 'doc dummy')
dummy2 = BoolOption('dummy1', 'doc dummy')
dummy3 = BoolOption('dummy1', 'doc dummy', None)
dummy4 = BoolOption('dummy1', 'doc dummy', True)
dummy5 = BoolOption('dummy1', 'doc dummy', multi=True)
dummy6 = BoolOption('dummy1', 'doc dummy', properties=tuple())
dummy7 = BoolOption('dummy1', 'doc dummy', properties=tuple('new',))
assert dummy1 == dummy2
assert dummy1 == dummy3
assert dummy1 != dummy4
assert dummy1 != dummy5
assert dummy1 == dummy6
assert dummy1 != dummy7

View file

@ -53,7 +53,7 @@ def valid_name(name):
class BaseInformation(object): class BaseInformation(object):
__slots__ = ('_informations') __slots__ = ('_informations',)
def optimpl_set_information(self, key, value): def optimpl_set_information(self, key, value):
"""updates the information's attribute """updates the information's attribute
@ -172,6 +172,37 @@ class Option(BaseInformation):
' must be a tuple').format(type(properties), self._name)) ' must be a tuple').format(type(properties), self._name))
self._properties = properties # 'hidden', 'disabled'... self._properties = properties # 'hidden', 'disabled'...
def __eq__(self, other):
"Option comparison"
if not isinstance(other, Option):
return False
a = list(self.__slots__ + Option.__slots__ + BaseInformation.__slots__)
#a.remove('_default_multi')
a.remove('_master_slaves')
a.remove('_multitype')
for var in a:
try:
val1 = getattr(self, var)
not_in1 = False
except:
not_in1 = True
try:
val2 = getattr(other, var)
not_in2 = False
except:
not_in2 = True
if True in (not_in1, not_in2):
if not_in1 != not_in2:
return False
elif val1 != val2:
return False
return True
def __ne__(self, other):
if not isinstance(other, Option):
return False
return not self == other
def optimpl_validate(self, value, context=None, validate=True): def optimpl_validate(self, value, context=None, validate=True):
""" """
:param value: the option's value :param value: the option's value
@ -342,7 +373,7 @@ class ChoiceOption(Option):
class BoolOption(Option): class BoolOption(Option):
__slots__ = ('_opt_type') __slots__ = ('_opt_type',)
_opt_type = 'bool' _opt_type = 'bool'
def _validate(self, value): def _validate(self, value):
@ -350,7 +381,7 @@ class BoolOption(Option):
class IntOption(Option): class IntOption(Option):
__slots__ = ('_opt_type') __slots__ = ('_opt_type',)
_opt_type = 'int' _opt_type = 'int'
def _validate(self, value): def _validate(self, value):
@ -358,7 +389,7 @@ class IntOption(Option):
class FloatOption(Option): class FloatOption(Option):
__slots__ = ('_opt_type') __slots__ = ('_opt_type',)
_opt_type = 'float' _opt_type = 'float'
def _validate(self, value): def _validate(self, value):
@ -366,7 +397,7 @@ class FloatOption(Option):
class StrOption(Option): class StrOption(Option):
__slots__ = ('_opt_type') __slots__ = ('_opt_type',)
_opt_type = 'string' _opt_type = 'string'
def _validate(self, value): def _validate(self, value):
@ -374,7 +405,7 @@ class StrOption(Option):
class UnicodeOption(Option): class UnicodeOption(Option):
__slots__ = ('_opt_type') __slots__ = ('_opt_type',)
_opt_type = 'unicode' _opt_type = 'unicode'
_empty = u'' _empty = u''
@ -428,7 +459,7 @@ class IPOption(Option):
class NetworkOption(Option): class NetworkOption(Option):
__slots__ = ('_opt_type') __slots__ = ('_opt_type',)
_opt_type = 'network' _opt_type = 'network'
def _validate(self, value): def _validate(self, value):
@ -440,7 +471,7 @@ class NetworkOption(Option):
class NetmaskOption(Option): class NetmaskOption(Option):
__slots__ = ('_opt_type') __slots__ = ('_opt_type',)
_opt_type = 'netmask' _opt_type = 'netmask'
def __init__(self, name, doc, default=None, default_multi=None, def __init__(self, name, doc, default=None, default_multi=None,