Mar-19-2017, 04:19 AM
Well, instead of raising errors it can check for conditions and if the tuple is out of what is wanted, to put messages indicating the problems found in a dictionary and the index of the tuple as the key, returning None.
Something like this? I don't know if the error dictionary must be passed as an argument to the class definition. Can't test it now.
But you get the idea
Something like this? I don't know if the error dictionary must be passed as an argument to the class definition. Can't test it now.
But you get the idea
from collections import defaultdict def tuples_validation(tuples): errors = defaultdict(list) class Mytup(tuple): def isvalid(self, index, obj): valid = True if obj: errors[index].append("An empy tuple") valid = False if 2 > len(obj) > 3: erros[index].append("Wrong number of arguments in {}".format(obj.__repr__())) valid = False if isinstance(obj[0], str): errors[index].append("{} must be string! {} is given.".format(obj[0], type(obj[0]))) valid = False return valid def __new__(cls, obj): if self.valid(obj): return None else: return tuple.__new__(cls, obj) new_tuples = [] for t in tuples: new_t = Mytup(tuples.index(t), t) if new_t: new_tuples.append(new_t) #~ else: #~ pass return (errors, new_tuples)