9
9
from . import validation
10
10
from . import format as frmt
11
11
from .tools .doc_inherit import inherit_docstring , allow_inherit_docstring
12
+ from .util import format_cardinality
12
13
13
14
14
15
def odml_tuple_import (t_count , new_value ):
@@ -413,9 +414,7 @@ def values(self, new_value):
413
414
self ._values = [dtypes .get (v , self .dtype ) for v in new_value ]
414
415
415
416
# Validate and inform user if the current values cardinality is violated
416
- valid = validation .Validation (self )
417
- for err in valid .errors :
418
- print ("%s: %s" % (err .rank .capitalize (), err .msg ))
417
+ self ._values_cardinality_validation ()
419
418
420
419
@property
421
420
def value_origin (self ):
@@ -528,7 +527,7 @@ def val_cardinality(self):
528
527
"""
529
528
The value cardinality of a Property. It defines how many values
530
529
are minimally required and how many values should be maximally
531
- stored. Use 'values_set_cardinality' to set.
530
+ stored. Use the 'set_values_cardinality' method to set.
532
531
"""
533
532
return self ._val_cardinality
534
533
@@ -549,50 +548,22 @@ def val_cardinality(self, new_value):
549
548
:param new_value: Can be either 'None', a positive integer, which will set
550
549
the maximum or an integer 2-tuple of the format '(min, max)'.
551
550
"""
552
- invalid_input = False
553
- exc_msg = "Can only assign positive single int or int-tuples of the format '(min, max)'"
554
-
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 )
551
+ self ._val_cardinality = format_cardinality (new_value )
579
552
580
- else :
581
- invalid_input = True
553
+ # Validate and inform user if the current values cardinality is violated
554
+ self . _values_cardinality_validation ()
582
555
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
556
+ def _values_cardinality_validation (self ):
557
+ """
558
+ Runs a validation to check whether the values cardinality
559
+ is respected and prints a warning message otherwise.
560
+ """
561
+ valid = validation .Validation (self )
588
562
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 )
563
+ # Make sure to display only warnings of the current property
564
+ res = [curr for curr in valid .errors if self .id == curr .obj .id ]
565
+ for err in res :
566
+ print ("%s: %s" % (err .rank .capitalize (), err .msg ))
596
567
597
568
def set_values_cardinality (self , min_val = None , max_val = None ):
598
569
"""
0 commit comments