Skip to content

Commit 019c87b

Browse files
committed
[property] Use util.format_cardinality
Replaced value_cardinality implementation with usage of the more generic util.format_cardinality function.
1 parent 5287065 commit 019c87b

1 file changed

Lines changed: 7 additions & 44 deletions

File tree

odml/property.py

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from . import validation
1010
from . import format as frmt
1111
from .tools.doc_inherit import inherit_docstring, allow_inherit_docstring
12+
from .util import format_cardinality
1213

1314

1415
def odml_tuple_import(t_count, new_value):
@@ -528,7 +529,7 @@ def val_cardinality(self):
528529
"""
529530
The value cardinality of a Property. It defines how many values
530531
are minimally required and how many values should be maximally
531-
stored. Use 'values_set_cardinality' to set.
532+
stored. Use the 'set_values_cardinality' method to set.
532533
"""
533534
return self._val_cardinality
534535

@@ -549,50 +550,12 @@ def val_cardinality(self, new_value):
549550
:param new_value: Can be either 'None', a positive integer, which will set
550551
the maximum or an integer 2-tuple of the format '(min, max)'.
551552
"""
552-
invalid_input = False
553-
exc_msg = "Can only assign positive single int or int-tuples of the format '(min, max)'"
553+
self._val_cardinality = format_cardinality(new_value)
554554

555-
# Empty values reset the cardinality to None.
556-
if not new_value or new_value == (None, None):
557-
self._val_cardinality = None
558-
559-
# Providing a single integer sets the maximum value in a tuple.
560-
elif isinstance(new_value, int) and new_value > 0:
561-
self._val_cardinality = (None, new_value)
562-
563-
# Only integer 2-tuples of the format '(min, max)' are supported to set the cardinality
564-
elif isinstance(new_value, tuple) and len(new_value) == 2:
565-
v_min = new_value[0]
566-
v_max = new_value[1]
567-
568-
min_int = isinstance(v_min, int) and v_min >= 0
569-
max_int = isinstance(v_max, int) and v_max >= 0
570-
571-
if max_int and min_int and v_max > v_min:
572-
self._val_cardinality = (v_min, v_max)
573-
574-
elif max_int and not v_min:
575-
self._val_cardinality = (None, v_max)
576-
577-
elif min_int and not v_max:
578-
self._val_cardinality = (v_min, None)
579-
580-
else:
581-
invalid_input = True
582-
583-
# Use helpful exception message in the following case:
584-
if max_int and min_int and v_max < v_min:
585-
exc_msg = "Minimum larger than maximum (min=%s, max=%s)" % (v_min, v_max)
586-
else:
587-
invalid_input = True
588-
589-
if not invalid_input:
590-
# Validate and inform user if the current values cardinality is violated
591-
valid = validation.Validation(self)
592-
for err in valid.errors:
593-
print("%s: %s" % (err.rank.capitalize(), err.msg))
594-
else:
595-
raise ValueError(exc_msg)
555+
# Validate and inform user if the current values cardinality is violated
556+
valid = validation.Validation(self)
557+
for err in valid.errors:
558+
print("%s: %s" % (err.rank.capitalize(), err.msg))
596559

597560
def set_values_cardinality(self, min_val=None, max_val=None):
598561
"""

0 commit comments

Comments
 (0)